+ 3

I wanted Read input in a loop, and keep asking for input until user chooses a valid choice. Please help me

I use nested switch case https://code.sololearn.com/cAIgz08Vhqs6/?ref=app

8th Dec 2021, 7:49 AM
shlee_
shlee_ - avatar
20 odpowiedzi
+ 1
#include <iostream> using namespace std; int main() { int i, days,choice; char place,vehicle; bool valid_place=true,valid_vehicle=true; cout<<"\n\t\t WELCOME TO ABC Tours\t\n\n"; cout<<"[1]PLACES\n[2]VEHICLE\n"; cout <<"\nEnter choice\n"; while(true){ cin>>choice; if(choice==1 or choice==2) break; cout<<"Enter again\n"; } //outer switch switch(choice) { case 1: for( i=0; i <5; i++){ cout<<"\n\t\t\t=Your Choice?=\n\n"; do{ cin>>place; //Nested switch switch(place){ //If place=='a' case 'a': cout <<"\na\n"; break; default: valid_place=false; cout<<"Invalid Selection\nenter valid place\n"; break; } }while(!valid_place); valid_place = true; } break; case 2: cout<< "\nEnter a letter for desired vehicles:"; do{ cin>>vehicle; cout<<"\nNumber of day trips:"; cin>> days; valid_vehicle = true; //Nested switch switch(vehicle){ //If vehicle == 'A' case 'A': cout <<"\n\nTotal Payment: TP"<<endl; break; default: valid_vehicle=false; cout<<"Invalid selection\nenter again\n"; } }while(!valid_vehicle); } return 0; }
8th Dec 2021, 5:31 PM
Jayakrishna 🇮🇳
+ 3
Jayakrishna🇮🇳 my input sample in days is a number and the code crashes Then when I input an invalid like. Letter a for example
9th Dec 2021, 11:00 AM
shlee_
shlee_ - avatar
+ 2
I have nested switch case
8th Dec 2021, 10:27 AM
shlee_
shlee_ - avatar
+ 2
Jayakrishna🇮🇳 I have problems with my code in number of days The validation of input works in vehicle but when it ask the user to input a number of days And when I input a number or letter it suddenly exits the code It doesn't ask again the user to input a valid days then even if you input a valid day still exits the code and it doesn't proceed to calculations https://code.sololearn.com/c0yilUYhkwkq/?ref=app
9th Dec 2021, 7:45 AM
shlee_
shlee_ - avatar
+ 2
Jayakrishna🇮🇳 Because I wanted it to proceed on the vehicle . when it ask the user to input a number of days And when I input a number or letter it suddenly exits the code It doesn't ask again the user to input a valid days then even if you input a valid day still exits the code and it doesn't proceed to calculations I try to input letter or number one time then it crash https://code.sololearn.com/c0yilUYhkwkq/?ref=app
9th Dec 2021, 9:12 AM
shlee_
shlee_ - avatar
+ 2
Jayakrishna🇮🇳 In days part only it crashes the code case 2: // VEHICLE... do { std::cout<< "\n Enter a letter to select desired vehicles:"; std::cin>>vehicle; if(vehicle < 'A' || vehicle > 'G') { bad_inputs++; } if(bad_inputs) { std::cout<<"You entered invalid choice "<<bad_inputs<< " time(s)."; } }while (bad_inputs < 5 && (vehicle < 'A' || vehicle > 'G')); std::cout << "\n Number of day trips: "; while( !( std::cin >> days ) && bad_inputs < 3 ) // while not succeeded in reading <days> // and <bad_inputs> < 3 times { std::cin.clear(); // clear error flags std::cin.ignore(); // clear input buffer bad_inputs++; } if( days < 0 || days > 31 ) { std::cout << "Invalid day" ; } else if( bad_inputs >= 3 ) { std::cout << "bad inputs: " << bad_inputs << " times"; } else { std::cout << "days: " << days << "\n"; } switch (vehicle) { //If vehicle == 'A'
9th Dec 2021, 10:56 AM
shlee_
shlee_ - avatar
+ 2
Okie Jayakrishna🇮🇳 I edited my code already... I wanted to exceed the limitation of 10 for the user to input again a valid number of days and vehicle
9th Dec 2021, 11:53 AM
shlee_
shlee_ - avatar
+ 2
Jayakrishna🇮🇳 My inputs 1 - choice S - place s - place S - place Q - place W -place A- place t-place y- place IP- place k - place d -days
9th Dec 2021, 11:54 AM
shlee_
shlee_ - avatar
+ 1
boolean valid_choice=false; while( ! valid_choice ) { cin>>choice; // find valid_choice=true, breaks //... //.. //... }
8th Dec 2021, 9:59 AM
Jayakrishna 🇮🇳
+ 1
Jayakrishna🇮🇳 all case should be wrap with a while loop? can you write it for me ?
8th Dec 2021, 10:02 AM
shlee_
shlee_ - avatar
+ 1
Your code is difficult to read.. Also am testing in mobile.. Yes. You can wrap in while loop. . But there if your valid cases are 1 or 2 only then you can do like this : while(true){ cin>>choice; if(choice==1 or choice==2) break; cout<<"please enter valid choice"<<endl; } //...go with another calculations. above loop will ask input until you put 1 or 2
8th Dec 2021, 10:16 AM
Jayakrishna 🇮🇳
+ 1
So what and when you are going wrong? You can do same for any input, like above if choices are 1 or 2. Else You have default case there you can put a value which cause repeat if loop. boolean valid_case=true, valid_char=true; do{ switch(case) { case 1: //.. ... do{ cin>>chr; switch(chr) { case 'a': //.. //.. default: valid_char=false; cout<<"enter valid input char"<<endl; break; }//end of inner swich } while(!valid_char) ; //end of inner do-while case n: // default: valid_choice= false; cout<<"enter valid input case"<<endl; break; } //end of outer swich }while (! valid_choice) ; you can like above... if any difficulty then put details about that specific instance here.. break code into small pieces and check it's working.. After all working fine then combine total code..shlee_
8th Dec 2021, 10:50 AM
Jayakrishna 🇮🇳
+ 1
ohh I see Jayakrishna🇮🇳 I ll try your suggestion
8th Dec 2021, 10:56 AM
shlee_
shlee_ - avatar
+ 1
Jayakrishna🇮🇳 in the line of place selection it exceeds to five it breaks my for loop condition ... How to do fix that example when user inputs invalid in the place it keeps on prompting the user to input another place ... even though it exceeds to the for loop condition which is five selection only is allowed which is not should be anyways I'll try to break it on pieces it doesn't work well enough for combination of total code
8th Dec 2021, 12:53 PM
shlee_
shlee_ - avatar
+ 1
This is a sample which runs for valid, invalid inputs... All calculations of cases I removed to simplify and character limit. You can add now, your calculations... Exceeding for loop may because of you don't have break after case 1; Now check all.. By adding details. .. hope it helps...
8th Dec 2021, 5:34 PM
Jayakrishna 🇮🇳
+ 1
You don't have break statement after case 1. What are the inputs you are trying...?
9th Dec 2021, 9:10 AM
Jayakrishna 🇮🇳
+ 1
Iam seeing it from mobile app. It's difficult to understand code.. That's why I said to break into small pieces.. I tested this code by input 1 a A It's running perfectly with all 10times limit exceeded.. Don't crashed. May you giving in compatible input like a number instead of string.. What is your input sample..?
9th Dec 2021, 10:39 AM
Jayakrishna 🇮🇳
+ 1
while ( !(std::cin>>days) && ....) Here it reads days but don't returns a value so it is true for any value other than zero but ! (not) makes to false for all input except 0 there.. Try while( bad_inputs<3) { std::cin>>days; ... ... edit: shlee_ post your input sample and exact error like : I tried 1 a A
9th Dec 2021, 11:44 AM
Jayakrishna 🇮🇳
+ 1
For input : 1 S s S Q W A t y IP k 1 D It's working as all inputs are invalid for choice Limit exceeding 10 times invalid Is it wrong for your expected output? No value is between 'a' to 'be What output you expecting there? Do you know about functions ? How to use it? For better and not to confuse.. Use function like : input : switch(input) { case 1: Case1() ; break; case 2: Case2() ; } void Case1() { ... } void Case2() { ... } Now you can find easily where it going wrong... Otherwise testing 350+ lines of code in mobile is 😇😇😫 not understandable... edit: shlee_ sry in the above, while ( ! (std::cin>>days) && ... if days input is other than zero makes it to true but not(!) makes false.. so except for 0, for any value it is false so loop stops..
9th Dec 2021, 12:36 PM
Jayakrishna 🇮🇳
0
TO MAY VAR... I'M NOT TO MUCH PRO...😁
8th Dec 2021, 6:23 PM
RTB
RTB - avatar