双打意味着除加?

Doubles meant to divide add?

我正在制作一个用作计算器的 C++ 程序。除了除法函数外,一切正常,除法函数出于某种原因添加了双打。有人有解决办法吗?

我试过明确地转换成替身并用头撞桌子。这是我的一些代码片段。

#include "pch.h"
#include <iostream>
using namespace std;

void divide()
{
    //first number
    cout << "What is the numerator?";
    double firstNum = 0;
    cin >> firstNum;
    //second number
    cout << "What is the denominator?";
    double secNum = 0;
    cin >> secNum;
    //multiplying
    double answer = firstNum/(double)secNum;
    cout << "Your answer is " << answer << ".";
}

int main() 
{
    //asks for what operation user would like to use
    cout << "Do you want to add, subtract, divide, or multiply? Type [1] for add, [2] for subtract, [3] for divide, and [4] for multiply(minus the brackets).";
    double opquery = 0;
    cin >> opquery;

    // if division
    if (opquery == 3)
    {
        divide();
        return 0;
    }
}

我本以为 4/4 等于 1,但它只是 returns 加法

更新:完整代码可在 https://github.com/hoverdoge/cppcalculatorerror/blob/master/code

处找到

Actually, the code you share here is ok and the result of 4/4=1.

But the code you share in the github has something different. Remove the ;after if (opquery == 1); The program can work normally.

更改自:

if (opquery == 1);
    {
        add();
        return 0;
    }

收件人:

if (opquery == 1)
    {
        add();
        return 0;
    }

注意:当你在if(xxx)之后添加额外的;时,无论opquery的值是什么,主要方法将执行以下步骤:

cout << "xxx...";
int opquery = 0;
cin >> opquery;
add();
return 0;

这就是出现此问题的原因。