[英]Trouble with an if else if statement c++
I am working on a grade book project that has 5 students that I want to read the names in for and then with an inner loop grab 4 grades for each student. 我正在一个有5个学生的年鉴项目上工作,我想为其阅读名称,然后通过内循环为每个学生获取4个成绩。 Something is not working on this loop. 在此循环上无法正常工作。 This what I am getting: 这就是我得到的:
Please enter the name for student 1: Dave 请输入学生1的姓名:Dave
Please enter the grade number 1 for Dave: 100 请输入戴夫的年级数字1:100
Please enter the grade number 2 for Dave: 100 请输入戴夫的2年级数字:100
Please enter the grade number 3 for Dave: 100 请输入戴夫的3年级数字:100
Please enter the grade number 4 for Dave: 10 请输入戴夫的年级数字4:10
Please enter the name for student 2: James 请输入学生2的姓名:James
Please enter the grade number 5 for James: 100 请输入James的5年级数字:100
Please enter the name for student 3: Sam 请输入学生3的姓名:Sam
Please enter the grade number 5 for Sam: 100 请输入山姆的5年级数字:100
Please enter the name for student 4: Jack 请输入学生4的姓名:杰克
Please enter the grade number 5 for Jack: 100 请输入Jack的5年级数字:100
Please enter the name for student 5: Mike 请输入学生5的姓名:Mike
Please enter the grade number 5 for Mike: 100 请输入迈克的5年级数字:100
It should grab 4 grades before it jumps to the next student. 在跳到下一个学生之前,它应该获得4个等级。 I have not been able to figure this out for the last couple hours. 在过去的几个小时中,我一直无法弄清楚。 Here is the code I have so far: 这是我到目前为止的代码:
#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;
}
}
}
}
Thanks for any help on this! 感谢您的帮助!
There's some pretty rough stuff going on in here, but the problem is that you have a semi-colon on all your inner loops except the first one: 这里有一些相当粗糙的东西,但是问题是,除了第一个循环外,所有内部循环中都有分号:
for (count2; count2 < SCORES; count2++);
Remove the semi-colon, and the stuff in the braces will become part of the loop. 删除分号,大括号中的内容将成为循环的一部分。
I'm going to suggest you make your code a little tidier and less error-prone by chucking all those function arguments into their own array when you enter the function, like this: 我建议您通过在输入函数时将所有这些函数参数移到它们自己的数组中,使代码更简洁,更不易出错,如下所示:
double *scores[5] = { student1, student2, student3, student4, student5 };
Then you take OUT all that repetition - the copy/paste is what caused your problems to begin with: 然后,您将所有重复都删除了-复制/粘贴是导致您的问题开始的原因:
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;
}
}
Why can't you use two nested loops like 为什么不能使用两个嵌套循环
for (int studix=0, stduix<STUDENTS; studix++) {
//...
for (int gradix=0; gradix<SCORE; gradix++) {
//...
}
//....
}
BTW, the condition could be a more complex one, eg with the internal loop being 顺便说一句,条件可能更复杂,例如内部循环
bool goodgrade=true;
for (int gradix=0; goodgrade && gradix<SCORE; gradix++) {
// you could modify goodgrade or use break; inside the loop
}
Don't forget the possible use of continue
and break
inside a loop. 不要忘记在循环内continue
和break
的可能用法。
And please, take time to read some good C++ programming book 而且,请花一些时间阅读一些不错的C ++编程书
Building on Basile's answer and my comments: 以巴西莱的回答和我的评论为基础:
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]...
}
//....
}
Yes, I know about 2 D arrays, but I am trying to make explicit how this can be done with "five individual arrays". 是的,我知道2D数组,但是我试图明确说明如何使用“五个单独的数组”来完成。 Clumsy, but I believe this works. 笨拙,但我相信这行得通。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.