【发布时间】:2013-04-29 05:11:01
【问题描述】:
我正在做一个成绩册项目,该项目有 5 名学生,我想读取他们的姓名,然后使用内部循环为每个学生获取 4 个成绩。某些东西在这个循环上不起作用。这是我得到的:
请输入学生 1 的姓名:Dave
请输入 Dave 的 1 年级:100
请输入 Dave 的 2 年级:100
请输入 Dave 的 3 年级:100
请输入 Dave 的 4 年级:10
请输入学生 2 的姓名:James
请输入 James 的 5 年级:100
请输入学生 3 的姓名:Sam
请输入 Sam 的 5 年级:100
请输入学生 4 的姓名:Jack
请输入 Jack 的 5 年级:100
请输入学生 5 的姓名:Mike
请输入迈克的 5 年级:100
它应该在跳到下一个学生之前抢到 4 个等级。在过去的几个小时里,我一直无法弄清楚这一点。这是我到目前为止的代码:
#include <iostream>
#include <string>
using namespace std;
const int STUDENTS = 5; //holds how many students we have
const int SCORES = 4;
void getNames(string names[], double student1[SCORES], double student2[SCORES],
double student3[SCORES], double student4[SCORES], double student5[SCORES], int SCORES, int STUDENTS);
int main()
{
string names[STUDENTS] = {""};
char grades[STUDENTS] = {""};
double student1[SCORES] = {0};
double student2[SCORES] = {0};
double student3[SCORES] = {0};
double student4[SCORES] = {0};
double student5[SCORES] = {0};
getNames(names, student1, student2, student3, student4, student5, SCORES, STUDENTS);
// Make sure we place the end message on a new line
cout << endl;
// The following is system dependent. It will only work on Windows
system("PAUSE");
return 0;
}
void getNames(string names[], double student1[SCORES], double student2[SCORES],
double student3[SCORES], double student4[SCORES], double student5[SCORES], int SCORES, int STUDENTS)
{
for (int i = 0; i < STUDENTS; i++)
{
cout << "Please enter the name for student " << i+1 << ": ";
cin >> names[i];
cout << endl;
if (i == 0)
{
int count1 = 0;
for (count1; count1 < SCORES; count1++)
{
cout << "Please enter the grade number " << count1+1 << " for " << names[i] <<": ";
cin >> student1[count1];
cout << endl;
}
}
else if (i == 1)
{
int count2 = 0;
for (count2; count2 < SCORES; count2++);
{
cout << "Please enter the grade number " << count2+1 << " for " << names[i] <<": ";
cin >> student2[count2];
cout << endl;
}
}
else if (i == 2)
{
int count3 = 0;
for (count3; count3 < SCORES; count3++);
{
cout << "Please enter the grade number " << count3+1 << " for " << names[i] <<": ";
cin >> student3[count3];
cout << endl;
}
}
else if (i == 3)
{
int count4 = 0;
for (count4; count4 < SCORES; count4++);
{
cout << "Please enter the grade number " << count4+1 << " for " << names[i] <<": ";
cin >> student4[count4];
cout << endl;
}
}
else
{
int count5 = 0;
for (count5; count5 < SCORES; count5++);
{
cout << "Please enter the grade number " << count5+1 << " for " << names[i] <<": ";
cin >> student5[count5];
cout << endl;
}
}
}
}
感谢您对此的任何帮助!
【问题讨论】:
-
为什么你有数组 student1, 2, 3, 4, 5 而不是学生数组?特别是因为你知道数组......(是的,你可以拥有数组数组,结构数组,任何你想要的)
-
如果里面有那么大的 if/else,为什么还要有循环呢?
-
相信你还没有掌握循环的概念。
-
该项目要求我有五个数组,每组四个双打来保存每个学生的考试成绩。
-
std::array<std::array<double,SCORES>,STUDENTS>
标签: c++