-
September 2nd, 2011, 09:53 AM
#1
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;
}
-
September 4th, 2011, 11:50 PM
#2
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 !
-
September 5th, 2011, 01:47 AM
#3
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|