当我从可用选择中选择基础链接时,为什么要获得怪异的输出
why do i get a weird output when i select substraction from the available choices
我确实会在消息说差异不可负的消息后获取此消息/输出。我上传了输出的照片
#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;
}
相关文章:
- 为什么'Go To Definition'给我多种选择?
- C++为什么我的指针选择排序中存在分段错误?
- 为什么这个选择排序算法仍然切换一个元素,当它已经是其他元素中最小的元素时?
- 为什么选择转换运算符的重载?
- 为什么这个模板代码不选择部分专业化
- 为什么传递文字 3 会选择 int 重载而不是短重载?
- 为什么<iostream>运算符<<会选择明显错误的重载?
- 为什么当我选择>250000个采样点时,程序不起作用?
- 为什么没有选择模板专业化?
- 为什么Visual Studio无法在模板类中选择正确的构造函数
- 为什么选择 g++ 给予者:"error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast]"
- 为什么重载分辨率不选择模板函数的 std::vector 重载?
- 为什么QTableView扩展选择忽略了我的默认选择行
- 为什么日食总是发生错误"An internal error occurred during: "通知选择侦听器". java.lang.StackOverflowError"
- 为什么编译器在调用 move 后选择复制 ctor
- 当我从可用选择中选择基础链接时,为什么要获得怪异的输出
- 为什么我的 nim 游戏总是选择桩 A?
- 为什么CRTP(奇怪的递归模板模式)试图选择另一个私有基类的另一个同名函数
- 为什么我们选择"bounding box"方法来填充三角形?
- 为什么 lambda 自动和参数选择常量重载?