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
but it doesn't. Please help
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;
}
// 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;
}
//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) <<time << setw(15) << distance << endl;
}
You don't modify your distance variable, you are just setting it and then sending it out.
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.
#include <iostream>
#include <iomanip>
usingnamespace 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) <<time << setw(15) << mph * time << endl;
}
system("pause");
return 0;
}