### Student Programmer needs help/

The whole for loop is messed up and I don't know how to fix it. It is suppose to display like this:
Hours-------------Distance Traveled
-------------------------------------
1---------------------75
2---------------------150
3---------------------225
int main()
{

int distance, time = 1, mph, hrs;

cout << " Welcome to your mile manager." << endl;
cout << " Please enter the speed at which you traveled (in MPH): ";
cin >> mph;

//Validating user input.
if (mph < 1)
{cout << "You could not have traveled negative mile. Try again." << endl;
cin >>mph;
}
cout << "How long have you been traveleing?: " ;
cin >>hrs;

//Input validation.
if (hrs < 1)
{cout << "You could not have travel less than 1 hour to get here. Try again.";
cin >> hrs;
}

cout << "Hour" << " Distance Traveled" << endl;
cout << "--------------------------" << endl;

// Equation.
distance = mph * hrs;

// For loop that iterates until it reaches the number the user
// put in and display results.
for (time, mph; time <= hrs, mph <= distance; time++, mph += mph)
{cout << setw(4) <<time << setw(15) << distance << endl;
}

system("pause");
return 0;
}
Last edited on
First use a `while` loop for your data validation.

As far as outputting your `distance`.

 ``1234567`` ``````//You set distance here in your code distance = mph * hrs; //You cout distance here in your code for (time, mph; time <= hrs, mph <= distance; time++, mph += mph) {cout << setw(4) <

You don't modify your distance variable, you are just setting it and then sending it out.
i don't understand what you are saying.

If `hrs = 2` and `mph = 50`

`distance` = 2 * 50
`distance` = 100

enter `for` loop

`distance` will = 100 the for the entire duration of the loop. Nothing you are doing is acting on your distance variable other then `distance = mph * hrs` which is outside your loop. Your cout will not look like you are expecting with the code you have written.

Last edited on
nothing I am trying is working any suggestions on how you would write it?
Last edited on
You need to work your equation for every cycle of the for loop. try this:

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344`` ``````#include #include using namespace std; int main() { int distance, time = 1, mph, hrs; cout << " Welcome to your mile manager." << endl; cout << " Please enter the speed at which you traveled (in MPH): "; cin >> mph; //Validating user input. if (mph < 1) {cout << "You could not have traveled negative mile. Try again." << endl; cin >>mph; } cout << "How long have you been traveleing?: " ; cin >>hrs; //Input validation. if (hrs < 1) {cout << "You could not have travel less than 1 hour to get here. Try again."; cin >> hrs; } // Display report header. cout << "Hour" << " Distance Traveled" << endl; cout << "--------------------------" << endl; // Equation. // For loop that iterates until it reaches the number the user // put in and display results. for (time; time <= hrs; time++) { //use your hours for iterations and fix the equation cout << setw(4) <
Last edited on
Thank you so much that fixed my problem.
Topic archived. No new replies allowed.