The following is the complete code. Can you give me a hint as to why the program is reading the "mph" instead of "hours"?? The program should only display the "Distance Traveled" according to how many hours the user inputs, not 40 separate calculations. The user inputs 40 mph and 3 hours. I just need the first 3 hours.
This is the chart I get:
Hours Distance Traveled
----------------------------------
1 40
2 80
3 120
.
.
.
.
.
.
40 1600

This is my code:

int main()
{
system("clear")

double distance, mph, hours;

//Get the data
cout << " What is the speed of the vehicle in MPH? ";
cin >> mph;
cout << " How many hours has it traveled? ";
cin >> hours;

cout << " Hours\t\tDistance Traveled " << endl;
cout << " ---------------------------------- " << endl;
cout << hours << "\t\t" << distance << endl;

//Input validation
for (hours = 1; hours <= mph; hours++)
{
if (mph < 0 && hours < 0)
{
cout << " Enter a positive number for mph and a value more than 1 for hours. \n";
}
else
cout << " " << hours << "\t\t" << (hours * 40) << endl;
}
return 0;
}

Maybe you should try the following code:

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
system("cls");

double distance, mph, hours;

//Get the data
cout << " What is the speed of the vehicle in MPH? ";
cin >> mph;
cout << " How many hours has it traveled? ";
cin >> hours;

cout << endl;

cout << " Hours\t\tDistance Traveled " << endl;
cout << " ---------------------------------- " << endl << endl;

//Input validation
for (int i = 1; i <= hours; i++)
{
if (mph < 0 && hours < 0)
{
cout << " Enter a positive number for mph and a value more than 1 for hours. \n";
}
else
cout << " " << i << "\t\t" << (i * mph) << endl;
}
return 0;
}

Hope this helps...

Hope this helps...

Yeah, it will surely help him. He will copy-paste the code and will forget where he committed mistake.:angry:
Did you tried explaining him where he went wrong
I know no one gets paid to help here. I even know that you can debug the flawed code very quickly and repost it. But the real pain lies in explaining the things to the new-bie so that he can get meaning to your code.

So now regarding the code tyoung4@runner
look at for (hours = 1; hours <= mph; hours++) . You already had some value in hours which you took from user, right? And look what you did, you made the hours=1 which mean that the previous value has been lost!. So, rather use what tux4life did.

Moreover, I dont think the second statement of

cout << " ---------------------------------- " << endl;
cout << hours << "\t\t" << distance << endl;

means anything.

Also, dont use system("clear") . Why? Because using system() makes your code unportable. For eg: On windows you use system("cls") while on Linux, you will have to use system("clear"); So take it from me, don't use system()

commented: You made a good point here! +1

The code helped me to see where my code was incorrect. But, I can't use system ("cls") - it doesn't work for Unix. Thanks for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.