无法将字符串与 while 和 if 语句进行比较
Can't compare string with while and if statements
我正在尝试创建一个命令菜单,用户可以在其中执行 he/she 想要的任意数量的命令,直到按下 "q" 结束循环。我想我已经拥有了做这件事所需的一切,除了我在中途意识到我的教授要求使用字符串。当我将字符串包含到程序中时,我开始收到错误消息,在有 while 或 if 语句的地方显示 "could not convert string to bool" 。我该怎么做才能解决此问题并使我的程序正常运行。提前致谢。
#include <iostream>
#include <string>
using namespace std;
int main()
{
char option;
char number=0;
string s;
string n;
string p;
string q;
char number2;
cout << " Please enter a number: "<< endl;
cin >> number;
do {
cout << " Please enter a command: " << endl;
cout << " s- square the number " << endl;
cout << " n- add the number and (number +1) " << endl;
cout << " p- add the number and (number -1) " << endl;
cout << " q- quit" << endl;
cin >> option;
if (option=s) {
s= number*number;
cout << "Square of this number is : " << s;
}
else if ( option=n){
number2= number+1;
n= number+number2;
cout << "Sum of" << number << "+" << number2 << "is: " << n;
}
else if (option=p) {
number2= number-1;
p= number+number2;
cout << "Sum of" << number << "+" << number2 << "is" << p;
}
else if (option=q)
cout << "Terminating Program";
} while(option);
return 0;
}
您在 if
和 else if
中 分配 而不是 比较 .
if (option=s) {
应该是
if (option=='s') {
注意双 =
此外,您需要在字符选择两边加上单引号 (')。
这是一个常见的错误,即使是经验丰富的开发人员也会犯。
这些声明
char number=0;
string s;
string n;
string p;
string q;
char number2;
应该都是int
int number=0;
int s;
int n;
int p;
int q;
int number2;
让我回答,就好像我会评估你的作业一样。你在这里有几个问题:
您被要求使用 string
。避免同时使用 char
和 string
。
char option; // professor asked to use string: (-1) point
string option; // ok
当您使用单个 =
时,就像在 option="a"
中一样,您 将值 "a"
分配给 变量 option
。但是在if-else
语句中你要比较,所以你应该使用==
比较运算符。此外,您无法将 char
与 string
.
进行比较
if(option = "a") // error: expression must have bool type: (-2) points
if(option == 'a') // error: no operator "==" matches std::string == char; (-2) points
if(option == "a") // ok
您使用 while(option)
,但 option
被声明为 char
,而不是 bool
。将此行替换为 while(option!="q")
以在您输入 q
.
时完成
while(option); // error: expression must have bool type; (-2) points
while(option != "q"); // GOOD!
此外,当您从 while
语句中转义时,您的程序将完成;所以,试着把 "Terminating Program"
消息放在这之后。
不需要声明那么多变量(s
, n
, p
, q
, number2
) .尝试在每个范围内使用临时变量,例如:
if (option=="s")
{
cout << "Square of this number is : " << number*number << endl;
}
else if ( option=="n")
{
int number2= number+1;
cout << "Sum of " << number << "+" << number2 << " is : " << number+number2 << endl;
}
在您编写此代码的表单中,每次键入新选项时,您将获得如下输出:
Sum of 10+11 is : 21 Please enter a command:
这对我来说很难看(-1 分)。尝试在每 cout
行之后放置一个换行符 (<< endl;
)。
最后,如果我键入菜单中未列出的任何其他字母怎么办?我希望收到类似 Enter a valid option
(-1 分)的消息。
我正在尝试创建一个命令菜单,用户可以在其中执行 he/she 想要的任意数量的命令,直到按下 "q" 结束循环。我想我已经拥有了做这件事所需的一切,除了我在中途意识到我的教授要求使用字符串。当我将字符串包含到程序中时,我开始收到错误消息,在有 while 或 if 语句的地方显示 "could not convert string to bool" 。我该怎么做才能解决此问题并使我的程序正常运行。提前致谢。
#include <iostream>
#include <string>
using namespace std;
int main()
{
char option;
char number=0;
string s;
string n;
string p;
string q;
char number2;
cout << " Please enter a number: "<< endl;
cin >> number;
do {
cout << " Please enter a command: " << endl;
cout << " s- square the number " << endl;
cout << " n- add the number and (number +1) " << endl;
cout << " p- add the number and (number -1) " << endl;
cout << " q- quit" << endl;
cin >> option;
if (option=s) {
s= number*number;
cout << "Square of this number is : " << s;
}
else if ( option=n){
number2= number+1;
n= number+number2;
cout << "Sum of" << number << "+" << number2 << "is: " << n;
}
else if (option=p) {
number2= number-1;
p= number+number2;
cout << "Sum of" << number << "+" << number2 << "is" << p;
}
else if (option=q)
cout << "Terminating Program";
} while(option);
return 0;
}
您在 if
和 else if
中 分配 而不是 比较 .
if (option=s) {
应该是
if (option=='s') {
注意双 =
此外,您需要在字符选择两边加上单引号 (')。
这是一个常见的错误,即使是经验丰富的开发人员也会犯。
这些声明
char number=0;
string s;
string n;
string p;
string q;
char number2;
应该都是int
int number=0;
int s;
int n;
int p;
int q;
int number2;
让我回答,就好像我会评估你的作业一样。你在这里有几个问题:
您被要求使用
string
。避免同时使用char
和string
。char option; // professor asked to use string: (-1) point string option; // ok
当您使用单个
进行比较=
时,就像在option="a"
中一样,您 将值"a"
分配给 变量option
。但是在if-else
语句中你要比较,所以你应该使用==
比较运算符。此外,您无法将char
与string
.if(option = "a") // error: expression must have bool type: (-2) points if(option == 'a') // error: no operator "==" matches std::string == char; (-2) points if(option == "a") // ok
您使用
时完成while(option)
,但option
被声明为char
,而不是bool
。将此行替换为while(option!="q")
以在您输入q
.while(option); // error: expression must have bool type; (-2) points while(option != "q"); // GOOD!
此外,当您从
while
语句中转义时,您的程序将完成;所以,试着把"Terminating Program"
消息放在这之后。不需要声明那么多变量(
s
,n
,p
,q
,number2
) .尝试在每个范围内使用临时变量,例如:if (option=="s") { cout << "Square of this number is : " << number*number << endl; } else if ( option=="n") { int number2= number+1; cout << "Sum of " << number << "+" << number2 << " is : " << number+number2 << endl; }
在您编写此代码的表单中,每次键入新选项时,您将获得如下输出:
Sum of 10+11 is : 21 Please enter a command:
这对我来说很难看(-1 分)。尝试在每
cout
行之后放置一个换行符 (<< endl;
)。最后,如果我键入菜单中未列出的任何其他字母怎么办?我希望收到类似
Enter a valid option
(-1 分)的消息。