Problem with user input....

Hi.. I ma just starting to learn programming in my spare time as a hobby kind of thing.. so I do not have a teacher.. just a few books I am reading.

Anyway, the exercise in the book says to make a simple program to ask a user to input his name and then print out the name to the user.... the trick of the exercise is that the info they give you in the chapter is in fact not able to work.. so the rest of the question is "tell us why it doesn't work, and then crate a fix by any means necessary"

Anyway, after reading about iostream on this site a bit I found a solution using getline. Still... I can not seam to work out how to get everything to work in a single app.. it is like the input buffer is carried over so I am unable to use getline and the normal cin in the same line...

Here is my code as it is now...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string name;

	cout << "Please Enter your name: ";
	cin >> name;
	cout << endl << endl << "Your name is: "<< name << endl << endl;
	cout << "The application will not store the variable for string past the space." << endl << "So even though you type your full name only the first part up to the" << endl << "first space is stored" << endl << endl ;

	system("Pause");
}


It looks like this
http://www5.picturepush.com/photo/a/2613873/img/2613873.jpg

Now this is the code that works....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string namefix;

	cout << "Please Enter your name again: ";
	getline(cin, namefix);
	cout << endl << endl << "Your name is: "<< namefix << endl << endl;
	system("Pause");
}


It looks like this
http://www1.picturepush.com/photo/a/2613874/img/2613874.jpg

Anyway, when i try to combine these two codes together, something weird happens.. even though I am using the code that works individually in separate projects.. when combines the 2nd input, doesn't allow the user to input anything, but instead uses the bit of the originally inputed string that comes after the space AS the user input instead of the user entering a totally new string.

This is the code I am using that DOESN'T work
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string name;
	string namefix;

	cout << "Please Enter your name: ";
	cin >> name;
	cout << endl << endl << "Your name is: "<< name << endl << endl;
	cout << "The application will not store the variable for string past the space." << endl << "So even though you type your full name only the first part up to the" << endl << "first space is stored" << endl << endl ;

	cout << "Please Enter your name again: ";
	getline(cin, namefix);
	cout << endl << endl << "Your name is: "<< namefix << endl << endl;

	system("Pause");
}


It looks like this...
http://www3.picturepush.com/photo/a/2613876/img/2613876.jpg


I tried looking at cin.clear() but I couldn't get it to work.

So basically I am asking how can I completely clear the input buffer after the first input request... ?

Any ideas?

Thanks in advance
Last edited on
Use cin.ignore(). You can just trial-and-error (keep on calling it until you find the prog works); I don't recall how to clear the entire input stream.
And I hope you don't plan to keep the system call.
ok thanks... I think i got it working now... . . .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string name;
	string namefix;

	cout << "Please Enter your name: ";
	cin >> name;
	cin.ignore(1000,'\n');
	cout << endl << endl << "Your name is: "<< name << endl << endl;
	cout << "The application will not store the variable for string past the space." << endl << "So even though you type your full name only the first part up to the" << endl << "first space is stored" << endl << endl ;

	cout << "Please Enter your name again: ";
	getline(cin, namefix);
	cout << endl << endl << "Your name is: "<< namefix << endl << endl;
	cout << "Instead of using cin we use getline to grab the user input. The command" << endl << "getline will allow the inclusion of spaces, so you can basically type a" << endl << "line into the user input and it will work" << endl << endl;

	system("Pause");
}


As for the system call.... why not... what I want is to have the "press any key" at the end of the application. when you run it normaly.. just like it dose when in debug mode in vs2008.

If you no a better way to do that please let me know. It seams to work though
Topic archived. No new replies allowed.