我正在尝试编写一个简单的计算程序,但答案一直返回为 0
I am trying to write a simple calculation program but the answer keeps coming back as 0
我花了几个小时试图弄清楚为什么它恢复为0 如果有人能提供帮助,那就太好了。
1 /*Written by Connor Green*/
2 /*CISP 1010 01/28/17*/
3
4 #include <iostream>
5 using namespace std;
6
7 int main()
8
9
10 {
11 int carton_size, number_cartons, eggs_per_omelette, number_of_omelettes;
12 /*This will tell you how many omelettes you can make*/
13
14 number_of_omelettes = carton_size * number_cartons / eggs_per_omelette;
15 cout << "Welcome to the Egg Ordering Guide.n";
16 cout << "How many eggs do you want per carton? (12, 18, or 25):n";
17 cin >> carton_size;
18 cout << "How many cartons?:n";
19 cin >> number_cartons;
20 cout << "How many eggs in an omelette? (2 or 3):n";
21 cin >> eggs_per_omelette;
22 cout << "You can make ";
23 cout << number_of_omelettes;
24 cout << " omelettes with this amount of eggs.n";
25
26 return 0;
27 }
这是因为
您在获取此计算的输入变量之前计算number_of_omelettes
。将该计算移动到输出之前
...
number_of_omelettes = carton_size * number_cartons / eggs_per_omelette;
cout << "You can make " << number_of_omelettes << " omelettes with this amount of eggs.n";
另外,请注意,除法运算符/
运算符在乘法运算符之前*
,这可能会导致整数除法导致零。为避免这种情况,请使用括号强制将乘法放在除法之前:
number_of_omelettes = (carton_size * number_cartons) / eggs_per_omelette;
相关文章:
- 我的C++级数计算程序出了什么问题?被困了好几个星期
- 使用 C++ 和 OOP 创建面积计算程序
- 简单的多播应用程序无法在同一网络上的不同计算机上运行
- 对于我扩展此程序来计算最高10x10矩阵的倒数的最简单方法是什么
- 我正在尝试编写一个简单的计算程序,但答案一直返回为 0
- 在c++上创建税务计算程序,错误为未初始化的局部变量
- 不计算程序中的最低和最高成绩。(C++)(流)
- 精确计算C++程序所使用的内存
- 使用OpenMP C++并行计算程序的积分
- 计算C++程序中留下评论的行数
- 让GPU做计算的简单方法
- 计算程序形状的法线
- 如何计算程序的空间复杂度
- 对根据时间和分钟计算通话成本的简单家庭作业程序感到沮丧
- C++利息计算程序
- 计算程序集中每个任务的准确 CPU 使用率
- c++计算程序没有响应
- 计算程序中的估计时间
- 程序简单,没有调用堆栈,"inpossible"查找错误
- 如何使大型计算程序更快