Help with loop

This is a discussion on Help with loop within the C++ Programming forums, part of the General Programming Boards category; How do i get this to loop out distance traveled per hour. Code: #include<iostream.h> #include<math.h> #include<iomanip.h> void main (void) { ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    17

    Help with loop

    How do i get this to loop out distance traveled per hour.
    Code:
    #include<iostream.h>
    #include<math.h>
    #include<iomanip.h>
    
    
    
    void main (void)
    
    {
    	float speed;
    	float time;
    	float distance;
    
    
    	cout <<"Enter the speed of the train in miles per hour: "<<endl;
    	cin >>speed;
    		if (speed <0)
    		{	cout <<"Please enter a positive number for speed"<<endl;
    			cin >>speed;
    		}
    	cout <<"Enter the hours the train has traveled: "<<endl;
    	cin >>time;
    		if (time <1)
    		{	cout <<"Please enter a time greater than zero: "<<endl;
    			cin >>time;
    		}
    		
    		distance = time * speed;
    
    		if speed >0
    		cout <<distance;
    
     loop?????
    
    }

    Code tags added by Hammer

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    This is not Java. Do not use 'void main'. We will all hunt you down and flame you to ash if you do.

    You have three loops:
    Code:
    for( initialization = something; test_something; increment_something )
    
    /*or*/
    
    while( something_is_somevalue )
    
    /*or*/
    
    do
       something
    while( something_is_somevalue );
    Pick one and come back when you're stuck again.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    17

    Loop

    I'm not sure if i folllow
    the user is to input the speed(mph) of a train and the hours traveled.
    then i need to use a loop to show the distance the train has traveled for each hour.

    so if the user entered 40 mph for 3 hours, using the formula distance =time * speed, is 120 miles
    so my output would be:

    1 - 40 miles
    2 - 80 miles
    3 - 120 miles

    i can't figure a way to have the loop output each hour

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    for (int i = 0; i < NumOfHours; i++)
    {
        /* Do something for each hour here, maybe cout something? */
    }
    maybe?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    How about a loop like this

    Code:
    int x;
    
    for (x=0;x<time;x++)
         cout << "Distance travelled at hour: " <<x<<"\t"<< (speed*x);
    Now of course that will give you one less result than you require, but I'm sure you can fix that. And your total distance calculation becomes a bit redundant. If you were going to make the program a bit more versatile, then you'd need to change the structure slightly.

    [edit]Dammit, beaten by a Hammer[/edit]
    Last edited by Azuth; 10-23-2002 at 07:40 PM.
    Demonographic rhinology is not the only possible outcome, but why take the chance

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 12:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-19-2005, 11:30 PM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 08:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21