如何使用do / while,While语句或For语句查找用户输入的几个整数的总和

时间:2014-02-17 20:23:26

标签: c++ for-loop while-loop sum do-while

请在这里帮忙。我想创建一个程序,用户输入几个数字(让我们说他/她的头上有6个数字)。然后程序应继续计算所有这些数字的总和。但是我必须使用循环语句,For语句,While语句或do / while语句。这就是我到目前为止所做的:

#include <iostream>

using namespace std;

int main()
{
    int count = 1;
    int sum = 0;
    int number;
    int numberitems;

    cout << "Enter number of items: \n";
    cin >> numberitems;

    cout << "Enter number: \n";
    cin >> number;

    do {

        sum = sum + number;

        count++;
    } while (count <= 6);
}

此处是使用声明的其他代码

#include <iostream>

using namespace std;

int main()
{
    int count = 1;
    int sum = 0;
    int number;

    cout << "Enter number: \n";
    cin >> number;

    while (count <= 6) {
        sum = sum += number;

        count++;
    }
    cout << sum << endl;
}

我知道这是初学者的事,我是初学者,所以请礼貌地帮助姐姐。 致谢

5 个答案:

答案 0 :(得分:1)

FOR循环运行良好,我稍微修改了一下:

#include<iostream>
using namespace std;
int main ()
{

    int sum = 0;
    int number;
    int numberitems;


    cout << "Enter number of items: \n";
    cin >> numberitems;

    for(int i=0;i<numberitems;i++)
    {
        cout << "Enter number: \n";
        cin >> number; 

        sum=sum+number;

    }
    cout<<"sum is: "<< sum<<endl;
}

但是,WHILE循环在第11行有一些错误(在此范围内未声明Count)。可能是什么问题?此外,如果你有一个使用DO,WHILE循环的解决方案,那将是美妙的。感谢

答案 1 :(得分:0)

你应该这样做:

#include<iostream>
using namespace std;
int main ()
{

    int sum = 0;
    int number;
    int numberitems;


    cout << "Enter number of items: \n";
    cin >> numberitems;

    for(int i=0;i<numberitems;i++)
    {
        cout << "Enter number <<i<<":" \n";
        cin >> number; sum+=number;
    }
    cout<<"sum is: "<< sum<<endl;
}

并使用while语句

#include <iostream>
using namespace std;
int main ()
{
    int sum = 0;
    int number;
    int numberitems;
    cin>>numberitems;

    cout << "Enter number: \n";

    while (count <=numberitems)
    {
        cin >> number;
        sum+=number;
    }
    cout << sum << endl;
}

答案 2 :(得分:0)

请注意,如果您要添加内容,则可能始终要检查您是否超出int的限制(特别是在家庭作业练习中)。 此外,int main ()应返回int

使用“do .. while”循环:

#include<iostream>
using namespace std; 
int main () 
{

  int sum = 0;
  int previous = 0;
  int number;
  int numberitems;
  int count = 0;

  cout << "Enter number of items: ";
  cin >> numberitems;

  if ( numberitems <= 0 ) 
  {
    //no request to perform sum
    cout << "Quitting without summing.\n\n";
    return 0;
  }

  do
  {
    cout << "Enter number to add : ";
    cin >> number; 

    sum+=number;

    // check here that the addition didn't break anything.
    // Negative + negative should stay negative, positive + postive should stay positive
    if ((number > 0 && previous > 0 && sum < 0) || (number < 0 && previous < 0 && sum > 0))
    {
      cout << "Error: Beyond int limits !!";
      return 1;
    }

    count++;
    previous = sum;

  }
  while ( count < numberitems);

  cout<<"sum is: "<< sum<<endl;

  return 0;
} 

答案 3 :(得分:0)

#include<iostream>
int main()
{//initialize variables
    int limit;
    int num;
    int sum=0;
    int counter=0;

    cout<<"Enter limit of numbers you wish to see"<<" ";
    cin>>limit;
    cout<<endl;

while(counter<limit)
{

   cout<<"Enter number "<<endl;
  cin>>num;

  sum=sum+num;
  counter++;
}
cout<<"The sum of numbers is "<<" "<<endl

return 0;
}

答案 4 :(得分:0)

一个简单的程序显示了如何使用for循环来查找几个整数的总和。

#include <iostream>    
using namespace std;

int main ()
{
    int sum = 0;

    int endnum = 2;

    for(int i = 0; i<=endnum; i++){
        sum += i;
    }
    cout<<sum;
}