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
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;
}

// Display report header.
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.

1
2
3
4
5
6
7
//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.
i don't understand what you are saying.
Trace your distance variable

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:

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
#include <iostream>
#include <iomanip>

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) <<time << setw(15) << mph * time << endl;
	}

	system("pause");
	return 0;
}
Last edited on
Thank you so much that fixed my problem.
Topic archived. No new replies allowed.