if数组上的随机数
Random numbers on if array
我正试图在一个包含年龄数字的数组中找到最高的项。问题是它会在if行返回随机数。我想弄清楚背后的逻辑。
语言是C++,我确信这很容易解决。
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int edad[100], n, i, emayor=0;
float suma;
do {
cout << "Ingrese su edad: ";
cin >> edad[i];
suma+=edad[i];
i++;
cout << "nnDesea Ingresar Edades? (1/0) ";
cin >> n;
} while(n==1 && i<100);
cout << "La sumatoria de edades es: "<< suma;
if (edad[i]>emayor) {
emayor=edad[i];
cout << "nLa edad mayor es: "<<emayor;
}
getch();
return 0;
}
您的程序有未定义的行为,因为您使用的是i
和suma
而没有初始化它们。
此外,在使用数字之前,请确保检查数字提取是否成功。
使用
int edad[100] = {}; // Initialize the elements to zero.
int n = 0; // It's a good practice to initialize all variables.
int i = 0;
int emayor = 0;
float suma = 0;
do{
cout << "Ingrese su edad: ";
if ( ! (cin >> edad[i]) )
{
// Error in reading from cin.
// Add error handling code, or break out of the loop.
break;
}
suma += edad[i];
i++;
cout << "nnDesea Ingresar Edades? (1/0) ";
cin >> n;
} while ( n == 1 && i < 100 );
...
相关文章:
- if数组上的随机数
- 在将数字随机生成为数组期间从内存输出随机数的数组
- 具有随机数的二维数组不会更改
- 如何在 c++ 中从数组中选择一个随机数?
- C++ 用于在数组中打印字符串的随机数
- 如何防止每次 Arduino 启动时颜色数组都是相同的随机数组
- 创建整数的 2D 数组,该数组将使用两个函数用随机数填充矩阵.我做错了什么?
- 需要关于如何将我的随机数数组连接到冒泡排序的帮助
- 如何对0,1,..中的k个随机数进行采样..,n-1,而不缓存到数组
- 使用数组生成随机数
- 测量我的随机数数组打印的时间始终显示为 0 秒
- 数组初始化为所有元素为零,吐出随机数
- 生成随机数并将它们放入数组中
- 如何在C 中创建正常分布式随机数生成器的数组
- 将范围随机数保存到数组而不重复的函数
- 使用具有唯一随机数的结构数组创建多个对象
- visual如何在C++中创建一个随机数数组
- 双精度随机数数组
- 随机数数组每次都是相同的
- 使用英特尔TBB填充随机数数组