我正在研究一個有5個學生的成績簿項目,我想讀取名字,然後用內部循環爲每個學生抓取4個成績。有些東西在這個循環中不起作用。這就是我得到:
請學生1輸入名字:戴夫
請輸入級1號戴夫:100
請輸入級號2戴夫:100
請輸入成績數量3戴夫:100
請輸入級4號戴夫:10
請學生2輸入名字:詹姆斯
請輸入級5號詹姆斯:100
請學生3輸入名稱:山姆
請參閱呃等級5號山姆:100
請學生4輸入名稱:傑克
請輸入級5號傑克:100
請學生5輸入名稱:邁克
請輸入成績數量5 for Mike:100
它應該抓取4個等級,然後才跳到下一個學生。在過去的幾個小時裏,我一直無法弄清楚這一點。下面是代碼我迄今:
麻煩if if語句C++
#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而不是一組學生?特別是因爲你知道數組...(是的,你可以有數組的數組,數組,結構的數組,不管你想要什麼) – Patashu 2013-04-29 05:14:04
爲什麼即使有循環,如果你打算在它內部有那麼大的if/else? – 2013-04-29 05:14:05
我相信你還沒有掌握循環的概念。 – 2013-04-29 05:15:17