从一个运行多次c ++的循环中获取输入

时间:2015-02-19 07:07:07

标签: c++ loops for-loop input output

我对c ++很新,我正在尝试并且已经搜索了如何获取循环6次的输入[Integer]并找到平均,最高和最低输入[Integer]。所以简单地说,该计划将一个接一个地要求6个不同的人得分。任何有关如何使用循环生成6种不同输出的说明将不胜感激。对不起,如果我在解释这个问题上听起来很简单。对我来说,c ++术语是一个缓慢的学习过程。这是我正在使用的for循环。

for(double score = 0.0; score < 6; score++)

3 个答案:

答案 0 :(得分:1)

使用for循环中的rand()srand()函数,您应该得到随机值。

您的另一个选择是要求用户使用cin>>number;

手动输入6个整数

希望这会有所帮助:)

答案 1 :(得分:1)

我认为这就是你想要的

double score[6];
cout << "Enter 6 different scores \n";
for(int i = 0; i < 6; i++)
cin >> score[i];

使用此循环,您必须为score输入6个值。

使用下一个循环,您可以输出先前输入的6个值

for( i = 0; i < 6; i++)
cout << score[i] << "\n";

答案 2 :(得分:1)

您需要为循环使用索引,然后为您的结果保留单独的变量,如下所示:

int sum = 0;
int highest = 0;    // this should be set to the minumum score.
int lowest = 1000;  // this should be set to the maximum score.

for (int i = 0; i < 6; i++) {
  // Read the next number
  int number;
  cin >> number;

  // Add it to the sum
  sum += number;
  // Check to see if it's higher than our current highest.
  if (number > highest) {
    highest = number;
  }
  // Check to see if it's lower than our current lowest.
  if (number < lowest) {
    lowest = number;
  }
}

// Now that we've read our six values, print out the results.
cout << "Average: " << (sum / 6) << endl;
cout << "Highest: " << highest << endl;
cout << "Lowest: " << lowest << endl;