如何添加多个数字?

时间:2015-11-19 01:05:31

标签: c++ visual-studio-2015

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



}

谁能看到我做错了什么?或者有人提出任何解决方案吗?

3 个答案:

答案 0 :(得分:0)

将4 + 2 + 2 =放入其中。预期结果:8。实际结果:10。会发生什么?

var numbers = [1,2,3,4]
numbers.forEach {
    if $0 % 2 == 0 {
        break
    }
}

num1 = 4

cout << "enter number" << endl;
cin >> num1;

sign ='+'

cout << "* to multiply, / to divide, + to add and - to subtract" << endl;
cin >> sign;

num2 = 2

cout << "enter another number" << endl;
cin >> num2;

扔掉第一个'+',sign = second'+',num2保持为2

do 
{ 

cout << "press = to find result or enter another number" << endl;
cin >> sign ||num2;

num1 = 4 + 2,num1 = 6,结果= 6

result = num1 += num2;

sign = final 2,num2 = original 2

cout << "press = to find result or enter another number" << endl;
cin >> sign ||num2;

num1从之前是6,num1 = 6 + 2 = 8,结果= 8

    result = num1 += num2;

sign = final'=',num2 = original 2

cout << "press = to find result or enter another number" << endl;
cin >> sign ||num2;

num1从之前是8,num1 = 8 + 2 = 10,结果= 10

    result = num1 += num2;

打印10

所以,你正在做很多事情,而这些事情根本就没有合并成一个连贯的计算器。

答案 1 :(得分:0)

First(Syntax)

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);

但是这不允许您在用户期望的时候不断更改操作符。

答案 2 :(得分:0)

您可以通过继续提示用户输入等式直到他们输入一些标记值来实现循环,例如:0 * 0

您现在使用的代码并没有真正进行计算,而您将要使用的运算符存储到&#34;符号&#34;变量你不能用你收集的变量进行适当的计算。

你可以使用条件(即if(sign ==&#39; *&#39;)然后相乘)但在这种情况下switch语句可能会更容易。我使用相同的变量创建了一个简单的计算器,并展示了在这种情况下如何使用switch语句,打开变量&#34; sign&#34;确定何时进行哪种计算。

循环使用sentinel值0 * 0,因此当用户输入该等式时,程序退出并输出消息&#34; Goodbye!&#34;。您可以将此代码的部分内容合并到项目中,您仍然需要在开头部分添加(按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;

}