I am using get.fail() to check if there's any char in the input, and if there is I would like to give the user a chance to re-enter. However, the program seems to still accept the user input whenever there's an integer in front of the input no matter the case. Say w1 and 1w, the program will tell the user that the it only accepts integers while the latter one accepts the input and moves over to the next line which then causes another problem.
void userChoice(int input){ switch(input) { case 1: insert(); break; case 2: display(); break; case 3: update_data(); break; case 4: delete_position(); break; case 5: cout<<"Thank you for using the program\n"; exit(0); break; case 6: tellSize(); break; default: cout<<"Not an option\n"; cin>>input; while(cin.fail()) { cin.clear(); cin.ignore(INT_MAX, '\n'); cin>>input; break; } userChoice(input); } } Referring to the code above, say I give the input 1w. The program will still execute case 1 as if there's nothing wrong, and then w is somehow passed into the insert() function which is not what I want. I would like the program to let the user re-enter the input no matter if it's 1w or w1, in short I do not want the program to move over to the next line if there's a char in an integer input.
tl;dr: Why does the code below still execute when the cin is 1w, shouldn't it print "Enter number only" since there's a character in there?
Edit: Here's a quick program I made, to reproduce the error I am facing, I first enter 1h and here's the first bug I'm facing, why is the program still executing when there's a char h in the input? Afterwards in the second input, I enter 2w and the program prints out 2, shouldn't the program loop the while loop since there's a char w in the input?
#include<iostream> #include<iomanip> #include<limits> using namespace std; void option_1() { int amount_input; cout<<"Enter the amount of cake"<<endl; cin>>amount_input; while(cin.fail()) { cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n'); cout<<"Enter number only\n"; cin>>amount_input; } cout<<amount_input; } void options(int input) { bool fail; switch(input) { case 1: option_1(); break; default: cout<<"Not an option"<<endl; cin>>input; while(cin.fail()) { cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n'); cin>>input; break; } options(input); } } void menu(){ int user_input; cout<<"Enter 1 to print something\n"; cin>>user_input; options(user_input); } int main(){ menu(); }
userChoice()parameter. The problem occurs when I pass1was a parameter to this function, as1gets accepted andwgets passed intoinsert(), that's not what I want. I wanted the program to loop the switch statement if there's a char in an input.1w-"w is somehow passed into the insert() function"- NO,wis left unread in the input stream and your switch completes successfully.w1- integer input fails,cin.fail()tests TRUE, you clear the error state, empty the input stream withcin.ignore(INT_MAX, '\n');(INT_MAXshould bestd::numeric_limits<std::streamsize>::max()after#include <limits>) and you wait for input again. What's the problem there? MCVE needed.cin>>user_input;putcout << "input is " << user_input << endl;and see what it says. You are not passing what you think you're passing.