2014-03-02 41 views
0

我必須編寫一個程序,要求用戶輸入0-100之間的等級,一旦用戶輸入數字「-1」,它將所有數據都記錄下來,並給出最高等級的數字以及平均值。反覆詢問用戶等級並使用C++存儲數據?

我很難得到它重複的問題,我很困惑如何存儲和計算所有的數據,當它是完全隨機的。

這是我想出迄今

#include <iostream> 
using namespace std; 

int main(){ 
    int num; 

    cout<<"To get final and highest score, enter -1."<<endl; 

    cout<<"Enter the score for student 1: "; 
    cin>>num; 
    while (num < 0 || num > 100){ 
     cout<<"Wrong. You must enter a number between 0 to 100.\n"; 
     cin>>num; 
    } 
    if (true){ 
     cout<<"Enter the grade for student 2: "; 
     cin>>num; 
    } 

    return 0; 
} 
+0

您不需要存儲「所有數據」 - 只是最高的值...並且您可以計算條目的總數和數量以跟蹤平均值。這比保留所有數字更清潔。當數字<0時,你必須從你的循環中突破,而不是說「錯誤」。這樣,你永遠不會離開...... – Floris

+0

'if(true){'顯然是多餘的! –

+0

我將如何計算條目的總數和數量? – user3371154

回答

0

一些僞代碼:

main(){ 
    nMax; 
    avg; 
    end = false; 
    num; 
    nNums = 0; 

    cout << "to exit enter -1"; 

    while(!end){ 
    do{ 
    cout << "Enter number"; 
    cin >> num; 
    }while(num < -2 || num > 100); 
    if (num < 0){ 
     end = true; 
    }else{ 
     avg = ((avg * nNums) + num)/++nNums; 
     nMax = max(nMax, num); 
    } 
    } 
    cout << avg << ", " << nMax; 
} 
0

給你:

#include <iostream> 

int main(){ 
    int num; 
    int sum=0, count=0; 
    int maxGrade = -1; 
    std::cout<<"To get final and highest score, enter -1."<<std::endl; 

    while(true) { 
    std::cout<<"Enter the score for student "<<count+1<<":"<<std::endl; 
    std::cin>>num; 
    if(num == -1) break;    // <<<< test specifically for break condition 
    if(num < 0 || num > 100) {   // <<<< test for any other invalid input 
     std::cout<<"Invalid grade. You must enter a number between 0 and 100.\n"; 
    } 
    else { 
     count++;       // <<<< track number of valid inputs 
     sum += num;      // <<<< track total grade 
     if(num>maxGrade) maxGrade = num; // <<<< track highest grade so far 
    } 
    } 
    std::cout<<"Number of students: "<<count<<std::endl; 
    std::cout<<"Average is "<< sum * 1.0/count<<std::endl; 
    std::cout<<"Highest grade is "<<maxGrade<<std::endl; 

return 0; 
} 

輸出示例:

To get final and highest score, enter -1. 
Enter the score for student 1: 
78 
Enter the score for student 2: 
55 
Enter the score for student 3: 
98 
Enter the score for student 4: 
-2 
Invalid grade. You must enter a number between 0 to 100. 
Enter the score for student 4: 
88 
Enter the score for student 5: 
-1 
Number of students: 4 
Average is 79.75 
Highest grade is 98 
+0

非常感謝你 – user3371154

+0

你的std :: endl的大部分應該用'\ n'替換,你不需要任何刷新流。 – Klaim

+0

@Klaim:我認爲「應該被替換」在這方面有點強大。我不相信像這樣的互動節目對性能至關重要。 「可以被替換」可能是更好的使用術語。我認爲這是一個偏好問題。例如,請參閱討論[此處](http://stackoverflow.com/a/8311177/1967396)。這實際上指出'cin'會刷新輸出緩衝區 - 所以你不需要明確地做。 – Floris

0

要計算最高等級和平均值,您不需要存儲所有輸入的值。所有你需要的是將輸入數字的總和與其數量和最大等級進行分類。該程序可能看起來如下

#include <iostream> 
using namespace std; 

int main() 
{ 
    bool empty = true; 
    int sum = 0; 
    int cnt = 0; 
    int max; 

    cout << "To get final and highest score, enter -1." << endl; 

    while (true) 
    { 
     cout << "Enter the score for student " << n + 1 << ": "; 

     int num = -1; 

     cin >> num; 

     if (num == -1) break; 

     if (num < 0 || num > 100) 
     { 
     cout << "Wrong. You must enter a number between 0 to 100.\n"; 
     } 
     else 
     { 
     if (empty || max < num) max = num; 

     empty = false; 
     sum += num; 
     ++n; 
     } 
    } 

    if (!empty) 
    { 
     cout << "The maximum score is " << max << endl; 
     cout << "The average score is " << sum/n << endl; 
// cout << "The average score is " << (sum + 0.0)/n << andl; //that to get a float number  
    } 

    return 0; 
}