如何在C ++中for循环后输入值?

时间:2011-09-03 22:19:53

标签: c++ for-loop

我必须制作一个输入10个学生成绩的程序,并显示他们的权重和未加权平均值。我是C ++编程的新手,所以我不太了解,当人们使用他没有教过的东西时,我的教授不喜欢它。

以下是代码:它显示为学生1,2的四个考试成绩是什么,等等。当我说“学生1的四个考试成绩是多少”时,我怎么能做到这一点,那么我会能够进入那些。再到学生2,学生3等?

感谢您的时间。

#include <iostream>
using namespace std;
const int numberofstudents = 10;

int main()
{

int student;
for(student=1; student<=numberofstudents; student++)
cout << "\nWhat are the four test scores of student number " << student << endl;

return 0;
}

1 个答案:

答案 0 :(得分:4)

我想你想为每个学生阅读四个值,如果是这样,那么理解这个代码:

#include <iostream>
using namespace std;

int main()
{
   const int numberofstudents = 10;
   double scores[numberofstudents][4];
   for(int student=0; student<numberofstudents; student++)
   {
     cout << "\nWhat are the four test scores of student number " << (student+1) << endl;
     int i = 0;
     while ( i < 4 )
     {
         //scores is a two dimentional array
         //scores first index is student number (starting with 0)
         //and second index is ith score  
         cin >> scores[student][i]; //read score, one at a time!
         i++;
     }
   }
   //process scores...
}

既然这是你的功课,你自己完成剩下的工作。一切顺利!