如何添加多个数字
How to add multiple numbers?
所以,我是一个初学者,正在学习C++。
我正在尝试创建一个程序,在尝试使用循环时添加多个数字。我什么都试过了,但计算器给了我错误的答案,或者根本不起作用。
这是我的代码:
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int main()
{
float num1;
float num2;
char sign;
float result = 0;
cout << "press 1 to calculate averages or press 2 to use simple" << endl;
cin >> sign;
if (sign == '2')
{
goto start;
}
start:
cout << "Calculator" << endl;
cout << "enter number" << endl;
cin >> num1;
cout << "* to multiply, / to divide, + to add and - to subtract" << endl;
cin >> sign;
cout << "enter another number" << endl;
cin >> num2;
do
{
cout << "press = to find result or enter another number" << endl;
cin >> sign ||num2;
result = num1 += num2;
}
while (sign != '=');
cout << "the result is: " << result << endl;
system ("pause");
}
有人看到我做错了什么吗?或者有人有什么解决方案吗?
将4+2+2=放入其中。预期结果:8。实际结果:10。会发生什么?
cout << "enter number" << endl;
cin >> num1;
num1=4
cout << "* to multiply, / to divide, + to add and - to subtract" << endl;
cin >> sign;
sign='+'
cout << "enter another number" << endl;
cin >> num2;
num2=2
do
{
cout << "press = to find result or enter another number" << endl;
cin >> sign ||num2;
丢弃第一个'+',符号=第二个'+],num2保持为2
result = num1 += num2;
num1=4+2,num1=6,结果=6
cout << "press = to find result or enter another number" << endl;
cin >> sign ||num2;
sign=最终2,num2=原始2
result = num1 += num2;
num1为6,num1=6+2=8,结果=8
cout << "press = to find result or enter another number" << endl;
cin >> sign ||num2;
sign=最终'=',num2=原始2
result = num1 += num2;
num1是之前的8,num1=8+2=10,结果=10
}
while (sign != '='); // get past here
cout << "the result is: " << result << endl;
打印10
所以,你所做的很多事情根本没有整合成一个连贯的计算器。
First(语法)
cin >> sign ||num2;
这不会像你预期的那样奏效。阅读更多关于位运算符及其工作原理的信息。
第二(逻辑)
如果您希望用户在生成输出之前继续输入值,则需要在不同的逻辑中进行输入(类似于这样)。
// after you have the operator choice and the first two values
do{
switch sign{
// perform the operation and store the value in result
result = num1 + num2; // for addition
}
// ask if the user wants to continue entering numbers
if (yes){ // swap values as follows
num1 = result;
cin >> num2;
continue = true;
}
else
continue = false;
}while(continue);
但这不允许您在用户期望的时候不断更改运算符。
您可以通过继续提示用户输入公式来实现循环,直到他们输入一些sentinel值,例如:0*0
你现在使用的代码并不能真正进行计算,而你将他们想要使用的运算符存储到"sign"变量中,而你没有用收集到的变量进行适当的计算。
您可以使用条件语句(即if(sign=='*')然后相乘),但在这种情况下,switch语句可能更容易。我使用相同的变量创建了一个简单的计算器,并展示了在这种情况下如何使用switch语句,打开变量"符号"来确定何时进行计算。
循环使用sentinel值0*0,因此当用户输入该等式时,程序将退出并输出消息"Goodbye!"。你可以将这些代码的一部分合并到你的项目中,你仍然需要添加开始部分(按1计算平均值或按2进行计算),但这个计算器应该会让你开始。
#include <iostream>
using namespace std;
main () {
float num1, num2;
char sign;
float result;
//you can set a sentinal value of "0/0" to implement a do/while loop
do {
cout << "Enter an equation (+,-,/,*) or '0*0' to exit: ";
cin >> num1 >> sign >> num2;
//stores the first number into num1, the operator into sign, and the second number into num2
//if the user enters the sentinal value, break out of the loop
if (num1 == 0 && sign == '*' && num2 == 0)
break;
//use a switch statement to do the right equation depending on the operator
switch (sign) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
}
//output the result
cout << "Result = " << result << endl;
} while (num1 != 0 && sign != '/' && num2 != 0);
cout << "Goodbye!" << endl;
}
- 我需要知道如何在 c++ 中使用增量值添加到一定数字
- 将随机生成的数字添加到数组 + 对这些数组求平均值
- 在不添加进位的情况下添加数字的C++程序
- 将两个数字添加为链表
- C++:添加到输出中的不需要的数字
- 如何非常快速地将数字添加到 Vector 中的一系列元素中
- 如何在 c++ 中添加两个大的双精度数字
- C++在减去 int 时向 int 添加一个额外的数字
- 字符串添加数字 C++
- C - 将数字添加到列表中?(就像python)
- 当我使用 += 将数字添加到数组中时,无论数字是什么,它总是以 0 结束
- 将数字添加到 QByteArray
- C++ 将数字添加到数组对象
- 使用 for 循环将数字添加到向量
- 将变量中的数字添加到对象的属性中
- 将[N]下的所有幸运数字添加到向量的算法
- 为什么当我将数字添加到字符串时,它在c++中显示随机文本
- 如何将数字添加到整数(如字符串)上
- 数字添加程序,最多100位数字.控制台输出奇数
- 在替换正则表达式中向匹配的数字添加一