为什么这个 C++计算器老是显示错误?

发布于 2025-01-10 03:31:38 字数 1751 浏览 0 评论 0原文

所以我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

路弥 2025-01-17 03:31:38

主要问题出在语句 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) variable m 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文