2016-01-14 163 views
0

我遇到了麻煩,我已經閱讀了其他論壇關於這個問題的網站,但他們並沒有真正涉及到我的情況。我是C++的新手,我幾乎只有3周左右的時間。我不知道爲什麼我的break語句不起作用,對我來說這是在循環中。我錯過了什麼?Break語句不在循環語句或switch語句中?

float a[60][7]; 
int row; 
cout << "How many students would you like to grade?" << endl; 

cin >> studentNum; 

cout << "Enter grades for the first student" << endl; 

row = 0; 
n = studentNum - 2; 

while (row < studentNum) 
{ 
    a[row][0]=k; 
    a[row][0] = row; 
    cout << "Enter grade for test 1: "; 
    cin >> a[row][1]; 
    cout << "Enter grade for test 2: "; 
    cin >> a[row][2]; 
    cout << "Enter grade for test 3: "; 
    cin >> a[row][3]; 
    cout << "Enter grade for test 4: "; 
    cin >> a[row][4]; 



    a[row][5] = (a[row][1] + a[row][2] + a[row][3] + a[row][4])/4; 
    a[row][6] = a[row][1] * 0.2 + a[row][2] * 0.3 + a[row][3] * 0.3 + a[row][4] * 0.2; 

    row++; 
} 
if (row == n) 
{ 
    cout << "Enter Grades for the last Student" << endl; 
    cout << "Enter grade for test 1: "; 
    cin >> a[row][1]; 
    cout << "Enter grade for test 2: "; 
    cin >> a[row][2]; 
    cout << "Enter grade for test 3: "; 
    cin >> a[row][3]; 
    cout << "Enter grade for test 4: "; 
    cin >> a[row][4]; 
    break; 
} 
else 
{ 
    cout << "Enter grades for the next student" << endl; 
    row = row + 1; 
} 



cout << "Stdnt" << "\t" << "Grade1" << "\t" << "Grade2" << "\t" << "Grade3" << "\t" << "Grade4" << "\t" << "Avg1" << "\t" << "Avg2" << endl; 
printarray(a, studentNum); 

cin.get(); 
return 0; 
+0

你想在'if'結尾使用'break'來達到什麼目的? – Michael

+0

我真的很感激這個幫助,我正在網上學習,但我知道這是一個錯誤,我的教授。將不會回覆我的電子郵件:( – frank104

+0

@邁克爾我想要聲明「爲最後一名學生輸入成績」以結束與最後一名學生的成績,但就目前而言,當我運行該程序時,「爲成績輸入最後一個學生「甚至不會彈出,並且」爲最後一個學生輸入成績「出現在程序的最底部 – frank104

回答

0

是這樣的嗎?

float a[60][7]; 
int row = 0; 

std::cout << "How many students would you like to grade?" << std::endl; 
std::cin >> studentNum; 

n = studentNum - 2; 

std::cout << "Enter grades for the first student" << std::endl; 
while (row <= n) 
{ 
    a[row][0] = k; 
    a[row][0] = row; 
    std::cout << "Enter grade for test 1: "; 
    std::cin >> a[row][1]; 
    std::cout << "Enter grade for test 2: "; 
    std::cin >> a[row][2]; 
    std::cout << "Enter grade for test 3: "; 
    std::cin >> a[row][3]; 
    std::cout << "Enter grade for test 4: "; 
    std::cin >> a[row][4]; 


    a[row][5] = (a[row][1] + a[row][2] + a[row][3] + a[row][4])/4; 
    a[row][6] = a[row][1] * 0.2 + a[row][2] * 0.3 + a[row][3] * 0.3 + a[row][4] * 0.2; 

    if (row==n) 
    { 
    std::cout << "Enter Grades for the last Student" << std::endl; 
    std::cout << "Enter grade for test 1: "; 
    std::cin >> a[row][1]; 
    std::cout << "Enter grade for test 2: "; 
    std::cin >> a[row][2]; 
    std::cout << "Enter grade for test 3: "; 
    std::cin >> a[row][3]; 
    std::cout << "Enter grade for test 4: "; 
    std::cin >> a[row][4]; 
    } 
    else 
    { 
    std::cout << "Enter grades for the next student" << std::endl; 
    } 

    row++; 
} 

std::cout << "Student" << "\t" << "Grade1" << "\t" << "Grade2" << "\t" << "Grade3" << "\t" << "Grade4" << "\t" << "Avg1" << "\t" << "Avg2" << std::endl; 
printarray(a, studentNum); 

std::cin.get(); 
return 0; 

P.S.用固定編輯

+1

刪除else子句中的額外行++。 –