如何添加多个数字

How to add multiple numbers?

本文关键字:数字 添加 何添加      更新时间:2023-10-16

所以,我是一个初学者,正在学习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;
}