如何正确声明if或while语句以满足所需的输入? 好的,所以我还是C++初学者。 我一直在尝试编写一个像样的基本计算器,以测试我的基本知识和我所知道的。 我一辈子都无法理解为什么程序没有通过循环重新启动,每次都继续结束,无论我输入“是”、“是”、“Y”还是“Y”。以下是我所拥有的: #include <iostream> using namespace std; int main() { int a, b; string c, d; while(c != "+", "-", "*", "/" or c == "Yes", "yes", "Y", "y") { cout << "N1 = "; cin >> a; cout << endl; if(!cin)//If anything but integer given, program ends { cout << "Give a Number next time.. " << endl; return 0; } cout << "N2 = "; cin >> b; cout << endl; if(!cin) { cout << "Give a Number next time.. " << endl; return 0; } cout << "+ , - , * or / ? "; cin >> c; cout << endl; if(!cin) { cout << "Give a Number next time.. " << endl; return 0; } if( c == "+")//Operator Functions from input { cout << a + b << endl; } else if( c == "-") { cout << a - b << endl; } else if( c == "*") { cout << a * b << endl; } else if( c == "/") { cout << a / b << endl; } if(c != "+ , -, * , /")//if not operator, Try again? { cout << "Again?... Y/N? " << endl; cin >> c; } if(c == "Yes", "yes", "Y", "y") { cout << "Restarting.. " << endl; } while(c == "No", "no", "N", "n") { cout << "closing... " << endl; return 0; } } #包括 使用名称空间std; int main() { INTA,b; 字符串c,d; 而(c!=“+”、“-”、“*”、“/”或c==“是”、“是”、“Y”、“Y”){ cout>a;cout
逗号运算符如何正确声明if或while语句以满足所需的输入? 好的,所以我还是C++初学者。 我一直在尝试编写一个像样的基本计算器,以测试我的基本知识和我所知道的。 我一辈子都无法理解为什么程序没有通过循环重新启动,每次都继续结束,无论我输入“是”、“是”、“Y”还是“Y”。以下是我所拥有的: #include <iostream> using namespace std; int main() { int a, b; string c, d; while(c != "+", "-", "*", "/" or c == "Yes", "yes", "Y", "y") { cout << "N1 = "; cin >> a; cout << endl; if(!cin)//If anything but integer given, program ends { cout << "Give a Number next time.. " << endl; return 0; } cout << "N2 = "; cin >> b; cout << endl; if(!cin) { cout << "Give a Number next time.. " << endl; return 0; } cout << "+ , - , * or / ? "; cin >> c; cout << endl; if(!cin) { cout << "Give a Number next time.. " << endl; return 0; } if( c == "+")//Operator Functions from input { cout << a + b << endl; } else if( c == "-") { cout << a - b << endl; } else if( c == "*") { cout << a * b << endl; } else if( c == "/") { cout << a / b << endl; } if(c != "+ , -, * , /")//if not operator, Try again? { cout << "Again?... Y/N? " << endl; cin >> c; } if(c == "Yes", "yes", "Y", "y") { cout << "Restarting.. " << endl; } while(c == "No", "no", "N", "n") { cout << "closing... " << endl; return 0; } } #包括 使用名称空间std; int main() { INTA,b; 字符串c,d; 而(c!=“+”、“-”、“*”、“/”或c==“是”、“是”、“Y”、“Y”){ cout>a;cout,c++,if-statement,while-loop,C++,If Statement,While Loop,逗号运算符,的作用与您认为的不同。它的语义是:“计算左侧,丢弃值,计算右侧,并将其用作表达式的结果。”它的优先级也最低。因此,对if条件的分析如下: if ( ((((c == "Yes"), "yes"), "Y"), "y") ) if (isOneOf(c, {"Yes", "yes", "Y", "y"})) 因此,Condition的计算值为“y”。这将经历数组到指针的转换,然后是指针到布尔的转换,并且由于指针为非空,因此计算结果为真 正确拼写条件的方法是: if (c == "Y
,
的作用与您认为的不同。它的语义是:“计算左侧,丢弃值,计算右侧,并将其用作表达式的结果。”它的优先级也最低。因此,对if
条件的分析如下:
if ( ((((c == "Yes"), "yes"), "Y"), "y") )
if (isOneOf(c, {"Yes", "yes", "Y", "y"}))
因此,Condition的计算值为“y”
。这将经历数组到指针的转换,然后是指针到布尔的转换,并且由于指针为非空,因此计算结果为真
正确拼写条件的方法是:
if (c == "Yes" || c == "yes" || c == "Y" || c == "y")
使用逻辑OR运算符|
。当然,这同样适用于while
条件
它是第一个if
条件(带有!=
)的条件,您正在测试字符串是否不同于文本字符串+,-,*,/
,它肯定会这样做。似乎您有意这样做:
if (c != "+" && c != "-" && c != "*" && c != "/")
也就是说,输入条件c
不是+
,它不是-
,它不是*
,它不是/
根据您的评论,如果您觉得重复比较乏味,您可以将其包装在函数中:
bool isOneOf(const std::string &needle, std::initializer_list<std::string> haystack)
{
return std::find(begin(haystack), end(haystack), needle) != end(haystack);
}
我有几点意见
开关
,而不是使用if-elseif-elseif
:
switch(c) {
case '+':
//do something
break;
case '-':
//do something
break;
}
这是因为实际上您只使用c
,而没有其他条件,因此开关可能更合适。if-else
更适合以下情况:
if (c == 'a'){
....
} else if (b == 36) { //notice there is a different variable used here
....
}
编辑:正如注释中所指出的,这需要将c
更改为char
。这种方式更简洁,但您需要为字符串使用不同的变量,该字符串接受“Yes”、“Yes”、“Y”、“Y”等输入
在Angew的优秀答案的基础上,如果您有这么多选项,可以将限定符移动到一个单独的函数,该函数包含一个包含完全失效的switch语句,则通常会更容易。这将极大地提高代码的可读性,如果您想在其他地方重用相同的语句,这一点尤其有用n示例如下所示。请记住,这只是一个建议,并不适用于所有情况。使用switch语句也可以显著改进其余代码,而不是使用大量长的
else if()
语句
替换此项:
while(c != "+", "-", "*", "/" or c == "Yes", "yes", "Y", "y")
为此:
while(ValidateOperatorInput(input_string)) // c in your case.
并在代码中的某个位置包含此函数:
inline bool ValidateOperatorInput(const std::string &input) {
switch(input) {
case "+": case"-": case "*": case "/":
case "Yes": case "yes": case "Y": case "y":
return true;
break;
// If the input string is not equal to any of the above cases then
// return false.
default:
return false;
}
}
if(c!=“+,-,*,/”
您不能像这样“链接”比较,这只是将c
与字符串“+,-,*,/”
进行比较。您需要为编译器详细说明:if(c!=“+”&&c!=“-”&&c!=“*”&&c!=“/”)
,与下文相同。当不需要刷新输出时,不要使用std::endl
。'\n'
可以很好地开始新的一行。谢谢用户657267!也谢谢pete,我知道'\n'但是刷新输出到底意味着什么,在什么情况下我会使用std:endl?@杰克输出通常是缓冲的-wh此时,输出被写入内存中的缓冲区,并仅发送到实际设备(终端、文件等)刷新流时。刷新发生在流关闭时、缓冲区增长超过一定大小时,或者在流上使用std::Flush
或std::endl
时。由于刷新通常是一个缓慢的操作,因此不必这样做是有意义的。这也意味着不要将std::endl
作为简单的行术语使用nator-这就是'\n'
的作用。std::string
不是顺序类型,因此不能在开关中使用。
@Angew你是对的当然,我前几天在java中使用了它,并认为它可以转移。我为我的无知道歉。@Angew你是对的。我刚刚注意到c在t中是string
这是因为我本能地考虑<代码> C <代码> >代码> char < /代码>。我编辑了我的答案,那么假设每个逻辑操作符之后,计算机是否把它作为单独的实体读取,不管我在if语句中使用哪一个逻辑运算符?“杰克,对不起,我不能理解你想说什么。在你的评论中,你能重新表述它吗,普拉斯?计算机“读”的“它”是什么意思?你在这里“读”的确切含义是什么?例如if(c==“Yes”| c==“Yes”| c==“Yes”| c==“Y”
每一个c==“Yes”
都将作为一个单独的实体从计算机中读取。这意味着每次它看c==“是”
使用|
或
运算符后,下一个值c==“是”
或c==“Y”“将必须包含<代码> C==< /Cord>。没有办法缩短这个吗?”Jack是的,这就是表达式在C++中通常如何工作,并且它有助于可读性。但是,我已经给我的答案增加了一个潜在的缩短。