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:
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:
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!