While Loop problem?

Basically, if I run the program below, the output line is blank..I honestly have no idea what to do, and I need this done. I don't want anyone to do it with me (unless i'm desperate), I just need some pointers, samples, examples, etc.




#include<iostream>
#include <string>
#include <iomanip>

using namespace std ;

int main()

{
char vehicle;
int minutein,
minuteout,
hourin,
hourout;

int hours;
double cost;

cout << "Type of vehicle: C for car, T for truck, B for bus: " << endl;
cin >> vehicle;
cout << "hours in: ";
cin >> hourin;
cout << "minutes in: ";
cin >> minutein;
cout << "hours out: ";
cin >> hourout;
cout << "min out: ";
cin >> minuteout;



hours = hourout - hourin;


while(vehicle == 'C' || vehicle == 'c') {
if(hours <= 3) {
cost = 0;
}
else {

cost = hours * 1.5;
}
}

while(vehicle == 'T' || vehicle == 't') {
if(hours <= 2) {
cost = 2.5; }

else {

cost = (hours * 5) + 2.5;
}
}

while(vehicle == 'B' || vehicle == 'b') {
if(hours <= 1) {
cost = 5.00; }

else {
cost = (hours * 7.5) + 5;
}
}

cout << cost;






system ("pause");
return 0;
}
Hi,

Try stepping through your code in your head, following each executable statement from one to the next.

What do you find happens in the while loops?

Cheers,
JimbO
hi devindinap.......


First of all let us know what u want to do with the program..........and better to use if statement than while loop as for as my understanding.......
The output line is blank because your program is stuck in an infinite loop.

You're not supposed to use a while loop for a single execution. Now, the value of "vehicle" will NEVER change, so once it enters a loop, it will never exit. Replace with if statements (or a switch).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
...
//if vehicle == C it will go inside... but when does the loop finish? 
//(When is vehicle different from 'C' or 'c' ? 
//as the value never changes... it will stay inside the loop forever
while(vehicle == 'C' || vehicle == 'c') {
if(hours <= 3) {
cost = 0;
}
else {

cost = hours * 1.5;
} 
}
...
[/b]

First why are u using a 3 while loops?
As gaminic said you should use a switch or some ifs...

The point is...

Consider that Vehicle value = 'C'
As it never changes inside the while loop it wont go out of the while so it would be an infinite loop.


Last edited on
If you really need to use a while loop for the program...

You should do something like...

1
2
3
4
5
6
7
Loop

  Switch || bunch of ifs else

  Modify type vehicle

exit when vehicle type != than the conditions of the ifs


For example
Topic archived. No new replies allowed.