当我从可用选项中选择减法时,为什么会得到奇怪的输出

时间:2019-05-20 12:52:06

标签: c++

在消息说差异不能为负之后,我确实得到了此消息/输出。我上传了输出的照片

#include<iostream>
#include<cstdlib>
using namespace std;

//Prototype
double add(double, double);
double subtract(double, double);
double multiply(double, double);
double divide(double, double);
void Choices();

int main()
{
    /// your program should work with doubles at all cases
    /// add more comments on the code. polish your code 

    // variable declaration:
    // To be used in the loop
    double x, y;
    int choice = 0;

    // loop execution
    for (int i = 0; choice != 5; i++)
    {
        Choices();
        cin >> choice;

        if (choice == 1)
        {
            cout << "Enter first number: ";
            cin >> x;
            cout << "Enter second number: ";
            cin >> y;
            cout << "Sum " << add(x, y) << endl;
            cout << "---------------------------" << endl << endl;
        }
        else if (choice == 2)
        {
            cout << "Enter first number: ";
            cin >> x;
            cout << "Enter second number: ";
            cin >> y;
            cout << "Difference: " << subtract(x, y) << endl;
            cout << "---------------------------" << endl << endl;
        }
        else if (choice == 3)
        {
            cout << "Enter first number: ";
            cin >> x;
            cout << "Enter second number: ";
            cin >> y;
            cout << "Product: " << multiply(x, y) << endl;
            cout << "---------------------------" << endl << endl;
        }
        else if (choice == 4)
        {
            cout << "Enter first number: ";
            cin >> x;
            cout << "Enter second number: ";
            cin >> y;
            cout << "Division: " << divide(x, y) << endl;
            cout << "---------------------------" << endl << endl;
        }

        else if (choice == 5)
        {
            /// number 5 is not an invald number
            cout << "Exit" << endl;
            cout << "You tried " << i << " times before you hit the end" << endl;
            cout << "---------------------------" << endl << endl;
            return 1;
        }
        else if (choice >= 6 || choice <= 0)
        {
            /// the program should not close after entering an invalide number
            cout << choice << " is not an option" << endl;
            cout << "------------------------------";
            cout << endl << endl;
        }
    }

    system("pause");
    return 0;
}
// To provide the avaialable choices/optioins:
void Choices()
{
    cout << "A menu driven program" << endl;
    cout << "1: Addition of two Numbers" << endl;
    cout << "2: Subtraction of two Numbers" << endl;
    cout << "3: Multiplicaion of two Numbers" << endl;
    cout << "4: Division of two Numbers " << endl;;
    cout << "5: Exit " << endl;
    cout << "--------------------------------------" << endl;
    cout << "Enter your choice: ";
}

// To add two numbers
double add(double a, double b)
{
    return a + b;
}

//To substract two numbers
double subtract(double a, double b)
{
    int difference = 0;
    difference = a - b;
    if (difference < 0)
    {
        cout << "The difference can not be negative";
        cout << endl << endl;
    }
    else if (difference > 0)
    {
        return difference;
    } /// the diffrence can't be negative. it is always posative. diffrence between 5 and 3 is 2. diffrence between 3 and 5 is 2.
}

// To multiply two numbers
double multiply(double a, double b)
{
    return a * b;
}

// To divide two numbers
double divide(double a, double b)
{
    return a / b;
}

我只想知道为什么会弹出该消息。在使用if语句之前,我没有收到此消息,不允许输出负数,但是在使用该语句之后,我确实收到了此输出或消息

enter image description here

3 个答案:

答案 0 :(得分:3)

代码的行为是不确定的。您的subtract函数不会在所有控制路径(包括“负”分支)上返回显式值。

使用返回值是未定义行为蔓延的地方。

一种解决方案是改为引发异常(throw "The difference can not be negative";),并使用catch (const char* e)在调用方中捕获该异常。

(也可能会将减法的结果强制转换为int-根据经验,请勿混淆数据类型。)

答案 1 :(得分:0)

我建议您使用return NULL
substract应该返回某些内容,因为您将其数据类型指定为double。仅void个函数不需要返回任何内容。
如果您未指定返回值,则编译器将返回not a number(nan)作为默认值

//To substract two numbers
double subtract(double a, double b)
{
    int difference = 0;
    difference = a - b;
    if (difference < 0)
    {
        cout << "The difference can not be negative";
        cout << endl << endl;
        return NULL;
    }
    else if (difference > 0)
    {
        return difference;
    } /// the diffrence can't be negative. it is always posative. diffrence between 5 and 3 is 2. diffrence between 3 and 5 is 2.
    else return NULL;
}

答案 2 :(得分:0)

现在可以正常使用

#include<iostream>
#include<cstdlib>
using namespace std;

//Prototype
double add(double, double);
void subtract(double, double);
double multiply(double, double);
double divide(double, double);
void Choices();

int main()
{
    /// your program should work with doubles at all cases
    /// add more comments on the code. polish your code 

    // variable declaration:
    double x, y;
    int choice = 0;

    // loop execution
    for (int i = 0; choice != 5; i++)
    {
        Choices();
        cin >> choice;

        //To perform a certain operation each time a choice is selected
        if (choice == 1)
        {
            cout << "Enter first number: ";
            cin >> x;
            cout << "Enter second number: ";
            cin >> y;
            cout << "Sum " << add(x, y) << endl;
            cout << "---------------------------" << endl << endl;
        }
        else if (choice == 2)
        {
            cout << "Enter first number: ";
            cin >> x;
            cout << "Enter second number: ";
            cin >> y;
            subtract(x, y);
            cout << "---------------------------" << endl << endl;
        }
        else if (choice == 3)
        {
            cout << "Enter first number: ";
            cin >> x;
            cout << "Enter second number: ";
            cin >> y;
            cout << "Product: " << multiply(x, y) << endl;
            cout << "---------------------------" << endl << endl;
        }
        else if (choice == 4)
        {
            cout << "Enter first number: ";
            cin >> x;
            cout << "Enter second number: ";
            cin >> y;
            cout << "Division: " << divide(x, y) << endl;
            cout << "---------------------------" << endl << endl;
        }

        //To end the program once exit choice is selected
        else if (choice == 5)
        {
            /// number 5 is not an invald number
            cout << "Exit" << endl;
            cout << "You tried " << i << " times before you hit the end" << endl;
            cout << "---------------------------" << endl << endl;
            return 1;
        }
        else if (choice >= 6 || choice <= 0)
        {
            /// the program should not close after entering an invalide number
            cout << choice << " is not an option" << endl;
            cout << "------------------------------";
            cout << endl << endl;
        }
    }

    system("pause");
    return 0;
}

// To provide the avaialable choices/optioins:
void Choices()
{
    cout << "A menu driven program" << endl;
    cout << "1: Addition of two Numbers" << endl;
    cout << "2: Subtraction of two Numbers" << endl;
    cout << "3: Multiplicaion of two Numbers" << endl;
    cout << "4: Division of two Numbers " << endl;;
    cout << "5: Exit " << endl;
    cout << "--------------------------------------" << endl;
    cout << "Enter your choice: ";
}

// To add two numbers
double add(double a, double b)
{
    return a + b;
}

//To substract two numbers
void subtract(double a, double b)
{
    //variable declaration to store the difference
    int difference = 0;

    //The equation
    difference = a - b;

    //To assure the difference is not negative 
    if (difference < 0)
    {
        cout << "The difference can not be negative";
        cout << endl << endl;
    }
    else if (difference >= 0)
    {
        cout << "The difference is: ";
        cout << difference;
        cout << endl << endl;
        return;
    } /// the diffrence can't be negative. it is always posative. diffrence between 5 and 3 is 2. diffrence between 3 and 5 is 2.
}

// To multiply two numbers
double multiply(double a, double b)
{
    return a * b;
}

// To divide two numbers
double divide(double a, double b)
{
    return a / b;
}