Question about while loops
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Question about while loops

  1. #1
    Join Date
    Sep 2011
    Posts
    0

    Question about while loops

    I'm really really new to C++ and i'm just trying to make a start of a text based game. I have 2 while loops and i'm wondering how to get back to the start of the first loop (please enter a name for your character) when the player types 'n' as name_confirm. I see where the problem is but i'm not sure how to make it work.
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
        string char_name;
        char name_confirm = 'n';
        bool name_correct = false;
        cout << "Welcome to <Insert Game Name Here>" << endl;
        while(name_correct == false)
        {
            cout << "Please enter a name for your character: ";
            cin >> char_name;
            while(name_confirm != 'y')
            {
                cout << "You have chosen: " << char_name << ". Is this correct? (y/n)" << endl;
                cin >> name_confirm;
                if(name_confirm == 'y')
                {
                    name_correct = true;
                }
                else if(name_confirm == 'n')
                {
                    name_correct = false;
                }
                else
                {
                    cout << "Please choose either 'y' if the name is correct or 'n' if it is not." << endl;
                    name_correct = false;
                }
            }
        }
        return 0;
    }

  2. #2
    Join Date
    May 2011
    Location
    VietNamess
    Posts
    1

    Re: Question about while loops

    i dont understand why you writen 2 code :

    while(name_confirm != 'y')

    if(name_confirm == 'y')
    {
    name_correct = true;
    }



    i thinks you should repair 2 code beacause if you writen it become meaningless !

  3. #3
    Join Date
    Aug 2009
    Posts
    439

    Re: Question about while loops

    What you can do is to just have one loop and check for the two conditions.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center