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

标签 c++ for-loop

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

这是代码:它显示为学生 1,2 的四项考试成绩是多少,等等。当它说“学生 1 的四项考试成绩是多少”时,我怎样才能做到这一点,那么我会能够输入这些。然后输入到 student2、student3 等?

感谢您的宝贵时间。

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

既然这是你的家庭作业,那么你自己完成剩下的工作。祝一切顺利!

关于c++ - C++中for循环后如何输入值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7296201/

相关文章:

c++ - 将 COM 对象返回给 JScript

c++ - 如何让视频中的所有帧都变成 i 帧?

javascript - 删除 JSON 迭代中的重复项

c# - 用于创建方法的 for 循环,或一些等效的方法?

loops - Racket 中的 for 循环似乎对我不起作用

c++ - 错误 : "EXC_BAD_ACCESS(code=EXC_I386_GPFLT)" when i try to insert class objects in array of class objects c++

c++ - 指向 vector 的共享指针返回包含标准分配器的不同共享指针

c++ - 从类的成员函数访问类的 std::vector 数据成员

javascript - 我可以解释一下这个 javascript 闭包的语法吗?

c - For 循环和 While 循环在 C 中选择值