stuck in infinite loop, not running other function | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

stuck in infinite loop, not running other function

I don not know why my code, do not run as i have wrote it? please help me by coping this program on your code block and help me to resolve the problem, thanks in advanced. #include <iostream> #include<fstream> #include<string> using namespace std; int getwhattheywant(); void displays(int x); int main() { string name; double power; ofstream objectfile ( "objects.txt") ; cout<< "enter name, power"<<endl; cout<<"enter ctrl+z to quit"<<endl; while(cin>> name>>power){ objectfile<<name<<" "<<power<<endl; } int whattheywant; whattheywant = getwhattheywant(); while(whattheywant!=4){ switch(whattheywant){ case1: displays(1); break; case2: displays(2); break; case3: displays(3); break; } whattheywant = getwhattheywant(); } } int getwhattheywant(){ int choice; cout<< "1-just plain items"<<endl; cout<<"2-helpful"<<endl; cout<<"3-harmful"<<endl; cout<<"4-quit"<<endl; cin>> choice; return choice; } void displays(int x) { ifstream objectfile("objects.txt"); double power; string name; if (x==1){ while(objectfile>>name>>power){ if(power==0){ cout<< name << " "<< power<<endl; } } } if (x==2){ while(objectfile>>name>>power){ if(power>0){ cout<< name << " "<< power<<endl; } } } if (x==3){ while(objectfile>>name>>power){ if(power<0){ cout<< name << " "<< power<<endl; } } } }

27th Nov 2016, 1:25 PM
parisa
parisa - avatar
4 Answers
+ 1
/* Running in code playground shows that you were not checking if the last cin succeeded */ #include <iostream> #include<fstream> #include<string> using namespace std; int getwhattheywant(); void displays(int x); int main() { string name; double power; ofstream objectfile ("objects.txt"); cout << "enter name, power" << endl; cout << "enter ctrl+z to quit" << endl; while(cin >> name >> power) { cout << name << " " << power << endl; objectfile << name << " " << power << endl; } int whattheywant = getwhattheywant(); while(whattheywant!=4) { switch(whattheywant) { case1: displays(1); break; case2: displays(2); break; case3: displays(3); break; } // end switch whattheywant = getwhattheywant(); } // end while } // end main int getwhattheywant() { int choice; cout << "1-just plain items" << endl; cout << "2-helpful" << endl; cout << "3-harmful" << endl; cout << "4-quit" << endl; if (!(cin >> choice)) { cout << "cin failed" << endl; choice = 4; } cout << "choice: " << choice; return choice; } void displays(int x) { ifstream objectfile("objects.txt"); double power; string name; if (x==1) { while(objectfile >> name >> power) { if(power==0) { cout << name << " "<< power << endl; } } } if (x==2) { while(objectfile >> name >> power) { if(power>0) { cout << name << " "<< power << endl; } } } if (x==3) { while(objectfile >> name >> power) { if(power < 0) { cout << name << " " << power << endl; } } } }
27th Nov 2016, 4:53 PM
Jesse Bayliss
Jesse Bayliss - avatar
+ 1
I think it would work, but not in the code playground as it appears to pipe the text input to the program. Perhaps try reading in 1 variable that tells it how many rows of data to read then read the rest of the data after that as your choices.
27th Nov 2016, 5:44 PM
Jesse Bayliss
Jesse Bayliss - avatar
0
thanks a lot dear friend, but it seems now that the part of displays(int x) function does not work. would you help me to solve this part, thanks a lot.
27th Nov 2016, 5:34 PM
parisa
parisa - avatar
0
thanks dear jesse. it must work but in my code block it also warning about unused "switch cases". should i write in my text in another tab? is it important ?
27th Nov 2016, 6:35 PM
parisa
parisa - avatar