当我从可用选择中选择基础链接时,为什么要获得怪异的输出

why do i get a weird output when i select substraction from the available choices

本文关键字:选择 为什么 输出 链接      更新时间:2023-10-16

我确实会在消息说差异不可负的消息后获取此消息/输出。我上传了输出的照片

#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语句之前没有收到此消息>

在此处输入图像描述

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

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

一种解决方案是抛出异常(throw "The difference can not be negative";(,然后用catch (const char* e)捕获呼叫者。

(还可以施放对int的减法结果也是可疑的 - 作为经验法则,不要混合您的数据类型。(

我建议您使用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;
}

现在它正在工作

#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;
}