Hi everyone. I just started studying C++ and I have encountered a weird error when learning I/O.
When I run this piece of code, the programm waits for me to input two words, but as soon as i input them it doesn't wait for me to enter the second set of words, but just displays the "thank you" and quits.
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "Please enter two words:" << endl;
string b, c;
cin >> b >> c;
cout << "I understood: "
<< b << ", and "
<< c << endl;
cout << "Please enter full name: ";
string str;
getline (cin,str);
cout << "Thank you, " << str << ".\n";
return 0;
}
However when i switch the two bits of the code like below it allows me to enter the data two times. Can you please tell me what the problem is, because it is driving me mad and slows down my progress.
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "Please enter full name: ";
string str;
getline (cin,str);
cout << "Thank you, " << str << ".\n";
cout << "Please enter two words:" << endl;
string b, c;
cin >> b >> c;
cout << "I understood: "
<< b << ", and "
<< c << endl;
return 0;
}
If you have any suggestions as to why it happens I would be really greatful
Thanks for the reply. It's a bit complicated concept for me to understand at this point. I have tried to run a new programm from the same course. it's basically just a bunch of getline() and stuff. very easy. the code is below
int main()
{
cout << "Welcome to the C++ News Network!" << endl << endl;
string userName;
cout << "Please tyoe in your first name: " << endl;
cin >> userName;
int smallNumber;
cout << "How many siblings do you have?" << endl;
cin >> smallNumber;
float largeNumber;
cout << "How much money would you like to earn every year?" << endl;
cin >> largeNumber;
string color;
cout << "Tell us your favourite color:" << endl;
cin >> color;
string amorphousObject;
cout << "Which vegetables have the wirdest shapes?";
getline( cin, amorphousObject );
string deadGuy;
cout << "Name a famous dead person" << endl;
getline( cin, deadGuy );
string celebrityActor;
cout << "Who is your favourite actor?" << endl;
getline( cin, celebrityActor );
string politician;
cout << "Name a current world leader" << endl;
getline( cin, politician );
string cartoonCharacter;
cout << "Who is your favourite cartoon character?" << endl;
getline( cin, cartoonCharacter );
string weirdGroup;
cout << "Name a hobby or a profession you find scary" << endl;
getline( cin, weirdGroup );
string somethingGross;
cout << "Name a food item you detested as a child" << endl;
getline( cin, somethingGross );
cout << endl << endl << endl
<< "And now, today's headlines from the C++ News Wire:"
<< endl;
cout << "-------------------------------------------------"
<< endl;
cout <<"ALIENS SHAPED LIKE " << color << " " << amorphousObject
<< " INVADE THE EARTH, KIDNAP " << celebrityActor << ", "
<< "RESURRECT " << deadGuy << "!" << endl << endl;
cout << userName
<< " RELEASES NEW ALBUM! " << smallNumber
<< " COPIES EXPECTED TO BE SOLD!"
<< endl << endl;
cout << politician << " CAUGHT IN LOVE TRIANGLE WITH "
<< cartoonCharacter << " AND SECRET "
<< weirdGroup << " CULT LEADER!" << endl << endl;
cout <<"WORLD'S LARGEST BABY BORN - WEIGHS " << largeNumber
<< " POUNDS, EATS " << smallNumber << " TONS OF "
<< somethingGross << " EVERY DAY!" << endl << endl;
return 0;
}
the problem is when i run it it all runs as it supposed to except for the bit where it gets to the first getline() statement. it just doesn't allow me to input anything, and than continues to ask me for input for the rest of them. ANd when the headlines start appearing, the space that is supposed to be for info that i can't input is empty. I know that probably later i will encounter the limits and everything, but is there a temporary solution just for now so that it does ask me for the inoput?