Can't figure out What's wrong

Hello, I'm having a problem with this program. I have everything correct up to the for loop part where it won't display the numbers correctly. This is the problem and what I've done.

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*The distance a vehicle travels can be calculated as follows:

  distance = speed * time

  For example, if a train 40 miles per hour for 3 hours, the distance traveled is
  120.

  Write a program that asks the user for the speed of a vehicle (in miles per hour) and 
  how many hours it had traveled. The program should then use a loop to display the 
  distance the vehicle has traveled for each hour of that time period. Here is an example
  of the output:

  What is the speed of the vehicle in mph? 40
  How many hours has it traveled? 3
  Hour   Distance Traveled
  ---------------------------------
  1				40
  2				80
  3			   120

  Input Validation: Do not accept a negative number for speed and do not accept any
  value less than 1 for the time traveled.  */
#include <iostream>
using namespace std;

int main()
{
	double distance,
		   speed,
		   time;
		   
	cout << " What is the speed of the vehicle in mph?  ";
	cin >> speed;
	
	while(speed < 0)
	{
		cout << " Please enter a positive number for the speed:  ";
		cin >> speed;
	}
	
	cout << " How many hours has it traveled?   ";
	cin >> time;

	while(time < 1)
	{
		cout << " Please enter a number greater than 1 for the hours:  ";
		cin >> time;
	}

	distance = speed * time;

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

	for(int count = 1; count <= time; count++)
	{
		cout << " " << count << "\t\t" << speed << endl;
		speed *= count;
	}

	return 0;
}


The for loop part is not displaying correctly, for example..if I put 40 for speed and 3 for hours, I will get 40 in hour 1, 40 for hour 2, and 80 for hour 3. Please help!
That is because in the first iteration speed gets printed as it is: 40
In next step it is multiplied with count which is still 1, therefore in second iteration it remains 40. However it is multiplied by count=2 after that so it becomes 80 and is printed in third iteration. Now your loop is finished therefore it does not print 240 which would be the value in next step.

What you need to do is to change the sequence of these two statements, i.e:
speed *= count;
cout << " " << count << "\t\t" << speed << endl;


It will work fine.

For more help: http://recurseit.blogspot.com
Last edited on
I think that the program will not work fine because the variable speed gets another value. It is multiplied by the count variable. To work fine it won't change the value of the variable speed. It will just print it.

Try this one:

for(int count = 1; count <= time; count++)
{
cout << " " << count << "\t\t" << speed*count << endl;

}

The value of the speed variable must to remain constant.
I would like to know the right answer, illojik.
Thank you for the help. CosminNTG was correct because the output was just like how the problem asked it. When I put in 40 for speed and 3 for hours, i got 40 for hour 1, 80 for hour 2, and 120 for hour 3, just like how it was supposed to be.
I tried it the way Undeclared did but got 240 for hour 3 when it should have been 120.
Topic archived. No new replies allowed.