loops

Hi I'm having trouble running my program. It has to have a loop and the intervals for the speed is 0-100 and the intervals for the time is 0-12. I hope someone can help me!

[code]
//This is a program that asks the user the speed
// of the vehicle and the distance it has traveled.
// It was written by Michelle Nguyen.
#include <iostream>
using namespace std;

int main ()
{
const int minSpeed = 0, // starting speed of the vehicle
maxSpeed = 100, // max speed of the vehicle
minTime= 0, // starting time
maxTime = 12; //max number of hours the vehicle can travel.
double time, distance, speed;

cout <<"What is the speed of the vehicle? ";
cin >> speed;
while (speed <0)
{
cout << "Please enter a positive number for the speed: ";
cin >> speed;
}

while (speed > 101)
{
cout <<"Please enter a speed less than 101: ";
cin >> speed;
}

while (time <0)
{
cout <<"Please enter a postive number for the time: ";
cin >> time;
}

while (time >13)
{
cout << "Please enter a time less than 13: ";
cin >> time;
}

distance = time*speed;
cout << endl;
cout << "hour" << "\t\t" << "distance traveled"<< endl;
cout << "--------------------------"<<endl;

for (int count = 0; count <=time; count++);
{
cout << hour << "\t\t" << speed*time << endl;
}
return 0;
}
closed account (18hRX9L8)
I'm not sure why you have two loops for the same variable. In your while loop you can test the variable for minimum and maximum values using (in this case) || operator. Read up on operators here: http://www.cplusplus.com/doc/tutorial/operators/#logical-operators.

Ex:
1
2
3
4
while(x<0 || x>13) {
          std::cout << std::endl << "Please enter in a number between 0 and 13: ";
          std::cin >> x;
}


Also, none of your loops are going to run because time and speed are uninitialized. That is why you should use do-while loops. See reference: http://www.cplusplus.com/doc/tutorial/control/#do-while.
Last edited on
Topic archived. No new replies allowed.