问名字,如果错了再问,如果正确,继续(不工作)C++

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

这是我的代码。我在这里尝试做的是一个代码,如果名称错误,则询问名称(如通行证),然后程序会说出这 3 条错误消息并再次询问名称,直到 2 个白名单名称之一已给出,然后继续使用代码。

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;

}

我对这段代码的测试给了我这个:
如果我输入白名单名称(indio 或 ighor),我会得到正确名称的消息

"Welcome friend =]".

如果我输入了错误的名字,我得到了错误名字的消息,很好,然后我被要求再次输入名字,我输入了白名单中的一个名字,它一直说这是错误的名字,错误的名字也会显示错误的名字信息。

请正确缩进您的代码。 尝试替换

continue;

来自

break;

continue 语句再次循环,你想做的是退出它。让我知道它是否有效,因为我现在无法测试。

do-while 循环中的逻辑有缺陷。而不是

            continue;

你需要

            break;

continue; 继续循环的下一次迭代。 break; 打破循环。

另外,while语句逻辑不正确。您需要使用:

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

话虽如此,您只需要其中一项检查,不需要两项。

您可以使用:

do  
{
   ...

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

} while (true);

do  
{
   ...

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

试试这个:

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;
}

它更简短、更简洁、更具表现力,而且也很有效..

In your code:

你的条件是 while (Name != "ighor" || Name != "indio"); 这意味着如果这两个条件中的任何一个是 true 那么循环将继续它是 work.If 你的输入是 "indio" 那么第一个条件变成了 true.

In my code:

while (Name != "ighor" && Name != "indio"); 这意味着这两个条件必须是 true 如果其中之一变为 false 那么循环将停止它的工作。现在,如果您的输入是 "indio",则此条件变为 false,您的循环将停止。

要了解有关 continuebreak 语句的更多信息,请阅读 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;
    }

感谢我更改的所有答案

continue;

`break;`

而且效果很好 =) 但是由于代码太混乱了,我可能会研究一下你们给我的其他技巧,这样我就可以让它更干净 =D 我对这段代码还有一个疑问

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

我在这里想要的是 "check" 答案,如果我是 "Sim" 或 "Vou" 就做点什么(意思是 "Yes" 和 "i will",但无论如何) 我想我可以使用你们告诉我的关于 "II" 替换“&&”的内容,这样它就可以正确地满足条件。 如果有一种方法或命令,那么它不会仅仅为了这个答案而区分大写字母和小写字母,所以我不需要把这两种方式都写出来,这个人可以写也会有所帮助。 那只是一个学习代码,仅供学习 propose

第一次发帖,真的很惊讶,这么多回复,速度也太快了,谢谢大家的支持。 你们统治。 写得不错。