#include <iostream>
#include <string>
usingnamespace std;
int main (){
int myFaveNumber;
string faveClass;
cout << "What is your favorite number? (integers only, please): ";
cin >> myFaveNumber;
cout << "What is your favorite class?";
getline (cin, faveClass);
cout << "\nMy favorite class is " << faveClass << ", and my favorite number is "
<< myFaveNumber << ".\n";
return 0;
}
#include <iostream>
#include <string>
usingnamespace std;
int main (){
int myFaveNumber;
string faveClass;
cout << "What is your favorite number? (integers only, please): ";
cin >> myFaveNumber;
cout << "What is your favorite class?";
cin>>faveClass;
cout << "\nMy favorite class is " << faveClass << ", and my favorite number is "
<< myFaveNumber << ".\n";
return 0;
}
What's wrong with using ignore()? Sometimes that is the best way of proceeding.
If your problem is the skipping of the getline() then you could try:
getline (cin >> ws, faveClass);
However this won't solve any problem other than skipping leading whitespace, if you enter an incorrect entry for myFaveNumber you will still skip any other entries.
monrelle you can just put first the question of getting favorite class and the second question will be the favorite number. You will not now worry of using cin.ignore.
1 2 3 4
cout << "What is your favorite class?";
cin>>faveClass;
cout << "What is your favorite number? (integers only, please): ";
cin >> myFaveNumber;