为什么这个 C++计算器老是显示错误?
所以我对 C++ 还很陌生,我正在制作一个乘法/除法计算器。当它只是一个操作时,一切都工作正常,但现在它给了我一堆错误
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
char operation;
cout << "Select an operation devision(d) multiplication(m)" << endl;
cin >> operation;
float firstNumber;
cout << "Please enter a number ";
cin >> firstNumber;
float secondNumber;
cout << "\nEnter a second number ";
cin >> secondNumber;
float answer;
if (operation = m) {
answer = firstNumber * secondNumber;
}
else if (operation = d) {
answer = firstNumber / secondNumber;
cout << "\nYour answer is " << showpoint << fixed << setprecision(2) << answer;
}
system("pause>0");
}
在底部附近,它根据您输入的操作进行计算,它说有这些错误:
<source>: In function 'int main()':
<source>:19:25: error: 'm' was not declared in this scope; did you mean 'tm'?
19 | if (operation = m) {
| ^
| tm
<source>:22:30: error: 'd' was not declared in this scope
22 | else if (operation = d) {
| ^
ASM generation compiler returned: 1
<source>: In function 'int main()':
<source>:19:25: error: 'm' was not declared in this scope; did you mean 'tm'?
19 | if (operation = m) {
| ^
| tm
<source>:22:30: error: 'd' was not declared in this scope
22 | else if (operation = d) {
| ^
So I'm pretty new to C++ and I'm making a multiplication/division calculator. Everything was working fine when it was just one operation but now its giving me a bunch of errors
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
char operation;
cout << "Select an operation devision(d) multiplication(m)" << endl;
cin >> operation;
float firstNumber;
cout << "Please enter a number ";
cin >> firstNumber;
float secondNumber;
cout << "\nEnter a second number ";
cin >> secondNumber;
float answer;
if (operation = m) {
answer = firstNumber * secondNumber;
}
else if (operation = d) {
answer = firstNumber / secondNumber;
cout << "\nYour answer is " << showpoint << fixed << setprecision(2) << answer;
}
system("pause>0");
}
Near the bottom where it's calculating based on the operation you put in, it says there these errors:
<source>: In function 'int main()':
<source>:19:25: error: 'm' was not declared in this scope; did you mean 'tm'?
19 | if (operation = m) {
| ^
| tm
<source>:22:30: error: 'd' was not declared in this scope
22 | else if (operation = d) {
| ^
ASM generation compiler returned: 1
<source>: In function 'int main()':
<source>:19:25: error: 'm' was not declared in this scope; did you mean 'tm'?
19 | if (operation = m) {
| ^
| tm
<source>:22:30: error: 'd' was not declared in this scope
22 | else if (operation = d) {
| ^
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
主要问题出在语句
if (operation = m)
中,您在其中进行赋值 (=
) 而不是比较 (==
) 。这也使用(不存在的)变量m
而不是值"m"
或'm'
。一旦你在乘法部分解决了这个问题,你在除法部分也会遇到同样的问题。您也只打印除法部分的输出,但这并不是真正的语法错误,而是逻辑错误。将其移到 if 语句之外以打印答案,无论选择什么操作。
The main problems are in the statement
if (operation = m)
where you're doing assignment (=
) instead of comparison (==
). This is also using the (non-existent) variablem
instead of the value"m"
or'm'
.Once you fix that in the multiplication section, you have the same problems in the division section. You're also only printing the output in the division section, but that's not really a syntax error, but a logic error. Move that outside the if statement to print the answer regardless of the operation chosen.