Hello everyone.

I am having a hard time getting this program to run w/o an infinite loop. I have been in my C++ class for a month. I think my test expression is incorrect, but I've tried what I think it should be.

Problem:
Write a program that asks the user for the speed and hours traveled. The program should use a loop to display the distance for each hour of that time period. Here's my code:

double distance, mph, hours;

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

for (hours =1; hours = 0; hours += mph)

distance = mph * hours;

cout << " (display the table: 1 40
2 80
3 120)
cout << hours << "\t\t" << distance << endl;

Hello everyone.

I am having a hard time getting this program to run w/o an infinite loop. I have been in my C++ class for a month. I think my test expression is incorrect, but I've tried what I think it should be.

Problem:
Write a program that asks the user for the speed and hours traveled. The program should use a loop to display the distance for each hour of that time period. Here's my code:

double distance, mph, hours;

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

for (hours =1; hours = 0; hours += mph)

distance = mph * hours;

cout << " (display the table: 1 40
2 80
3 120)
cout << hours << "\t\t" << distance << endl;

Well, there's obviously more to the code than that. That won't compile. Here is a problem:

for (hours =1; hours = 0; hours += mph)

One, don't confuse = with ==. I imagine you want == in the middle term. Two, even if you change it to ==, do you really want to compare hours to 0?
Three, do you want to increment hours by mph ?

Use code tags and post the actual code so we know what it really is. That won't give you an infinite loop because it won't even compile.

[code]

// paste code here

[/code]

It did compile, but it ran the same answer repeatedly. I'm working on it.
Thanks. :)

It did compile, but it ran the same answer repeatedly. I'm working on it.
Thanks. :)

This compiled?

cout << " (display the table: 1 40
2 80
3 120)
cout << hours << "\t\t" << distance << endl;

Oh No! I'm sorry..I was in a hurry to get back to my problem, so I just thought that you would be able to understand that was what the table should look like, w/o me actually writing it out. Sorry, I'll be thorough next time.

I fell asleep last night at the desk, and this morning I woke up sick...so I'm still working on it.

Have a good day. BTW, You're great!

Well, there's obviously more to the code than that. That won't compile. Here is a problem:

for (hours =1; hours = 0; hours += mph)

One, don't confuse = with ==. I imagine you want == in the middle term. Two, even if you change it to ==, do you really want to compare hours to 0?
Three, do you want to increment hours by mph ?

Use code tags and post the actual code so we know what it really is. That won't give you an infinite loop because it won't even compile.


OK, so 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.

[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;

//Display the heading 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; }

[/code]

My message is too short?

OK, so 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.

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;

//Display the heading
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;
}

If you're still asking questions, why did you mark the thread "solved"?

This bit

//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;
}

You comment it as input validation, which, in part, it does. But input validation does not belong inside what looks to be your processing loop.

The loop itself - why are you comparing hours to mph as the loop test? That makes no sense. The variable hours is the input from the user, you should not use that as the loop variable. It should be used as the limit on the loop variable. So, your processing loop should be something like for( i = 1; i <= hours; i++ ) Why do you multiply hours (or i ) by 40? Shouldn't that be multiplying time traveled by mph?

If I change the test expression to anything else, I get an infinite loop. I have changed the code to this:

//Calculate the result
for (hours = 1; hours <= mph; hours++)
cout << " " << hours << "\t\t" << (hours * mph) << endl;
return 0;

I get the correct calculations, but just too many hours..

Still, comparing hours and mph as the loop test is nonsensical.

And changing the hours variable, which is the user's input of total time of travel, changes the result.

Work it out by hand to see what you should get.
for hours of 4, mph of 20
Hour Distance Traveled
1 20
2 40
3 60
4 80

Using your code, the results are
1 20
2 40
3 60
4 80
5 100
6 120
7 140
8 160
9 180
10 200
11 220
12 240
13 260
14 280
15 300
16 320
17 340
18 360
19 380
20 400

Does that look at all right?

In your 'for' loop you're using the variable 'hours' as the control variable. ..try something similar to this ..

for (int i = 1; i <= hours; i++)

and like 'vamnes' said you need to look at your method of calculation

Thanks! This really helped. It's soo much easier to understand once you see the code, then figure out the logic. ;)

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.