【问题标题】:Can't compare string with while and if statements无法将字符串与 while 和 if 语句进行比较
【发布时间】:2016-01-29 00:14:51
【问题描述】:

我正在尝试创建一个命令菜单,用户可以在其中执行任意数量的命令,直到按“q”结束循环。我想我有我需要做的一切,除了我中途意识到我的教授要求使用字符串。当我在程序中包含字符串时,我开始收到错误消息,说“无法将字符串转换为布尔值”,只要有一段时间或 if 语句。我能做些什么来解决这个问题并让我的程序正常工作。提前致谢。

#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; 
}

【问题讨论】:

  • 当用户键入q 时,您并没有结束循环。在此处添加 break 语句。

标签: c++ string if-statement while-loop command


【解决方案1】:

您是在 分配 ifelse 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; 

【讨论】:

  • 还需要引用s
  • 引号和双 = 是解决方法。但是,现在当我运行程序时,当我输入命令而不是数字时,它会产生字母答案和“-”符号。我以为是char的原因,所以我把它换成了int。现在程序处于一个永无止境的循环中。
  • @Brandon,看我的编辑。 C++ 是一种有趣的语言,它在各种“数字”类型之间进行自动转换(char 是 C++ 中的一种数字类型)。所以当numberchar 并且你说number = 10 + 5; 然后cout &lt;&lt; number 你得到的是一个字符,即使你做了整数运算。所以改成int是正确的选择。但是不要更改option --- 保留它char
  • 是的。进行了调整,程序现在运行顺利。非常感谢您的帮助!
【解决方案2】:

让我回答,就好像我是评估你作业的人一样。您在这里有几个问题:

  1. 要求您使用string。避免同时使用charstring

    char option;      //  professor asked to use string: (-1) point
    
    string option;    //  ok
    
  2. 当您使用单个= 时,例如在option="a" 中,您将值"a"分配 到变量option。但是在if-else 语句中你想比较,所以你应该使用== 比较运算符。此外,您无法将 charstring 进行比较。

    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
    
  3. 您使用while(option),但option 被声明为char,而不是bool。将此行替换为while(option!="q") 以在您输入q 时完成。

    while(option);        // error: expression must have bool type; (-2) points
    
    while(option != "q");  // GOOD! 
    

    此外,当您退出 while-statement 时,您的程序将结束;因此,请尝试将"Terminating Program" 消息放在此之后。

  4. 您不需要声明这么多变量(snpqnumber2)。尝试在每个作用域内使用临时变量,例如:

    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;
      }
    
  5. 在您编写此代码的表单中,每次键入新选项时,您都会获得如下输出:

    Sum of 10+11 is : 21 Please enter a command:
    

    这对我来说很难看(-1 分)。尝试在每 cout 行之后添加一个换行符 (&lt;&lt; endl;)。

  6. 最后,如果我键入菜单中未列出的任何其他字母怎么办?我希望收到类似Enter a valid option 的消息(-1 分)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-28
    • 2018-10-15
    • 1970-01-01
    相关资源
    最近更新 更多