Thread: Loop that wont keep going!

  1. #1
    Registered User pyrotherm's Avatar
    Join Date
    Sep 2003
    Posts
    14

    Unhappy Loop that wont keep going!

    Code:
    #include <iostream.h>
    int main()
    
    {
    
    float ag;
    float mt;
    int redo_calc;
    
    do{
    
    redo_calc=1;
    
    cout<<"Enter the amount of gas used:"
    <<endl;
    
    cin>>ag;//ag=amount of gas
    
    cout<<"Gallons of gasoline is: "<<ag
    <<endl;
    
    cout<<"Enter miles traveled:"
    <<endl;
    
    cin>>mt;//mt=miles traveled
    
    cout<<"Miles traveled is: "<<mt
    <<endl;
    
    cout<<"________________"
    <<endl;
    
    
    cout<<"Miles Per Gallon is: "<<ag/mt
    <<endl;
    
    cout<<"Enter 1 to do another calculation, or 0 to stop program"
    <<endl;
    
    cin>>redo_calc;
    break;
    }while (redo_calc>0);
    
    	return 0;
    }
    my problem is that i want it to be so that when you insert anything but 1 the process is terminated, but if you insert 1 it keeps going!
    <br>
    when i press 1 it still stops!

  2. #2
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    1. Indent your code.
    2. You break the loop before it can continue.

  3. #3
    Registered User pyrotherm's Avatar
    Join Date
    Sep 2003
    Posts
    14
    i do indent my code, usually but am new to this and have a very good beginner's compiler, codewarrior pro 4, gives a lot of slack in the area of indenting, but need to know how to fix that code

  4. #4
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    i do indent my code, usually but am new to this and have a very good beginner's compiler, codewarrior pro 4, gives a lot of slack in the area of indenting, but need to know how to fix that code
    It was just some advice.
    For your code, I said it, you break the loop before it can continue.

  5. #5
    Registered User pyrotherm's Avatar
    Join Date
    Sep 2003
    Posts
    14
    i'm sorry but what do you mean break before continue, my code will not continue no mattwer where i put that break!, do i need another variable or what, how can i fix this code, i am new and in need of help

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Well since this loop isn't a loop that goes on forever just get rid of the break. What you are saying with that break there is that after the user has typed 1 or 0 it will break no matter what. or if you want to have a break you need to set up a condition ( using if or switch) but since you have a condition for the loop already another condition would be useless.

  7. #7
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    remove the break from the program entirely
    also your out put says enter 1 to continue where any non 0 input will cause the loop to continue you may want to change the output to reflect this.

    opps lol someone beat me to it

  8. #8
    Registered User pyrotherm's Avatar
    Join Date
    Sep 2003
    Posts
    14
    thank you very much, the code werks now!
    thanks for not treating me like an idiot!
    if you ever need help with fixing your comp feel free to e-mail me at [email protected]
    I am A + certified to repair comps

  9. #9
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589

    code advice

    use a test condition like
    Code:
    while(redo_calc!=0)
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  10. #10
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    my code will not continue no mattwer where i put that break!
    What you are saying with that break there is that after the user has typed 1 or 0 it will break no matter what. or if you want to have a break you need to set up a condition ( using if or switch) but since you have a condition for the loop already another condition would be useless.
    "Break: Break is what you use to break. For more information, see break."

    In other words, "break;" means "Quit this loop right now". It comes in handy sometimes, but if you just put a "break" without an "if(condition)", you might as well just not use a loop
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

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-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM