简体   繁体   English

我正在为我祖母的生日编写 C++ 程序,但是我不断收到错误代码:错误 C2451

[英]I am working on a C++ program for my grandmother's birthday, however I keep getting error code: error C2451

I have only taken one C++ class and am planning on filling my grandma's children's names with different stuff, what I have currently is just filler.我只上过一门 C++ 课程,并计划用不同的东西填充我祖母孩子的名字,我目前所拥有的只是填充物。 However when I try to debug myself by changing string type to integer like it tells me to I then just get new error codes.但是,当我尝试通过将字符串类型更改为整数来调试自己时,就像它告诉我的那样,然后我只会得到新的错误代码。 Here is what I have currently, the program is quite simple:这是我目前所拥有的,该程序非常简单:

#include <iostream>
#include <string>
#include <cstring>

using namespace std;
string name;

string main() {
    bool done = false;
    while (!done) {

    cout << "Enter one of your children's names, or press 'q' to quit" << endl;
    cin >> name;

    if (name = "q") {
        done = true;
        break;
    }
    else if (name = "Jason") {
        cout << "Jason is your eldest son" << endl;
    }
    else if (name = "Aaron") {
        cout << "Aaron was your second child" << endl;
    }
    else if (name = "Mandy") {
        cout << "Mandy is your only girl" << endl;
    }
    else if (name = "Adam") {
        cout << "Adam came after Mandy" << endl;
    }
    else if (name = "Ben") {
        cout << "Ben is your youngest" << endl;
    }
    else {
        cout << "That name does not exist for your children: try Jason, Aaron, Mandy, Adam, or Ben instead" << endl;
    }
}

} }

On your if and else if statement you need to change == instead of = (assignment) in order to check if the values of two operands are equal or not.if and else if语句中,您需要更改==而不是= (赋值)以检查两个操作数的值是否相等。 Also, in your String main() I would recommend to change it for just: int main()另外,在您的String main()我建议仅将其更改为: int main()

Just to reiterate what everyone else has mentioned只是重申其他人提到的内容

  • Adjust your if/else compare operator ---> if (name == "q")调整 if/else 比较运算符 ---> if (name == "q")
  • Use int main()使用int main()

    int main()
    {
         bool done = false;
         string name;

         while (!done)
         {
              if (name == "q")
              {
                   done = true;
                   break;
              }
         // Remainder of code goes here

         } // Close while loop
    } // Close main()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM