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