for statement not incrementing properly

I know what's going to happen but I'm seeing spots. I've walked away from this and let it set, hoping I would have the solution jump out at me. Unfortunately that isn't the case. So fresh eyes will probably point out what I can't seem to see.

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

/*
This problem will ask user for input of speed and time
then calculate and output distance.Incrementing and displaying each hour and distance traveled during that time.
*/
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
	int speed = 0, distance, time = 1, output1, output2;				//declare variables

			cout <<"How fast will the vehicle be traveling? " << endl; //ask for user input
			cin >> speed;												//user input
			cout << "How many hours will it travel? " << endl;				//ask for user input
			cin >> time;												// user input
			distance = time * speed;		//formula

						for (int i = 0; i<= time; i++)
			{
				output1 = i;
				output2 = output1 * speed;
						}


			cout << "What is the speed (in MPH)? " << "\t\t" << speed << endl;
			cout << "How many hours has it traveled? " << "\t" << time << endl;
			cout << "\n";
			cout << "\n";
			cout << "Hours			Distance Traveled " << endl;
			cout << "_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _" << endl;
			cout << "\n";
			cout << output1 << "\t\t\t" << output2 << endl;
			
					if (time < 1 || speed < 0)	//cannot accept a negative value for speed or any value less than 1 for time travelled
						{
						cout << "Please re-enter your data! One of the values you have chosen is not valid. " //ask user to re-enter input
							<< endl;
						}

	system ("pause");
	return 0;
}
The scope of your for-loop is incorrect. You're iterating over it time - 1 times. Each iteration will over-write both output1 and output2. You have to remember that output1 and output2 can each only store a single value (C++ is not a vector language).

I'd suggest either moving your for loop down to around line 33, or storing all of the offsets in a vector/list populated during your current for-loop, then re-iterating over it and printing the results.

The first suggestion I've offered would look something like: (pseudo code)
1
2
3
4
5
6
for (int i = 0; i < time; ++i)
{
 calculate distance travelled
 print hours travelled: X
 print distance travelled: Y
}



hth.
Thank You Zaita. That is exactly what clues I needed. Here is the completed code let me know if I need to make it more professional (other than commenting).

[code

/*
This problem will ask user for input of speed and time
then calculate and output distance.
*/
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int speed = 0, distance, time = 1, output1, output2; //declare variables

cout <<"How fast will the vehicle be traveling? " << endl; //ask for user input
cin >> speed; //user input
cout << "How many hours will it travel? " << endl; //ask for user input
cin >> time; // user input



cout << "What is the speed (in MPH)? " << "\t\t" << speed << endl;
cout << "How many hours has it traveled? " << "\t" << time << endl;
cout << "\n";
cout << "\n";
cout << "Hours Distance Traveled " << endl;
cout << "_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _" << endl;
cout << "\n";
for (int i = 0; i < time; i++)
{

distance = time * speed;
output1 =i+1;
output2 = output1 * speed;
cout << output1 << "\t\t\t" << output2 << endl;
}


if (time < 1 || speed < 0) //cannot accept a negative value for speed or any value less than 1 for time travelled
{
cout << "Please re-enter your data! One of the values you have chosen is not valid. " //ask user to re-enter input
<< endl;
}

system ("pause");
return 0;
}

][/code]
You need to fix your code tags please. The if (time < 1 || speed < 0) { } should be put at the top right after you ask the user for the information. And should be wrapped within a while loop.

One method you can use to do it is:
1
2
3
4
5
int time = 0;
while (time <= 0) {
 cout << "Please enter time:";
 cin >> time;
}


I also recommend splitting your declaration of variables up onto multiple lines, especially when you're assigning default values.
Last edited on
Topic archived. No new replies allowed.