计算用户使用循环输入的整数数量
count the amount of integers the user enters using a loop
#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 添加到 CNT。(请参阅下面带有//** 注释的行(。
此外,不需要在开始时进行 cnt==值检查(并且那里缺少一个"="字符(。
更新的代码
总而言之,您的代码应该更改如下:
#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;
}
相关文章:
- 如何初始化整数+字符串数组
- 整数指针数组的元素是否可以指向整数数组?
- 整数指针数组的大小
- 将整数输入数组的方法比 scanf() 或 cin 更快?
- 将字符 2d 数组转换为整数 2d 数组
- 取消随机排列整数/字符数组
- 用于存储原子整数的数组
- 在 C++ 中使用 10 个随机整数的数组进行气泡排序不起作用
- 指向整数 C++ 数组的指针
- 从矢量<对<双*,对<双*,整数*>>到数组的可视化C++转换
- 设置整数等于数组中的值数量
- 计算每个连续的整数在数组中总和
- 从文件中读取整数到数组
- 创建字符指针和整数指针数组
- 使用 const 整数声明数组
- 创建整数 2d 数组(与字符串 2d 数组相比),而不提及维度
- 发送 2 个整数的数组作为参数
- 如何在向量内的结构内打印指向整数的数组
- C++:使用 STL 排序对不同列上的整数 2d 数组进行排序的时间复杂度
- 对1000个整数的数组进行排序