计算用户使用循环

时间:2017-05-21 07:11:18

标签: c++ loops integer using counting

#include <iostream>
int main()
{
  int cnt = 0, sum = 0, value = 0;
    std::cout << "Please enter a set of numbers and then press ctrl+z and ENTER to add the numbers that you entered" << std::endl;
    if (cnt = value) 
    ++cnt;          
    while (std::cin >> value)

       sum += value;  

    std::cout << "the sum is: " << sum << std::endl;
    std::cout << "the amount of numbers you entered are: " << cnt << std::endl;

    return 0;

}

我所拥有的if语句是错误的,并且不计算用户输入值的整数数量。

如何让程序计算用户使用循环输入的整数量?

1 个答案:

答案 0 :(得分:4)

解决方案

为了计算提供的整数数,只要给出新输入,只需将1加1到cnt。 (见下面// **评论的行)。

此外,不需要在开始时进行cnt ==值检查(并且那里缺少一个&#39; =&#39;字符)。

更新代码

总结一下,你的代码应该改变如下:

#include <iostream>
int main()
{
    int cnt = 0, sum = 0, value = 0;
    std::cout << "Please enter a set of numbers and then press ctrl+z and ENTER to add the numbers that you entered" << std::endl;      
    while (std::cin >> value)
    {
       sum += value;  
       cnt++; //**
    }
    std::cout << "the sum is: " << sum << std::endl;
    std::cout << "the amount of numbers you entered are: " << cnt << std::endl;

    return 0;

}