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

How to input values after for loop in C++?

本文关键字:循环 输入 for c++      更新时间:2023-10-16

我必须编写一个程序,输入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;
}

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

#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...
}

既然这是你的家庭作业,剩下的就自己做吧。祝一切顺利!