密码提示使用循环(C )
Password Prompt using For Loop (C++)
我的代码中有一个小问题,从练习问题中使用Alex Allain使用下面的for循环进入C 。
代码下面
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string password;
for ( int i = 5; i>=0; i-- ) {
cout << "Please enter your password you have " << i << " attempts remaining: ";
cin >> password;
}
if ( password == "Kent" ) {
cout << " Password is correct.";
break;
} else {
i--;
cout << i << " attempts remaining." << endl;
}
return 0;
}
我在下面的Break语句中遇到此错误
C:UsersDocumentsJumping into C++srcpractice 4 (chapter 5
variant).cpp|23|error: name lookup of 'i' changed for ISO 'for' scoping
[-fpermissive]|
我使用段循环进行了相同的精确代码,没有问题,但是当我使用循环时,编译器会进行狩猎。
您应该将if-else放在for循环中:
for ( int i = 5; i>=0; i-- ) {
cout << "Please enter your password you have " << i << " attempts remaining: ";
cin >> password;
if ( password == "Kent" ) {
cout << " Password is correct.";
break;
} else {
cout << i-1 << " attempts remaining." << endl;
}
}
相关文章:
- 如何循环打印顶点结构
- 如何在C++中从两个单独的for循环中添加两个数组
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 正在尝试了解输入验证循环
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 循环后如何继续阅读
- Ardunio UNO解决了多个重叠的定时器循环
- Eigen如何在容器循环中干净地附加矩阵
- 在某些循环内使用vector.push_back时出现分段错误
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- 如何使用递归循环我的代码(当用户输入无效输入时,它会再次提示他们)?
- 为什么我的代码在 c++ 中无限循环.我的代码需要反复提示用户
- if/else 循环:C++程序:不会显示最终提示/最终循环
- 密码提示使用循环(C )
- 循环不提示用户输入(C )
- 为项目和循环创建简单的控制菜单会比预期打印提示的次数更多
- 如何向编译器提供有关循环运行最长时间的提示
- 如何使用 for 循环多次提示用户输入不同的文件名
- 如何使用循环显示菜单并重新提示输入
- 循环重复而不再次提示用户