问名字,如果错了再问一次,如果对了,继续(不工作)C++

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

本文关键字:如果 C++ 工作 继续 错了 一次      更新时间:2023-10-16

这是我的代码。我在这里尝试做的是一个代码,如果名称错误,则要求输入名称(如通行证),然后程序说出这 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),我会得到正确名字的按摩

"欢迎朋友=]"。

如果我输入了错误的名字,我得到了错误的名字

的按摩,很好,然后我被要求再次输入名称,我输入了一个白名单名称,它一直说这是错误的名字,错误的名字也显示错误的名字消息。

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

它很短,更干净,更具表现力,而且也很有效。

在您的代码中:

您的条件while (Name != "ighor" || Name != "indio");这意味着如果这两个条件中的任何一个true那么循环将继续工作。如果您的输入"indio"则第一个条件变为true

在我的代码中:

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

要了解有关continuebreak声明的更多信息,请阅读此和此内容。

试试这个:

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")

我在这里想要的是"检查"答案,如果我是"Sim"或"Vou"(意思是"是"和"我会",但无论如何)我认为我可以使用你们告诉我的关于"&&"的"II"替换的内容,以便正确设置条件。如果有办法或命令,所以它不会为了这个答案而区分大写和小写,所以我不需要把人可以写的两种方式也有帮助。那只是一个学习代码,仅用于镶嵌建议

这是我第一次发帖,真的很惊讶,这么多答案,而且太快了,谢谢大家的支持。你们统治。好溺爱。

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

continue;

break;

继续语句再次通过循环,您要做的是退出它。让我知道它是否有效,因为我现在无法测试。

相关文章: