C++ If/else if statements
C++ If/else if statements
我正在编写一个关于克莱默法则和矩阵的程序(空闲时间太多了)。在某个地方,我有这个问题,你需要输入"是"或"否"。如果我输入其他单词(例如lol),它不会转到else If语句或再次询问问题。如何将答案限制为"是"/"否"?
cout << "There! you have now your first equation! is it " << a << "x+" << b << "y=" << e << "? Is it? Yes/No"<<endl;
cin >> z;
if ( z== "No" )
{
while ( z== "No" )
{
cout << "what is the value of x?"<<endl;
cin >> a;
cout << "n"<<endl;
cout << "what is the value of y?"<<endl;
cin >> b;
cout << "n"<<endl;
cout << "what is the value of answer?"<<endl;
cin >> e;
cout << "n"<<endl;
cout << "There! you have corrected your first equation! is it " << a << "x+" << b << "y=" << e << "? Is it? Yes/No"<<endl;
cin >> z;
}
}
else if ( z== "Yes" )
{
cout << "We will now proceed!"<<endl;
}
我只是一个c++初学者使用codeblocks
仔细阅读do while循环!这是一个post - test循环,所以你可以验证z是yes还是no。http://www.cplusplus.com/doc/tutorial/control/
基本思想是:当用户输入不等于yes或no时执行用户输入
基本例子:
int x;
do {
cout << "Please enter 1 or 0" << endl;
cin >> x;
} while (x != 0 && x != 1);
相关文章:
- 我的简单if-else语句是如何无法访问的代码
- 如何将enable-if与模板参数和参数包一起使用
- 无论条件是否为true,if总是在c++中执行
- Arduino:for/while/if在void setup()或void loop()之前?——错误:之前需要不合格
- Insert函数不适用于2 if语句C++
- If语句未被求值C++
- C++嵌套if语句,基本货币交换
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- 是否可以使用if constexpr删除控制流语句
- 要与"if constexpr"一起使用的编译时消息(在预处理器之后)
- 如何删除peer if else分支中的冗长句子
- 我似乎对if/else的基本语句有问题:/
- if数组上的随机数
- 将按位if条件转换为普通if条件
- If语句在c++中被忽略
- 比较if语句中的数组值和int值
- 使用if-else将数字转换为单词
- C 为什么我的程序循环和"if-statements"不起作用?
- C++ if statements
- C++ If/else if statements