Thread: Newbie Loop Question.

  1. #1
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079

    Newbie Loop Question.

    I'm brand new to C++ programming, this shouldn't be too difficult of a question for a real programmer. I made a calculator program and I'm not understanding the loop I managed to put in.

    See... what it does it is perform all of the operations then loop back to the beginning with a "while" command. I specifically did this:

    int x;
    while(1)
    { <THE FOLLOWING IS ALL THE OPERATIONS>
    }

    See how it simply ends with a curly bracket? What I want to do is make my final command, which is:

    cout << "1: Try Again \n";
    cout << "2: Exit \n";
    cout << ">";

    ...the reply "Try Again" I want to make start the loop again, while the command "Exit" I want to close the program. Unfortunatly, since the "cout" is in the loop brackets, the "cin" and all of the "If" commands have to be in the loop bracket as well, so the "Exit" command just makes it loop still. How do I make this work?

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    118
    I dont really get how you get the imput, but you could do something like this:
    Code:
    if (exit)
          break;
    Why drink and drive when you can smoke and fly?

  3. #3
    Registered User eth0's Avatar
    Join Date
    Dec 2003
    Posts
    164
    do
    { <THE FOLLOWING IS ALL THE OPERATIONS>
    } while (x != 2);

    or

    while(1)
    { <THE FOLLOWING IS ALL THE OPERATIONS>

    if (x == 2)
    break;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple C question: user input to repeat a loop
    By evernaut in forum C Programming
    Replies: 2
    Last Post: 11-18-2006, 09:23 AM
  2. for loop question
    By Stack Overflow in forum C Programming
    Replies: 7
    Last Post: 06-09-2004, 11:23 PM
  3. Question... do/while loop
    By Akaii in forum C Programming
    Replies: 2
    Last Post: 08-10-2003, 04:52 AM
  4. newbie question: the for loop
    By Panopticon in forum C++ Programming
    Replies: 2
    Last Post: 01-10-2003, 03:25 AM
  5. Newbie question
    By uNreal in forum C++ Programming
    Replies: 3
    Last Post: 11-18-2002, 01:28 PM