problem with bool -- can't exit loop
Sep 8, 2011 at 7:42pm UTC
In the following program I want the program to stop iterating loop when the user chooses choice 6 (line 52). If the user chooses choice 6 then the bool variable "more" should be set equal to false and condition in the while argument at line 41 should evaluate to false and exit the while loop but it does not. It seems like no matter what the user enters at line 52, the program never sets the variable "more" equal to false at line 92. Why is this?
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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
#include <iostream>
using namespace std;
class Odometer
{
public :
Odometer();//default constructor
void disp() const ;
void reset();//resets distance to zero
void set_eff(double efficiency);//set efficiency
void add_miles(double dist_traveled);//adds miles to odometer
double fuel_consumed(double distance) const ;//returns gallons of fuel consumed
double get_dist() const ;
double get_eff() const ;
private :
double dist;//distance covered measured in miles
double eff;//fuel efficiency measured in mpg
};
int main()
{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
char loop = 'y' ;
do
{
bool more = true ;
Odometer test;
cout << "Set the efficiency in mpg: " ;
double mpg;
double miles_traveled;
cin >> mpg;
test.set_eff(mpg);
while (more)
{
cout << "1) Reset odometer\n"
<< "2) Set fuel efficiency\n"
<< "3) Add miles\n"
<< "4) Gallons of fuel consumed\n"
<< "5) Display milage and efficiency\n"
<< "6) Exit\n\n" ;
short choice;
cin >> choice;
while ((choice < 1) || (choice > 5))
{
cout << "1) Reset odometer\n"
<< "2) Set fuel efficiency\n"
<< "3) Add miles\n"
<< "4) Gallons of fuel consumed\n"
<< "5) Display milage and efficiency\n"
<< "6) Exit\n\n" ;
cin >> choice;
}
if (choice == 1)
{
test.reset();
}
else if (choice == 2)
{
cout << "Enter efficiency in mpg: " ;
cin >> mpg;
test.set_eff(mpg);
test.disp();
}
else if (choice == 3)
{
cout << "Enter miles traveled: " ;
cin >> miles_traveled;
test.add_miles(miles_traveled);
test.disp();
}
else if (choice == 4)
{
cout << "The numeber of gallons of fuel consumed: " << test.fuel_consumed(test.get_dist()) << endl;
}
else if (choice == 5)
{
test.disp();
}
else //choice == 6
{
more = false ;
}
if (!more)
{
cout << "test" ;
}
}
cout << "Would you like to run the program again? (y/n)? " ;
cin >> loop;
loop = tolower(loop);
while (loop != 'y' && loop != 'n' )
{
cout << "Incorrect output. Would you like to run the program again? " ;
cin >> loop;
}
}while (loop == 'y' );
cout << "Press a key to exit: " ;
char exit;
cin >> exit;
return 0;
}
Odometer::Odometer() : dist(0), eff(0)
{
//default constructor
}
void Odometer::reset()
{
dist = 0;
}
void Odometer::set_eff(double efficiency)
{
eff = efficiency;
}
void Odometer::add_miles(double dist_traveled)
{
dist += dist_traveled;
}
double Odometer::fuel_consumed(double distance) const
{
return dist/eff;
}
double Odometer::get_dist() const
{
return dist;
}
double Odometer::get_eff() const
{
return eff;
}
void Odometer::disp() const
{
cout << "The number of miles traveled: " << get_dist() << endl;
cout << "The efficiency in mpg: " << get_eff() << endl;
}
Sep 8, 2011 at 7:46pm UTC
Nevermind, I found it. Line 53 is wrong.
Sep 8, 2011 at 7:53pm UTC
Or you could use break to stop the loop. Instead more=false ;
write break ;
.
Last edited on Sep 8, 2011 at 7:54pm UTC
Topic archived. No new replies allowed.