Thread: Easy Question

  1. #1
    Bryan
    Guest

    Easy Question

    Hi everyone, im new to programming and am taking it at school this year.
    Heres an easy question but im stuck on what to do.

    I need to know what code to write so that if a person selects "Yes" they are dropped out of a loop but if they select no the loop continues. How would i go about doing this?

    Heres the code im working with

    if (yesno==1);
    {
    cout<< "Thank You";
    }
    if (yesno==2)
    {
    cout<< "Please Enter your info again";

    As i said i need it so if the person picks yes they get dropped out of the loop but if the answer is no the loop continues.

  2. #2
    Hamster without a wheel iain's Avatar
    Join Date
    Aug 2001
    Posts
    1,385
    #define yes 1
    #define no 0

    while (yesNo!=yes)
    { code}

    //loop exit code here
    Monday - what a way to spend a seventh of your life

  3. #3
    Bryan
    Guest
    sorry i didnt explain properly, i need to know the code that will actually exit out of the loop if a person picks yes

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    This isn't the best code but you can work with it.
    Code:
    enum choice {NO, YES};
    
    int main(void) {
        enum choice yes_no;
    
        while(0) {
            yes_no = (getchar() == 'y')?YES:NO;
            if(yes_no == YES)
                 break;
        }
        return 0;
    }

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>while (0)
    That won't work too well
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    You can use:
    Code:
    break;
    or you can use:
    Code:
     exit(0)
    to exit the whole program.

    Note:
    exit(0) : is used to indicate the program was terminated normally.
    exit(1) : is used to indicate the prgoram was terminated due to an error.

  7. #7
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    In Iaian's example, if the answer is yes, the loop automatically exits. At the end of your code in the loop brackets, get input from the user for yesno (1 or 2). If they answer 1 for yes, the loop will exit when the while statement checks the answer.
    Anything besides 1 will keep the loop going. 3, 10, abc, etc. You could put some error checking in for invalid input.
    Truth is a malleable commodity - Dick Cheney

  8. #8
    Registered User GreenCherry's Avatar
    Join Date
    Mar 2002
    Posts
    65
    Code:
    if (yesno==1);
    {
    cout<< "Thank You";
    break;
    }
    if (yesno==2)
    {
    cout<< "Please Enter your info again";
    ...
    This is probably simple enough for you to understand. If you want it to end the program after it says "Thank You" then use return 0.
    I have a rabbit in my pants! Please be happy for me.

  9. #9
    Bryan
    Guest
    thanks ammar, "break;" was exactly what i needed

  10. #10
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Another solution would be to make it a while loop, as iain said.
    PHP Code:
    #define YES true
    #define NO false

    char input;
    bool yesno NO;

    while(
    yesno != YES//so it'll loop until yesno is YES (duh)
    {
       
    cin >> input;
       if(
    input == 'y')  //if the input is yes, then change yesno to YES
          
    yesno YES;
       else  
    //otherwise, threaten them :p
          
    cout << "Sorry, I don't take no for an answer.\n";
    }
    cout << "Thank you!"//Always be polite! 
    Just Google It. √

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

  11. #11
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by iain
    #define yes 1
    #define no 0

    while (yesNo!=yes)
    { code}

    //loop exit code here
    this code fragment will do everything but work
    hello, internet!

  12. #12
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    lol why, because the exit code is outside the loop? So what? I'm out of the loop a lot and I still work
    Just Google It. √

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

  13. #13
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by Hunter2
    lol why, because the exit code is outside the loop? So what? I'm out of the loop a lot and I still work
    heh
    hello, internet!

  14. #14
    Registered User
    Join Date
    Oct 2002
    Posts
    11
    is your life just one big syntax error?
    mine is.
    --Cid666

  15. #15
    Registered User Kirdra's Avatar
    Join Date
    Aug 2002
    Posts
    105
    Maybe this is a bit clearer:

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        int loop = 0;
        
        do
        {
            cout << "Enter a number: ";
            int num;
            cin >> num;
            
            if(num == 1)
            {
            cout << "One!, thats my favorite number.\n";
            loop = 1;        //Break the loop
            }
            
            else
            {
            cout << "Hmm, ok... try again.\n";
            }
            
        } while(loop == 0);
        
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. easy Vector contructor question
    By noodle24 in forum C++ Programming
    Replies: 5
    Last Post: 04-21-2006, 07:43 PM
  2. Another Embarassingly Easy Question
    By almo89 in forum C Programming
    Replies: 2
    Last Post: 02-11-2006, 04:59 PM
  3. This is hopefully an extremely easy question...
    By rachaelvictoria in forum C Programming
    Replies: 2
    Last Post: 11-07-2005, 01:36 AM
  4. 4 easy question
    By Zeratulsdomain in forum C++ Programming
    Replies: 2
    Last Post: 10-15-2005, 10:43 PM
  5. Easy Question
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 08-12-2002, 12:19 PM