0

I have a little bit problem about cin on C++ (Please guide me =w=)

From this code, there are 2 parts, First is the main menu(Function StockSelect) and the second one is submenu(ShowStockMenu). The problem is I just want it to get out from submenu to the main menu when I typed the alphabet, but this code just skips the 'cin' part of the main menu and jump right to the end of the program (exit the program). #include<iostream> #include <fstream> #include<stdlib.h> using namespace std; bool StockSelect(int &stocknum); int ShowStockMenu(); int main() { int stocknum; while(StockSelect(stocknum)) { int inqchoice = ShowStockMenu(); switch(inqchoice) { case 1: //PriceDataInquiry(stocknum); cout<<endl<<"1"<<endl; break; case 2: //GainLossCalculation(stocknum); cout<<endl<<"2"<<endl; break; default: cout<<">>Invalid number... Going back to menu<<"<<endl<<endl; cout<<endl<<"--------------------------------------------"<<endl; break; } } return 0; } bool StockSelect(int &stocknum) { cout<<"Type number (1-5) to select stock"<<endl; cout<<" 1. A"<<endl; cout<<" 2. B"<<endl; cout<<" 3. C"<<endl; cout<<" 4. D"<<endl; cout<<" 5. E"<<endl<<endl; cout<<"Stock number : "; double NoDeci;//to remove the case that has decimal cin>>NoDeci; cout<<endl; if(NoDeci==1 || NoDeci==2 || NoDeci==3 || NoDeci==4 || NoDeci==5) { stocknum = NoDeci; return true; } cout<<endl<<"Invalid Number!...Quiting"; exit(EXIT_FAILURE); } int ShowStockMenu() { cout<<"----------------------------"<<endl; cout<<"Type number 1 or 2 to select"<<endl; cout<<" 1 : PriceDataInquiry"<<endl; cout<<" 2 : GainLossCalculation"<<endl<<endl; cout<<"Enter the number : "; double NoDeci; cin>>NoDeci; cout<<endl; if(NoDeci==1 || NoDeci==2) { return NoDeci; } return 0; }

7th Oct 2019, 3:48 PM
Yourint
Yourint - avatar
4 odpowiedzi
+ 1
Alpha5and5 Thank you so much •w•
12th Nov 2019, 8:42 AM
Yourint
Yourint - avatar
0
Alpha5and5 I already tried to fix it by using cin.clear and cin.ignore(numeric_limits<streamsize>::max(),'\n') but I'm not sure whether it is technically right or not(._.) and here is the link (not the edited one) https://code.sololearn.com/cCTyvdChVj2K/#cpp
11th Nov 2019, 10:47 AM
Yourint
Yourint - avatar