Ask name, if wrong ask again, if right, keep going ( Not working ) C++

2.1k views Asked by At

That's my code. What i'm trying to do here is a code that Asks for a name ( like a pass ) if the name is wrong, then the program says those 3 error messages and asks the name again, until one of the 2 white-listed names is given, then just keep going with the code.

int main(void)
{
setlocale(LC_ALL, "Portuguese");
string Name = "";
cout << "Whats your name dude ?" << endl;
cin >> Name;

if (Name == "ighor" ||
    Name == "indio")
    {
    cout << "Welcome friend =D" << endl;
    system("pause");
    system("cls");
    }
    else
    {
        do  
        {
            cout << "Sorry, wrong name =[" << endl;
            system("pause");
            system("cls");
            cout << "I'll give you another chance =]" << endl;
            system("pause");
            system("cls");
            cout << "Write your name again" << endl;
            cin >> Name;

            if (Name == "ighor" ||
                Name == "indio")
            {
                continue;
            }

        }   while (Name != "ighor" ||
                   Name != "indio");


    }

cout << "Whats up" << Name << endl;
system("pause");
system("cls");      
        return 0;

}

My tests with this code gave me this :
If i put a white-listed name ( indio or ighor ) i get the massage of correct name

"Welcome friend =]".

If i put a wrong name, i get the massage of wrong name, nice, then i'm asked to enter the name again, i put one of the white-listed names and it keeps saying that it's the wrong name, wrong names also show wrong name message.

5

There are 5 answers

1
R Sahu On BEST ANSWER

The logic in the do-while loop is flawed. Instead of

            continue;

you need

            break;

continue; continues with the next iteration of the loop. break; breaks the loop.

Also, the while statement logic incorrect. You need to use:

while (Name != "ighor" &&  Name != "indio");
                      ^^^ not ||

Having said that, you only need one of the checks, not both.

You can use:

do  
{
   ...

   if (Name == "ighor" || Name == "indio")
   {
      break;
   }

} while (true);

or

do  
{
   ...

}  while (Name != "ighor" && Name != "indio");
1
Bruno Cerk On

Thanks for all the answers i did changed

continue;

for

`break;`

and it worked perfect =) But as the code was too confusing i may study a bit the other tips you guys gave me so i can make it cleaner =D I just have another doubt with this piece of code

cin >> RPI;
if (RPI == "Sim"||RPI == "sim"||RPI == "vou"||RPI == "Vou")

What i wanted here was to "check" the answer and make something if i was "Sim" or "Vou" which(means "Yes" and "i will", but anyway) I think that i can use what you guys told me about the "II" replacement for "&&" so it makes the condition correctly. If there was a way or a command, so it don't differentiate Capital and lowercase just for this answer, so didn't i need to put both ways that the person can write would also help. Thats just a learning code, only for studding propose

It was the first time i posted and really surprised, those lot of answers and it was too fast, thanks you all for the support. You guys rule. Good codding.

1
Azo On

Please indent your code correctly. Try replacing

continue;

by

break;

The continue statement goes through the loop again, what you want to do is exit it. Let me know if it works, as I am not able to test right now.

0
Marinos K On

Try this:

int main() {

  string name;
  cout << "Whats your name dude ?" << endl;
  cin >> name;

  while (!(name == "ighor" || name == "indio")) {
    cout << "Sorry, wrong name =[" << endl;
    system("pause");
    system("cls");
    cout << "I'll give you another chance =]" << endl;
    system("pause");
    system("cls");
    cout << "Write your name again" << endl;
    cin >> name;
  }

  cout << "Whats up " << name << endl;
  system("pause");
  system("cls");      
  return 0;
}

it's shorted, cleaner, more expressive and it works too..

1
Prosen Ghosh On

In your code:

Your condition was while (Name != "ighor" || Name != "indio"); which means if any of this two conditions is true then the Loop will continue it's work.If your input is "indio" then the first condition became true.

In my code:

while (Name != "ighor" && Name != "indio");which means this two condition must be true if one of them became false then the loop will stop it's work. Now, if your input is "indio" this condition became false and your loop will stop.

To know more about continue and break statement read this and this.

Try This:

int main(void)
    {
    setlocale(LC_ALL, "Portuguese");
    string Name = "";
    cout << "Whats your name dude ?" << endl;
    cin >> Name;

    if (Name == "ighor" ||
        Name == "indio")
        {
        cout << "Welcome friend =D" << endl;
        system("pause");
        system("cls");
        }
        else
        {
            do
            {
                cout << "Sorry, wrong name =[" << endl;
                system("pause");
                system("cls");
                cout << "I'll give you another chance =]" << endl;
                //system("pause");
                system("cls");
                cout << "Write your name again" << endl;
                cin >> Name;

                if (Name == "ighor" ||
                    Name == "indio")
                {
                    continue;
                }

            }   while (Name != "ighor" &&
                       Name != "indio");


        }

    cout << "Whats up" << Name << endl;
    system("pause");
    system("cls");
            return 0;
    }