How can i make a string that will allow me to Use Spaces between the words ???

This code only allows me to post words without spaces.

cout << "   Enter the Name of Employee # "   << i+1 << ": ";
		cin >> N;
		myClass[i].setName( N );

I am not entirely sure, but I think you might want to use cin.getline() for this. I think that will help.

depends on how variable crap was defined. If its a character array char crap[255]; then you are right. But if its a std::string then jamthwee is right. And jamthwee shows the prefered c++ method.

I'm Using a String.

string crap;
getline(cin,crap,'\n');

It worked but when it doesn't print the first name, It only prints the second name and on..


I have used cin.getline() but it gives me acompile error:

cout << "   Enter the Name of Employee # "   << i+1 << ": ";
		cin.getline(N);
		myClass[i].setName( N );

I have used getline() but that didn't stop the compiler to get a value..

string N;
 
		// Set the Name of the Employee
		cout << "   Enter the Name of Employee # "   << i+1 << ": ";
		getline ( cin, N );
		myClass[i].setName( N );

>I have used getline() but that didn't stop the compiler to get a value..
Post a full program that has the problem. I'm guessing that you have a cin>>[I]<blah>[/I] somewhere before the getline, and the getline is eating the leftover newline.

int n; // declare n of type integer

	cout << " \n\n";
	cout << "             SET THE NUMBER OF EMPLOYEES" << endl;
	cout << "             --> ";
	cin >> n;

	///////////////////////////////////////////////////
	// Loop until 'n' number of employees is reached //
	// & Enter the data for each number of Employees //
	// using the set functions                       //
	///////////////////////////////////////////////////
	for ( int i = 0 ; i < n ; i++ )
	{
		system("CLS");  // clear the screen

		cout << "\n\n";

		cout << "   xxxxxxxxxxxxxxxx" << endl;
		cout << "   x EMPLOYEE # " << i+1 << " x" << endl;
		cout << "   xxxxxxxxxxxxxxxx" << "\n\n";

		// Set the Name of the Employee
		cout << "   Enter the Name of Employee # "   << i+1 << ": ";
		getline ( cin, N );
		myClass[i].setName( N );

Damn, I'm good. cin >> n; reads an integer, but leaves a newline on the stream. getline ( cin, N ); is supposed to stop reading when it finds a newline, so it looks like the call is getting skipped. There's a sticky thread about clearing the input stream that goes into a lot of detail about fixing the problem. But the best way to avoid it is not to use formatted input in the first place. Read everything as a string and use stringstreams (or boost::lexical_cast) to convert the string into the type you want.

I have used

int n = getch()

It fixed the problem but in " for " in the second round the problem came back...

This article has been dead for over six months. Start a new discussion instead.