Hello, community:
I have a problem an I need some assistance. I have a programming homework assignment that is a simple Distance = Rate * Time, with some conditions the speed of the vehicle must have a parameter between 10 - 120 mph and time cannot hold a negative value or surpass 24 hours.
my code is:
P.S. My compiler is Visual Studio 17 community.
//Distance Traveled
//The distance a vehicle travels can be calculated as follows : “distance = speed * time”.
//the car travels 40 miles per hour for 3 hours, the distance traveled is 120 miles.
//Write a program that asks the user for the speed of a vehicle(in miles per hour) and how many hours it has traveled.
//The program should display the distance the vehicle has traveled for each hour of that time.
//For input validation, Must not accept values x <= 10 or x => 120 for speed and x <= 1 or => 24 for time. Use “int” declarations.*/
/**********************************************************************************************************************************/
//Create By: J L
#include "pch.h"
#include <iostream>
usingnamespace std;
int main()
{
int distance, speed, time; //Line 15 declaring variables and using int.
cout << "How fast is the vechicle traveling please enter positive natural numbers from \"10 - 120\"?\n";
cin >> speed;
while (speed < 10 && speed > 120)
{
cout << "Sorry thats a incorrect values please enter a new value:\n";
cin >> speed;
}
cout << "How long has the vehicle been traveling please enter positive natural numbers from \"1 - 24\"?\n";
cin >> time;
while (time < 1 && time >24)
{
cout << "Sorry thats a incorrect value please enter a new value:\n";
cin >> time;
}
cout << "\n\nThe speed you have entered is: " << speed << " miles-per-hour.\n";
cout << "\nThe time you have entered is: " << time << " hours.\n\n";
cout << "Hours\tDistance Traveled\n";
cout << "-----" << "\t" << "-----------------\n";
for (int i = 1; i<=time; i++)
{
distance = speed * i;
cout << i << "\t\t" << distance << endl;
}
return 0;
}
so my issue is when I use my while statement to set the speed for values of 10 - 120 to loop itself if the value is less than 10 or greater than 120. It doesn't loop as directed. The same thing goes for the Time variable please any assistance will be greatly appreciated. Furthermore, I apologize in advance for posting in the wrong section of the forums for assistance.