0

我正在做一个成绩册项目,该项目有 5 名学生,我想读他们的名字,然后用一个内部循环为每个学生获取 4 个成绩。某些东西在这个循环上不起作用。这是我得到的:

请输入学生 1 的姓名:Dave
请输入 Dave 的年级 1:100
请输入 Dave 的年级 2:100
请输入 Dave 的年级 3:100
请输入年级4 代表 Dave:10
请输入学生姓名 2:James
请输入年级编号 5 代表 James:100
请输入学生姓名 3:Sam
请输入年级编号 5 代表 Sam:100
请输入学生姓名 4 : 杰克
请为 Jack 输入 5 年级:100
请输入学生 5 的姓名:Mike
请为 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;
            }
         }

     }
}

感谢您对此的任何帮助!

4

3 回答 3

3

这里有一些非常粗糙的东西,但问题是除了第一个循环之外,你的所有内部循环都有一个分号:

for (count2; count2 < SCORES; count2++);

去掉分号,大括号里的东西就会变成循环的一部分。

我建议您在输入函数时将所有这些函数参数放入它们自己的数组中,从而使您的代码更整洁,更不容易出错,如下所示:

double *scores[5] = { student1, student2, student3, student4, student5 };

然后你去掉所有的重复——复制/粘贴是导致你的问题开始的原因:

for (int i = 0; i < STUDENTS; i++)
{
    cout << "Please enter the name for student " << i+1 << ": ";
    cin >> names[i];
    cout << endl;

    for (int s = 0; s < SCORES; s++)
    {
        cout << "Please enter the grade number " << s+1 << " for " << names[i] <<": ";
        cin >> scores[i][s];
        cout << endl;
    }
}
于 2013-04-29T05:29:48.773 回答
2

为什么不能使用两个嵌套循环,例如

  for (int studix=0, stduix<STUDENTS; studix++) { 
     //...
     for (int gradix=0; gradix<SCORE; gradix++) {
        //...
     }
     //....
  }

顺便说一句,条件可能更复杂,例如,内部循环是

     bool goodgrade=true;
     for (int gradix=0; goodgrade && gradix<SCORE; gradix++) {
       // you could modify goodgrade or use break; inside the loop
     }

不要忘记在循环中可能使用continueand 。break

请花点时间阅读一些好的 C++ 编程书籍

于 2013-04-29T05:15:29.327 回答
1

基于 Basile 的回答和我的评论:

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};

double *gradeArray[STUDENTS];
gradeArray[0] = student1;
gradeArray[1] = student2;
gradeArray[2] = student3;
gradeArray[3] = student4;
gradeArray[4] = student5;

for (int studix=0, stduix<STUDENTS; studix++) { 
 // get the name of the student
 for (int gradix=0; gradix<SCORE; gradix++) {
    // put the grades in gradeArray[studix][gradix]...

 }
 //....
}

是的,我知道二维数组,但我试图明确说明如何使用“五个单独的数组”来完成。笨拙,但我相信这行得通。

于 2013-04-29T05:30:44.407 回答