2013-04-29 104 views
0

我正在研究一個有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; 
      } 
     } 

    } 
} 

感謝有這方面的幫助!

+2

爲什麼你有數組student1,2,3,4,5而不是一組學生?特別是因爲你知道數組...(是的,你可以有數組的數組,數組,結構的數組,不管你想要什麼) – Patashu 2013-04-29 05:14:04

+0

爲什麼即使有循環,如果你打算在它內部有那麼大的if/else? – 2013-04-29 05:14:05

+3

我相信你還沒有掌握循環的概念。 – 2013-04-29 05:15:17

回答

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

謝謝你做到了。是的,它看起來很粗糙,隨着時間的推移,它變得越來越粗糙,我沒有發現這種愚蠢;錯誤。感謝您的幫助,現在可以按照您的意見進行整理,並整理一下。 – 2013-04-29 05:37:06

+0

沒問題。只是一些一般性的建議,你應該立即質疑當你詢問成績時'count2'的值是否可以從'4'開始。尾隨的分號(空的for-loop)是一個迂迴和難以發現的問題,但質疑你的輸出應該直接引導你。一些實驗(例如切換'(i == 0)'和'(i == 1)''的條件會將該循環定位爲有問題的,然後您可以嘗試將它與字符逐字比較以便快速找到問題。無論如何,快樂的編碼=) – paddy 2013-04-29 05:43:57

+0

感謝稻田,我總是感謝學習新的麻煩拍攝方式。我在編程基礎1,並沒有在課堂上拍攝很多麻煩,這是非常在家裏做的學習。再次感謝。還要感謝提供解決方案的所有其他人員,我嘗試瞭解所有這些解決方案以瞭解更多信息。 – 2013-04-29 05:49:05

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++編程的書

+0

該項目特別要求使用持有4個等級的5個陣列。我沒有看到嵌套循環如何幫助我們,因爲我無法告訴循環現在它是下一個數組輪流。或者我錯過了什麼?我知道這樣做有更簡單的方法,但這些是我應該編寫該程序的準則。 – 2013-04-29 05:22:52

+0

我想你需要展示如何在內部循環中編制索引,因爲「項目需要」使用五個數組。看起來像'double * gradeArray [學生];'是解決方案的一部分。然後'gradeArray [0] = student1; '等 – Floris 2013-04-29 05:24:39

+0

你知道'break'和'continue'語句嗎 – 2013-04-29 05:24:46

1

大廈巴西萊的回答和我的意見:

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]... 

} 
//.... 
} 

是的,我知道大約二維數組,但我想明確地說明如何用「五個單獨的數組」來完成這個工作。笨拙,但我相信這是有效的。

+0

當你正在做double * gradeArray [學生]; *對數組做什麼?對不起,我在編程基礎1中遇到了一個愚蠢的問題,但還沒有做到這一點。 – 2013-04-29 05:52:46

+1

@paddy在他的答案中做了同樣的事情......我聲明瞭一個「指針數組」 - 因爲數組的「名稱」實際上是一個指針。所以當你有'double student1 [5]'時,'student1'實際上是一個指針(指向第一個元素)。當你創建一個指針數組時,你可以「選擇使用哪個數組」 - 在我的例子中,gradeArray [2]指向'student3'的開始,所以'gradeArray [2] [3]'是學生3(記得數組從零開始)。這是爲你解釋嗎? – Floris 2013-04-29 05:56:23

+0

只是爲了澄清:'double * anything'聲明'任何東西'是'double類型指針'的類型。指針和數組之間有着非常密切的關係 - 這對於理解很多C(和C++)代碼非常重要,並且非常值得讓你的大腦思考。一個常見的錯誤是使用指針而不確保它們指向一個有效的地址 - 爲您預留的內存。這是在你聲明'double myArray [5]'時自動完成的 - 但是通過真正理解指針的作用,解鎖了很多電源。 – Floris 2013-04-29 06:00:43