2016-04-14 179 views
0

我創建了一個程序,允許用戶輸入10個等級。我使用了一個while循環來存儲數組中的成績,但如果用戶只有5個等級可以輸入,他可以輸入done來退出程序。如何使用數組的while循環?

循環結束後,它會計算並顯示。列表中的最高年級,最低年級和平均成績

不幸的是,當用戶鍵入完成時,程序將顯示未輸入的其餘成績線。

你能幫我找出如何停止while循環來顯示循環的其餘未被中止的等級嗎?

#include <iostream> 
using namespace std; 

int main() 
{ 
const int SIZE = 10; 
int grade[SIZE]; 
int count = 0; 
int lowestGrade; 
int highestGrade; 
bool done = false; 


cout << "This program is limited to entering up to 10 grades." << endl; 

while (grade[count] != done && count < SIZE) 
{ 

    cout << "Enter a grade #" << count + 1 << " or done to quit: "; 
    cin >> grade[count]; 
    count++; 

} 

//LOWEST GRADE 
lowestGrade = grade[0]; 
for (count = 0; count < SIZE; count++) 
    if (grade[count] < lowestGrade) 
    { 
     lowestGrade = grade[count]; 
    } 

//HIGHEST GRADE 
highestGrade = grade[0]; 
for (count = 0; count < SIZE; count++) 
{ 
    if (grade[count] > highestGrade) 
    { 
     highestGrade = grade[count]; 
    } 
} 


//AVERAGE GRADE 
double total = 0; 
double average; 
for (int count = 0; count < SIZE; count++) 
    total += grade[count]; 
average = (total/SIZE); 

cout << endl; 

cout << "Your highest grade is: " << highestGrade << endl; 
cout << "Your lowest grade is: " << lowestGrade << endl; 
cout << "Your average grade is: " << average << endl; 



system("pause"); 
return 0; 

} 
+0

看代碼'cin >>等級[count];'。你打算如何將「完成」一詞放在一個用於等級的數組中?也許像'-1'這樣的數字可以用來表示「完成」,然後檢查輸入的數字是否爲「-1」? – PaulMcKenzie

回答

1

這是您的代碼的兩個問題。

第一:

.... 
cout << "Enter a grade #" << count + 1 << " or done to quit: "; 
cin >> grade[count]; 
count++; 
.... 

上面的代碼將attepmpt讀字「做」成整型變量,產生0不是你想要做什麼!

:以上

... 
for (count = 0; count < SIZE; count++) 
... 

代碼將嘗試遍歷所有可能的元素(大小)。但是,你可能比這更少!您需要使用前一個循環中計算的count作爲邊界(當然,在循環中使用不同的名稱作爲控制變量)。

0

使用另一個變量來存儲用戶輸入的級別數量。您還可以將字符串不存儲在您的整數數組:

std::string input = ""; 
while(count < SIZE) 
{ 
    cout << "Enter a grade #" << count + 1 << " or done to quit: "; 
    getline(cin, input); 
    if(input == "done") 
     break;   
    try 
    { 
     grade[count] = std::stoi(input); 
     count++; 
    } 
    catch(std::invalid_argument) 
    { 
     cout << "not a valid number\n"; 
    } 
} 
int actualsize = count; 

,然後使用這個變量來終止您的for循環:

for (int i = 0; i < actualsize; i++) 
1

有幾件事情在這裏解壓。

基本上,您要檢索的輸入是char *並且>>運算符將其轉換爲int以適合您的等級數組。

下一步你正在用grade[count] != done檢查是否如果「等級」在ID「count」的整數不等於bool false。在這種情況下,這將始終返回true。

爲了您的使用情況,你要被檢查的是如果輸入等於字符 *「做」

這不能在while循環的謂詞發生,因爲你的等級陣列只存儲int

因此,我認爲最簡單的問題解決方案是檢查輸入是否等於「完成」。 如果是你想要設置完成布爾值爲true 否則,我們可以嘗試將其轉換爲int並將其存儲在等級數組中。

這裏是修改後的循環:

while (!done && count < SIZE) 
{ 
    cout << "Enter a grade #" << count + 1 << " or done to quit: "; 

    string input = ""; 
    cin >> input; 

    if (input == "done") 
    { 
     done = true; 
    } 
    else 
    { 
     grade[count] = stoi(input); 
    } 

    count++; 
} 

以下是有些外部的問題的範圍內,但)的其它附加優勢,使用Stoi旅館(是,它忽略輸入不是一個號碼,這將屏蔽某人輸入「馬鈴薯」等無效輸入。這就是我立即將輸入轉換爲字符串的原因。

0

有兩種簡單的方法來解決你的問題:

  • 您可以閱讀串的整數,而不是在情況下讀取字符串爲「完成」,打破循環,否則,讀字符串轉換爲整數,東西如下:

```

// rest of the code 

int total_count = 0; 

while (count < SIZE) { 
    cout << "Enter a grade #" << count + 1 << " or done to quit: "; 
    string temp; 
    cin >> temp; 
    if(temp == "done") { 
    break; 
    } else { 
    grade[count] = stoi(temp); 
    count++; 
    total_count = count; 
    } 
} 

// rest of the code 

```

  • 如果你不想使用字符串,那麼假設成績是非負數,你可以在用戶輸入負數時停止閱讀輸入,比如說「-1」。所以,你需要做的事情如下:

```

// rest of the code 

int total_count = 0; 

while (count < SIZE) { 
    cout << "Enter a grade #" << count + 1 << " or -1 to quit: "; 
    int temp; 
    cin >> temp; 
    if(temp == -1) { 
    break; 
    } else { 
    grade[count] = temp; 
    count++; 
    total_count = count; 
    } 
} 

// rest of the code 

```

另外,不要忘記更換SIZE通過TOTAL_COUNT在其餘的循環即計算'最低等級','最高等級'和'平均等級'。

注意:如果使用第一個選項,則必須在頂部也執行#include <string>