This is a very odd problem that I just can't seem to figure out! I am getting a cin line twice when I should only get one. Can anyone help with this? I don't even know how to google this problem, to be honest.
#include<iostream>
usingnamespace std;
int main()
{
//assign variables
int number;
//user defined variables
cout << "Input the number: ";
cin >> number;
if (!(cin >> number))
{
cin.clear();
cout << "Invalid Input! Input the month (numerically): ";
cin.ignore( 100 , '\n' );
}
cout << number << endl;
return(0);
}
This is a working piece of the code with an output for verification. Weird thing is, it takes the second input as the cin. Where is the first input coming from?
You use the istream object cin on both line 12 and 13. Advice, remove the one on line 12. Also there are no while statements in your code, just if-statement
The reason I mentioned while is because if I switch while in for if, I get the exact same issue. I appreciate your response! I didn't realize that having the cin in the if statement would actually call for an input! Noob mistakes!