C++ Race Car Homework Problem

Good Afternoon All,

I'm working on a homework problem for my intro to C++ class in college and I've hit a road block nearing the end of my code. The assignment is as follows:

*begin instructions*

You will be designing a race car simulator. The race car will drive across a straight track that is 100 meters long. The race car will start with a speed of 0 m/s. Your program will receive three different inputs that affect the speed of the car.

Move. This input tells your simulator to move the race car for x second at the current speed. Where x is a numerical value directly following the string Move.

Accelerate. This input increases the current speed according to the formula: newspeed = currentspeed+y where y is a numerical value directly following the string Accelerate

Decelerate. This input decreases the current speed according to the formula: newspeed = currentpseed-z where z is a numerical value directly following the string Decelerate

*end instructions*

I am supposed to be inputting each command along with an acceleration or deceleration value or a value of time to move as seen in the following format example:

Accelerate
2
Move
5
Accelerate
1
Move
1
Decelerate
3
Accelerate
10
Move
20

I then output the number of seconds it took to get down the 100 meter track.

The trouble I'm having is that I'm not sure how to get my value of seconds to be the number of seconds it took to get to the 100 meter mark instead of the number of seconds the program is telling the car to drive. My program so far is as follows:

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <string>
#include <cctype>

using namespace std;


int main(){

string command;
int distance = 0;
int speed = 0;
int seconds = 0;
int accel = 0;
int decel = 0;
int move = 0;

while (distance < 100){

cin >> command;

if (command == "Accelerate"){
cin >> accel;
speed += accel;
}
else if (command == "Decelerate"){
cin >> decel;
speed -= decel;
if (speed < 0){
speed = 0;
}
}
else if (command == "Move"){
cin >> move;
speed = speed;
seconds += move;
distance = speed * seconds;

}
}
cout << "It took " << seconds << " seconds to finish the race.";

return 0;
}



Any help to work this seemingly simple problem out is much appreciated.
Thanks!
Use code tags like this:



[code]

//Your Code Here

[/code]



You have a line in there that says "speed = speed;" which is pretty useless.

Instead of doing "distance += speed * seconds" you can instead loop through all the seconds like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//Declare These Up With The Other Variables
int total = 0;
bool end = false;


for (int i = 0; i < seconds; i++)
{
	distance = speed;

        //Set "total" to the seconds needed to reach 100meters
	if (distance >= 100 && !end)
	{
		total = seconds;
		end = true;
	}
}
Thanks, that's pretty much what I was attempting to do but I wasn't sure what to put in my if statement. The line where I wrote "speed = speed" was more of a mental reminder for me than to actually have a use, I probably should've taken it out.
Thanks again!
Topic archived. No new replies allowed.