My code says invalid if I input a valid number of days and it prompts the user again to enter days but it reads the first input | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

My code says invalid if I input a valid number of days and it prompts the user again to enter days but it reads the first input

https://code.sololearn.com/c0yilUYhkwkq/?ref=app The loop was supposed to keep reading input until a numeric input was given. It allows max of 3 invalid inputs (letter etc.) and it will exit the loop when <bad_inputs> reaches 3. After the loop is done, there we check whether or not <days> was valid. If it is valid, then continue with calculation. std::cout<<"\nPlease enter valid days:"; do { do { std::cin>> std::setw(1)>> days; std::cin.clear(); std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n'); if(days >= 0|| days <= 31) { bad_inputs++; std::cin.clear(); } if (bad_inputs) { std::cout<<"You have entered an invalid number"; std::cout<<"Please enter a number again:"; } }while (!(days >= 0|| days <= 31)&& bad_inputs <3); std::cin.clear(); std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n'); }while ((days >= 0|| days <= 31)&& bad_inputs <3);

10th Dec 2021, 9:22 AM
shlee_
shlee_ - avatar
8 Answers
+ 1
#include <iostream> using namespace std; int main() { int days{},bad_inputs {}; std::cout<<"\nPlease enter valid days:\n"; do { std::cin>>days; std::cin.clear(); //std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n'); if(days <= 0 || days >31) { bad_inputs++; std::cin.clear(); std::cout<<"You have entered an invalid number"<<endl; if(bad_inputs <3) std::cout<<"Please enter a number again:\n"; } }while (!(days >= 0 && days <= 31)&& bad_inputs <3); cout<<days; return 0; }
10th Dec 2021, 7:36 PM
Jayakrishna 🇮🇳
+ 1
Jayakrishna🇮🇳 still it asks the user to input again a valid day three times even if it's valid
12th Dec 2021, 8:48 AM
shlee_
shlee_ - avatar
+ 1
std::cout<<"\nPlease enter valid days:"; do{ std::cin>>days; std::cin.clear(); if (!(days >= 0 && days < 31)) std::cin.clear(); cout << " \n\t-------------------------------------------------------\n"; //Nested switch switch (vehicle) { //If vehicle == 'A' case 'A': //Toyota Hi Ace calculations ... std::cin.ignore (256, '. '); break; } while( days <= 0 && days < 31); I change it .. I remove the bad inputs , It doesn't exit the code normally Jayakrishna🇮🇳
12th Dec 2021, 11:45 AM
shlee_
shlee_ - avatar
12th Dec 2021, 11:55 AM
shlee_
shlee_ - avatar
+ 1
Your while condition is true only for 0 or negetive values... So other inputs, loop can't iterate. You don't have any other exit condition. I code above works for max 3 invalid inputs. And works for minimum single valid input. I checked it. Do you checked it..?
12th Dec 2021, 2:12 PM
Jayakrishna 🇮🇳
0
Jay Matthews it still says invalid and please enter again
10th Dec 2021, 9:49 AM
shlee_
shlee_ - avatar
0
Jay Matthews now when if (days<1 || days>31) { bad_inputs++; cin.clear(); } It doesn't proceed to calculations even when I entered a valid number of days
10th Dec 2021, 11:47 AM
shlee_
shlee_ - avatar
0
I wrap the case 2 with while loop because that code above that u write doesn't proceed to calculations Jayakrishna🇮🇳
12th Dec 2021, 2:15 PM
shlee_
shlee_ - avatar