麻烦的if else if语句c ++

时间:2013-04-29 05:11:02

标签: c++

我正在制作一个成绩簿项目,其中有5名学生,我想阅读其中的名字,然后用内循环为每个学生抓4个等级。有些东西不适用于这个循环。这就是我得到的:

请输入学生1的姓名:Dave
请输入Dave的等级编号1:100
请输入Dave的等级编号2:100
请输入Dave的等级编号3:100
请输入Dave的4级:10 请输入学生2的姓名:James
请输入詹姆斯的成绩5:100 请输入学生3的姓名:Sam
请输入Sam的等级编号5: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;
            }
         }

     }
}

感谢您提供任何帮助!

3 个答案:

答案 0 :(得分: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;
    }
}

答案 1 :(得分:2)

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

  for (int studix=0, stduix<STUDENTS; studix++) { 
     //...
     for (int gradix=0; gradix<SCORE; gradix++) {
        //...
     }
     //....
  }
BTW,病情可能更复杂,例如内部循环

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

不要忘记在循环中可能使用continuebreak

请花点时间阅读一些优秀的C ++编程书

答案 2 :(得分: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]...

 }
 //....
}

是的,我知道2 D阵列,但我试图明确如何用“五个单独的阵列”来完成。笨拙,但我相信这很有效。