2015-10-19 66 views
0

我的程序計算每個學生的平均分數。它輸出數字平均值和字母等級。有了這兩個功能,我就可以得到每個學生的字母等級,,但我不知道如何獲得每個字母等級的總金額。 (請注意,我不能使用結構或數組或矢量等爲這項任務:()發出函數返回值

#include <iostream> 

string letterScore(double&); 
int scoreCounter(string&); 

using namespace std; 

int main() { 
    /* repeat for 5 students */ 
    for (int i = 0; i < 5; i++) { 

     /* for simplicity, assume avgScore has already been calculated 
      and that function returned the average scores received for 
      each student: 90, 80, 70, 60, and 50 respectively */ 

     // pass the average score to figure out letter grades 
     string letterscore = letterScore(avgScore); 
    } 

    return 0; 
} 

string letterScore(double& avgScore) { 

    int As, Bs, Cs, Ds, Fs = 0; 
    string letterscore; 

    if (avgScore >= 90) 
    { 
     letterscore = "A"; 
     As = scoreCounter(letterscore); 
    } 
    else if (avgScore >= 80 && avgScore <= 89) 
    { 
     letterscore = "B"; 
     Bs = scoreCounter(letterscore); 
    } 
    else if (avgScore >= 70 && avgScore <= 79) 
    { 
     letterscore = "C"; 
     Cs = scoreCounter(letterscore); 
    } 
    else if (avgScore >= 60 && avgScore <= 69) 
    { 
     letterscore = "D"; 
     Ds = scoreCounter(letterscore); 
    } 
    else if (avgScore >= 50 && avgScore <= 59) 
    { 
     letterscore = "F"; 
     Fs = scoreCounter(letterscore); 
    } 

    return letterscore; 
} 

int scoreCounter(string& letterscore) { 
    int counter = 0; 

    if (letterscore == "A") 
    { 
     counter++; 
    } 
    else if (letterscore == "B") 
    { 
     counter++; 
    } 
    else if (letterscore == "C") 
    { 
     counter++; 
    } 
    else if (letterscore == "D") 
    { 
     counter++; 
    } 
    else if (letterscore == "F") 
    { 
     counter++; 
    } 

    return counter; 
} 

我知道問題出在第二個功能,我是如何連接兩個。現在,As,Bs,Cs,Ds,Fs的輸出是0而不是1(假設上面提到的90,80,70,60和50的平均值)。我怎樣才能回到main()每個字母等級總數?謝謝!

回答

1

我不知道究竟你被允許做什麼或不做什麼。
考慮到你不能使用數組和結構,你只能用你解決您的問題功能通過傳遞你的櫃檯參數作爲參考:

string letterScore(double& avgScore, int& countA, int& countB, int& countC, [etc...]) { 
    string letterscore; 

    if (avgScore >= 90) 
    { 
     letterscore = "A"; 
     countA++; // here is the counter ! 
    } 
    [etc...] // do the same for other blocks 

,並在您main()

int countA = countB = [etc...] = 0; 
for (int i = 0; i < 5; i++) { 
     string letterscore = letterScore(avgScore, countA, countB, [etc...]); 
    } 
+0

謝謝你這一點,你釘它,它是那麼簡單。 – Blueshift