Exit the program
Apr 19, 2014 at 4:39pm UTC
Hi Guys!
For the most part my program is working fine, but when i want to exit the program by provide the choice 4, the program should terminate but it does not terminate by debugging I figured it out that the value loopCondition zero is not passing properly to the function... can you please help me where I need to make changes?
Here is my code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int menu(int );
int myChoice(char , int );
int main(){
//char choice;
int loopCondition = 1;
cout << "\n\nTemperature Statistics\n----------------------\n\nReading logged values for processing and presentation...\n\nPress Enter for menu: " ;
cin.get();
menu(loopCondition);
return 0;
}
int menu(int loopCondition){
char choice;
while (loopCondition){
cout<<"\n\nMENU\n----\n\n1. Display temperature values\n2. View maximum and minimum temperatures\n3. View average temperature\n4. Quit\n\nMake your choice: " ;
cin.get(choice);
cin.get();
myChoice(choice, loopCondition);
cout<<"\n\nPress Enter to continue:" ;
cin.get();
}
return 0;
}
int myChoice(char choice, int loopCondition){
if (choice=='1' ){
double t;
cout<<"\nDisplaying the latest 24 temperature values:\n\n" ;
ifstream fil("templog.txt" );
for (int i=0;i<24;i++){
if (i%6==0)
cout<<endl;
fil>>t;
cout<<fixed<<setprecision(2)<<setw(8)<<t;
}
fil.close();
}
else if (choice=='2' ){
cout<<"\nCalculating the maximum and minimum temperature...\n" ;
}
else if (choice=='3' ){
cout<<"\nCalculating average temperature...\n" ;
}
else if (choice=='4' ){
loopCondition = 0;
cout << "\n\nTerminating the program." ;
return loopCondition;
}
}
Thanks in Advance
Sincere Regards
Apr 19, 2014 at 4:44pm UTC
Modify line 24:
1 2
// myChoice(choice, loopCondition);
loopCondition = myChoice(choice, loopCondition); // returns 0 on exit
Last edited on Apr 19, 2014 at 4:44pm UTC
Apr 19, 2014 at 6:35pm UTC
Hi JLBorges
The solution you have suggested exits the program in the second turn of while loop.. by changing the loopCondition to 0???
anyways thanks for your reply
Topic archived. No new replies allowed.