the beginning, cin | cin.get | cout | string | iostream

Learning a programming language is hard enough without having to wrangle your computer into compiling and executing your code. From beginners to experts, the tips here will make life in C++ easier.
Post Reply
darknkreepy3#
Site Admin
Posts: 247
Joined: Tue Oct 27, 2009 9:33 pm

the beginning, cin | cin.get | cout | string | iostream

Post by darknkreepy3# »

Code: Select all

/*
C++ The Beginning Step by Kristoffe Brodeur.
06-11-2010 compiles in DEV C++ 4.9.9.2 and windows 7
*/
#include <cstdlib>
#include <iostream>

using namespace std;

int main()
	{
	int year=2010;
	cout << "This was written in " << year << endl;
	
	//-------------------------------------------------------------------------
	cout << "\n*****COUT AND IN, AND CLEARING THE BUFFER\n";
	char firstInitial;
	cout << "What is your first name's initial letter:";
	cin >> firstInitial;
	//after cin input, call cin again and clear the input buffer if its more than one character
	cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
	
	cout << "\n"; // "\n" = endl (newline,endline,etc...)
	
	//single quotes as ouput, double quotes cut strings and code inserts char 'firstInitial'
	cout << "Your first initial is '" << firstInitial << "'\n";
	
	//-------------------------------------------------------------------------
	cout << "\n*****CHAR ARRAYS STORING INPUT\n";
	//now make a char array with 9 places reserved in memory [0][1][2][3][4][5][6][7][8]
	char theQuickBrownA[9];
	//create a buffer for 9 characters to input
	char tempChars[9];
	//identify that length in a variable named 'cLength'
	int cLength=9;
		
	cout << "9 character limit test. Type in 'The Quick Brown Fox Jumped Over the Lazy Dog.\n";
	cin >> tempChars;
	//anything typed past 9 characters will be in 'tempChars' past the original 9th position
	// simple 'cin' stops input after a space ' ' and you will only see 'The'
	
	//use a counted length string copy
	//strncpy(destination,source,length of characters to copy from position 0)
	strncpy(theQuickBrownA,tempChars,cLength);
	//now, in C++, it likes to have a NULL character which is expressed as '\0' ending the string of chars
	//if you comment this line below out, you will see random garbage output to the screen
	theQuickBrownA[cLength]='\0';
	
	//again clear the input buffer past 9 characters
	//so the next cin won't already have input going into it
	cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n' );
	cout << "What C++ remembers:" << theQuickBrownA << endl;	
	
	//-------------------------------------------------------------------------
	cout << "\n*****LIMITING THE CIN CHARS TO SEND\n";
	//a newline one right after another, \n\n, created two carriage returns
	cout << "An easier way of limiting user input is by the 'getline' function.\n\n";
	cout << "Please enter (16 characters)\n" << "0123456789abcdef:";
	//the first 10 characters are 0123456789, its NULL at the end makes 11
	//[0][1][2][3][4][5][6][7][8][9]['/0']
	int limEx=10;
	char numEx[limEx];
	/*
	the get function in cin allows
	(array to store input,limit to send to that array-1)
	get puts a NULL at the end of the process when it stores the input '\0'
	so if you want 10 characters from cin.get and the NULL if produces at the end
	that would be limEx+1
	*/
	cin.get(numEx,limEx+1);
	//again, clear all excess garbage 
	cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n' );
	
	cout << "\nThis is what C++ remembers of numEx:" << numEx << endl;
	//-------------------------------------------------------------------------
	cout << "\n*****STRINGS ARE CHAR ARRAYS\n";
	//a string in C++ is an char array[] automatically managed by the string class
	string partyTitle="John Doe's 50th Birthday Party";
	cout << "Our hotel is booked for " << partyTitle << "\n";
	
	system("PAUSE");
	return EXIT_SUCCESS;
	}
Post Reply