Compiler Differences & Programming Question

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
// This program will determine the distance a vehicle moving at a spceidifc speed will travel.
#include <iostream>
using namespace std;
int main()
{
int Speed, Hours, Distance;
int DisplayNumber, DisplayDistance;

// Ask the user for the speed of a vehicle (in miles per hour).
cout << "Enter the speed at which a vehicle is traveling [in miles per hour] " << endl;
// Do not accept a negative number
cin >> Speed;	// Input
while (Speed <= 0) 
{ 
	cout << "Error: Enter a positive integer." << endl;
	cin >> Speed;
}

// Ask how many hours the vehicle has traveled.
cout << "How many hours has the vehicle traveled? " << endl;
// Do not accept any value less then one.
cin >> Hours;	// Input
while (Hours < 1) 
{ 
	cout << "Error: Do not enter a value less then one." << endl;
	cin >> Hours;
}

// Calculate distance with formula
Distance = Speed * Hours;
cout << "\n";

// The program should then use a loop to display the distance the vehicle has traveled for each hour of that time period.
cout << "Hour		Distance Traveled " << endl;
cout << "--------------------------------- " << endl;
for (DisplayNumber = 1; DisplayNumber <= Hours; DisplayNumber++)
	cout << DisplayNumber << "\t\t" << (DisplayNumber * Speed) << endl;
cout << "\n";

// Output
cout << "The distance traveled is equal to " << Distance << " miles." << endl;

system("pause");
return 0;
}
Last edited on
Topic archived. No new replies allowed.