While going through the C++ articles on this site to refresh my mind about C++ for the upcoming ACSL Competition, I found a section on Basic Input/Output:
http://www.cplusplus.com/doc/tutorial/basic_io/
I subsequently copied the exact code from the programming example under "cin and strings" into a .cpp file in a Win32 Console Application project workspace in Microsoft Visual C++ 6.0:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
// cin with strings
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string mystr;
cout << "What's your name? ";
getline (cin, mystr);
cout << "Hello " << mystr << ".\n";
cout << "What is your favorite team? ";
getline (cin, mystr);
cout << "I like " << mystr << " too!\n";
return 0;
}
|
I ran this, it prompted me "What's your name? " and I entered "A B" and hit the Enter key [this is a Windows XP SP3 Home Professional or something similar, I doubt it makes that big a difference]. The cursor then jumped to the next line, but it did not output anything, as if expecting some more input. After waiting enough time, I entered "C D" and hit enter again. This time it displayed "Hello A B." and then a new line. On this new line it displayed "What is your favorite team? " to which I typed in "E F" and hit Enter. The next line it displayed was "I like C D too!" and then indicated that the program was finished. The final input/output of the program looked like this:
What's your name? A B
C D
Hello A B.
What is your favorite team? E F
I like C D too!
Press any key to continue |
This was not expected. After browsing through a multitude of getline() problems after using cin >> x; to get an int x, I still was unsure of exactly why getline() was behaving this way, so I did some tweaking. I tried removing the first getline() statement, and now it jumped the entering of the name (as expected) but it still required two lines for the second getline() [for the favorite team]. The I/O now looked like this:
What's your name? Hello .
What is your favorite team? A B
C D
I like A B too!
Press any key to continue |
Inserting cin.ignore() statements just reduced the output. The following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
// cin with strings
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string mystr;
cout << "What's your name? ";
cin.ignore();
getline (cin, mystr);
cin.ignore();
cout << "Hello " << mystr << ".\n";
cout << "What is your favorite team? ";
cin.ignore();
getline (cin, mystr);
cin.ignore();
cout << "I like " << mystr << " too!\n";
return 0;
}
|
produced these results:
What's your name? Alex B
John G
Hello lex B.
What is your favorite team? The Jackrabbits
I like hn G too!
Press any key to continue |
Removing the second getline() [while keeping the first] did not help either, as this time it did not ask for a team but instead reprinted mystr:
What's your name? A B
C D
Hello A B.
What is your favorite team? I like A B too!
Press any key to continue |
Extending this theme led to a continued offset of getline()'s input; the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
// cin with strings
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string mystr;
cout << "What's your name? ";
getline (cin, mystr);
cout << "Hello " << mystr << ".\n";
cout << "What is your favorite team? ";
getline (cin, mystr);
cout << "I like " << mystr << " too!\n";
cout << "What is your favorite pizza topping? ";
getline (cin, mystr);
cout << "That's disgusting? Who eats " << mystr << " on their pizza?\n";
cout << "What is your favorite color? ";
getline (cin, mystr);
cout << "NI! " << mystr << " is forbidden!\n";
return 0;
}
|
ideally does the same as the one in the example, just with a few more questions. The I/O for my test runs always ended up like this instead:
What's your name? Alex B
John G
Hello Alex B.
What is your favorite team? The Jackrabbits
I like John G too!
What is your favorite pizza topping? Pepperoni
That's disgusting? Who eats The Jackrabbits on their pizza?
What is your favorite color? Orange
NI! Pepperoni is forbidden!
Press any key to continue |
Again, this is a .cpp file in a Win32 Console Application project in Microsoft Visual C++ 6.0, with a standard Windows XP operating system. Is getline() supposed to function this way? Am I missing some important step here?