问名字,如果错了再问一次,如果对了,继续(不工作)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),我会得到正确名字的按摩
如果我输入了错误的名字,我得到了错误的名字"欢迎朋友=]"。
的按摩,很好,然后我被要求再次输入名称,我输入了一个白名单名称,它一直说这是错误的名字,错误的名字也显示错误的名字消息。
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
并且您的循环将停止。
要了解有关continue
和break
声明的更多信息,请阅读此和此内容。
试试这个:
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;
继续语句再次通过循环,您要做的是退出它。让我知道它是否有效,因为我现在无法测试。
- 如果没有malloc,链表实现将失败
- 如果我只是不访问queue_front节点的子节点,而是将它们推到队列中呢?还是BFS吗
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 设计一个只能由特定类实例化的类(如果可能的话,通过make_unique)
- 线程,如果else语句,都是错误的上下文切换后,会发生什么
- 如果编译的源代码是特定于它编译的硬件的,我们如何分发它
- 如果我std::dynamic_pointer_cast并且底层dynamic_cast的结果为null,那么返回的sh
- C++擦除(如果存在)
- 如果"new int"返回"int*",那么为什么"new int[n]"不返回"int**"?
- 在'如果'陈述输出如何正确
- 如果用户输入无效,如何使用字符串变量-C++重复输入命令
- 如果有一个模板构造函数只有一个泛型参数,为什么我必须有一个复制构造函数
- 如果基类包含双指针成员,则派生类的构造函数
- 如果没有数学库,我如何在C++中创建复利公式
- 如果我已经有一个头,那么模板(-t)文件属于哪里
- 如果C++对象的类在另一个boost模块中声明,如何使用boost将指向该对象的指针返回到python
- 如果条件为TRUE(最佳方式?),则在do while循环中后置增量
- 如果 std::vector::clear() 不是静态的,如何在没有实例的情况下调用它?