C++错误:在常量中有太多字符 对不起,如果它是简单的,但是我是C++的新手,还没有真正掌握它。我需要建立一个计算器,其唯一命名的变量是指针,这就是我目前所拥有的,但我不断地出错,我不知道为什么。但是,所有与我的if构造相关的错误 int main() { //Creating variables //Values to perform operations on float *aptr = new(nothrow)float; float *bptr = new(nothrow)float; float *ansptr = new(nothrow)float; int *endptr = new(nothrow)int; char *operationptr = new(nothrow)char; cout << "Simple Operation Calculator" << endl; //Displays program name cout << "Performs +, -, *, or / on any two real operands." << endl; //Describes nature of program to user *endptr = 1; while(*endptr = 1) //Creates a loop so that the user may perform as many operations as desired { //Prompts the user for the first operand cout << "First operand: " << endl; cin >> *aptr; //Prompts user for operator cout << "Operator(+,-,*,/): " << endl; cin >> *operationptr; //Prompts user for second operand cout << "Second operand: " << endl; cin >> *bptr; //Performs requested operation if(*operationptr == '+' || *operationptr == 'plus') { *ansptr = *aptr + *bptr; } else if(*operationptr == '-' || *operationptr == 'minus') { *ansptr = *aptr - *bptr; } else if(*operationptr == '*' || *operationptr == 'times') { *ansptr = *aptr * *bptr; } else if(*operationptr == '/' || *operationptr == 'divided by') { if(*bptr = 0) { cout << "Cannot divide by zero. Terminating program." << endl; *endptr = 2; break; } *ansptr = *aptr / *bptr; } else { cout << "Invalid operand input. Terminating program." << endl; *endptr = 2; break; } //Displays results cout << *aptr << *operationptr << *bptr << " = " << *ansptr << endl; //Asks user if they wish to perform another operation. If so, they stay in loop. If not, then break from loop. cout << "Do you wish to perform another operation? (1 = yes, 2 = no)" << endl; cin >> *endptr; //If 1, loop repeats. If 2, program ends. if (*endptr == 2) { cout << "Thank you for using my program. Goodbye!" << endl; } } //end while loop return 0; }//end main function

C++错误:在常量中有太多字符 对不起,如果它是简单的,但是我是C++的新手,还没有真正掌握它。我需要建立一个计算器,其唯一命名的变量是指针,这就是我目前所拥有的,但我不断地出错,我不知道为什么。但是,所有与我的if构造相关的错误 int main() { //Creating variables //Values to perform operations on float *aptr = new(nothrow)float; float *bptr = new(nothrow)float; float *ansptr = new(nothrow)float; int *endptr = new(nothrow)int; char *operationptr = new(nothrow)char; cout << "Simple Operation Calculator" << endl; //Displays program name cout << "Performs +, -, *, or / on any two real operands." << endl; //Describes nature of program to user *endptr = 1; while(*endptr = 1) //Creates a loop so that the user may perform as many operations as desired { //Prompts the user for the first operand cout << "First operand: " << endl; cin >> *aptr; //Prompts user for operator cout << "Operator(+,-,*,/): " << endl; cin >> *operationptr; //Prompts user for second operand cout << "Second operand: " << endl; cin >> *bptr; //Performs requested operation if(*operationptr == '+' || *operationptr == 'plus') { *ansptr = *aptr + *bptr; } else if(*operationptr == '-' || *operationptr == 'minus') { *ansptr = *aptr - *bptr; } else if(*operationptr == '*' || *operationptr == 'times') { *ansptr = *aptr * *bptr; } else if(*operationptr == '/' || *operationptr == 'divided by') { if(*bptr = 0) { cout << "Cannot divide by zero. Terminating program." << endl; *endptr = 2; break; } *ansptr = *aptr / *bptr; } else { cout << "Invalid operand input. Terminating program." << endl; *endptr = 2; break; } //Displays results cout << *aptr << *operationptr << *bptr << " = " << *ansptr << endl; //Asks user if they wish to perform another operation. If so, they stay in loop. If not, then break from loop. cout << "Do you wish to perform another operation? (1 = yes, 2 = no)" << endl; cin >> *endptr; //If 1, loop repeats. If 2, program ends. if (*endptr == 2) { cout << "Thank you for using my program. Goodbye!" << endl; } } //end while loop return 0; }//end main function,c++,C++,“加” 是字符常量,不能包含多个字符 “+”很好,因为它是常量中的单个字符 根据对这个答案的评论, 如果编译器不需要字符,则“plus”可以。有带“”的字符文本和带“”的字符串文本。字符文字有一个字符。字符串文字是字符数组。你不能写像“plus”这样的东西,因为它有不止一个字符,技术上你可以,但它是一个多字符的文字,但我们还是别说了 尽管如此,这没有任何意义,因为operationptr指向一个char对象。单个字符不能包含整个单词plus 如果您希望能够接受plus作为输入,那么我建议您开始使

“加”

是字符常量,不能包含多个字符

“+”很好,因为它是常量中的单个字符

根据对这个答案的评论,


如果编译器不需要字符,则“plus”可以。有带“”的字符文本和带“”的字符串文本。字符文字有一个字符。字符串文字是字符数组。你不能写像“plus”这样的东西,因为它有不止一个字符,技术上你可以,但它是一个多字符的文字,但我们还是别说了

尽管如此,这没有任何意义,因为operationptr指向一个char对象。单个字符不能包含整个单词plus

如果您希望能够接受plus作为输入,那么我建议您开始使用字符串。事实上,使用std::string

作为补充说明,您使用指针和动态分配的频率太高了。您还忘了删除使用new创建的对象-这是内存泄漏。我想你来自一种使用新语言来创建所有对象的语言。在C++中,这不是必要的,不是一个好的实践。相反,您可以这样声明对象:

float aptr;

不需要取消引用此对象。您可以直接将aptr用作浮点。

对于初学者来说,这是错误的:while*endptr=1您想要,while*endptr==1正确;甚至operationptr==plus在语义上也是错误的,因为字符串的内容很重要,而不是指针值。一旦取消了仅使用指针的限制,请使用std::string。或者事实上,立即使用它们并指向它们,见鬼。如果你仍然习惯于使用==而不是=,请考虑反转操作数。虽然*endptr=1将执行您可能不希望执行的操作,但while1=*endptr将无法编译。一些编译器会警告你这种事情。“加”是合法的C++,它不是一个字符,请看建议用于多个字符和“单个字符”。谢谢,这些信息非常有用。我忘了删除对象。使用new分配内存是赋值的一个要求,因为唯一允许的命名变量是指针。@HingleMcCringleberry这听起来像一个糟糕的赋值!让你的老师看看我的文章!