Help


If you follow too close behind another car and that other car stops suddenly, you will not be able to stop in time, and you will crash into the rear of that other car. The following formula gives the distance between you and the car in front which allows you to stop safely:

stoppingDistance = speed * (2.25 + speed / 21)

a) Write a program which determines whether a vehicle’s tailgating distance is safe. The program should read in the speed of the vehicle and the vehicle’s tailgating distance. The program should then calculate the distance that would be required to stop the vehicle.

If the stopping distance is less than the tailgating distance, your program should print “No problem.” If the stopping distance equals the tailgating distance, your program should print “Minor wreck.” If the stopping distance is greater than the tailgating distance, your program should print “Major wreck!”

Notice that the expression on the right side of the formula above will force the compiler to convert everything to type double. Use type double for your working variables.

Write your program so that it is able to produce the following screen display:

Sample session:

Enter your speed (in mph): 60.0
Enter your tailgate distance (in feet): 240.0
Major wreck!

b) Notice that one of the tests is a comparison for equality. Since the probability of two real numbers being exactly equal is zero, if you use floating point numbers there will never be an input that produces a minor wreck. Modify the program so that it allows a range of uncertainty of plus or minus 20 feet, within which the result will be a minor wreck. To facilitate adjustment of this range of uncertainty, use a named constant, that is:

RANGE = 40.0

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
64
65
66
67
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int option;
double spead, distance, stoppingDistance;
cout << "Enter 1 for mph/feet and 2 for kmph/m" << endl;
cin >> option;


switch(option)
{
case 1:


cout << "Enter your spead (in mph)" << endl;
cin >> spead;
cout << "Enter your tailgate distance (in feet)" << endl;
cin >> distance;

stoppingDistance = spead * ( 2.25 + spead / 21 );
if (stoppingDistance < distance)
{
cout << "No problem" << endl;
}
else if (stoppingDistance == distance)
{
cout << "Minor wreck !" << endl;
}
else
{
cout << "Major wreck !" << endl;
}

break;

case 2:

cout << "Enter your spead (in kmph)" << endl;
cin >> spead;
spead = spead / 1000;
cout << "Enter your tailgate distance (in m)" << endl;
cin >> distance;
distance = distance * 3.28;

stoppingDistance = spead * ( 2.25 + spead / 21 );
if (stoppingDistance < distance)
{
cout << "No problem" << endl;
}
else if (stoppingDistance == distance)
{
cout << "Minor wreck !" << endl;
}
else
{
cout << "Major wreck !" << endl;
}

default: cout<<" "<<endl;
}

system("PAUSE");
return 0;
}
What's your question?

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.


Topic archived. No new replies allowed.