2014-04-29 28 views
1
#include <string> 
#include <iostream> 
#include <iomanip> 
using namespace std; 
void add(string names[], int section[], int grade[]); 
void printRecords(const string names[], const int sections[], const int grades[], int size); 
void insertionSort(int numbers[], int size); 
void display(const int numbers[], int size); 
int main(){ 

    //command 
    string command; 
    //add 
    string names[20] = { "" }; 
    int sections[20]; 
    int grades[20]; 


    cout << "Enter a command (add, print, drop, sort, average, quit):\n"; 
    cin >> command; 

    while (command != "quit"){ 
     if (command == "add"){ 
      add(names,sections, grades); 
     } 
     else if (command == "print"){ 
      printRecords(names, sections, grades, 20); 
     } 
     else if (command == "drop"){ 
      cout << command; 
     } 
     else if (command == "sort"){ 
      cout << command; 
     } 
     else if (command == "average"){ 
      cout << command; 
     } 
     else if (command == "smallest"){ 
      cout << command; 

     } 
     else{ 
      cout << "Invalid command.\n"; 
     } 

     cin >> command; 

    } 
    cout << "Quitting the program.\n"; 
    return 0; 
} 

void printRecords(const string names[], const int sections[], const int grades[], int size) { 
    int i; 
    for (i = 0; i < size; i = i + 1) { 
     cout << "[" << i << "]:\t"; 
     cout << setw(20) << left << names[i]; 
     cout << setw(5) << sections[i]; 
     cout << setw(10) << grades[i]; 
     cout << "\n"; 
    } 
    return; 
} 
void insertionSort(int numbers[], int size) { 
    int i; 
    int j; 
    int toInsert; 
    // outer loop i is the element we are to insert in to the sorted portion. 
    // [0, i-1] is the sorted range. 
    for (i = 1; i < size; i = i + 1) { 
     toInsert = numbers[i]; 
     j = i - 1; 
     while (j >= 0 && numbers[j] > toInsert) { 
      numbers[j + 1] = numbers[j]; 
      j = j - 1; 
     } 
     // either j >= 0 and numbers[j] <= toInsert 
     // or j = -1 here 
     // we want to store toInsert at numbers[j+1] 
     numbers[j + 1] = toInsert; 
    } 
    return; 
} 
void add(string names[], int sections[], int grades[]){ 
    int i = 0; 
    while (names[i] != "" && i < 20){ 
     i = i + 1; 
    } 
    cin >> names[i]; 
    cin >> sections[i]; 
    cin >> grades[i]; 
     return; 
} 

我只需要用戶在調用該函數時只輸入一組新信息。 添加功能是我最關心的。爲什麼它無休止地傾瀉?爲什麼我的功能無限循環

假設在 當前電子表格的最後一條記錄後面添加一個學生的記錄(姓名,部分和等級)。您的電子表格應該能夠容納20個學生記錄。如果 已有20條學生記錄,則您的代碼應提示「電子表格已滿。可以 不可以添加。「

+0

在此處發佈代碼。 – alex

回答

0

cin >> command更改爲getline(cin, command)

+2

@MantoshKumar:爲什麼? – lpapp

+0

這不會有什麼區別,'cin >>'一個int或一個字符串都會跳過前導空格。 –

0

在這裏,這應該解決它。發生什麼事是Chnossos懷疑的。當你輸入緩衝區沒有清除換行符,除非你調用cin.ignore()來忽略它。但是,如果沒有換行符,您將最終等待它,讓您輸入額外的輸入來達到目的。

現在,getline(cin, var)消耗換行符,所以在使用此函數後您不需要忽略,否則您還將等待更多數據。

我沒有仔細研究它,但我編碼並且它正在工作,因此我相信你只是獲得了太多的換行符而沒有足夠的消耗,因此它看起來像一個無限循環。

有更雄辯的方式來做到這一點,但基本要點是字符串使用getline()以及其他類型,你可以繼續,如果你使用cin.ignore()以後使用cin >>

我建議你看看這個更多,也。這是代碼。

#include <string> 
#include <iostream> 
#include <iomanip> 
using namespace std; 
void add(string names[], int section[], int grade[]); 
void printRecords(const string names[], const int sections[], const int grades[], int size); 
void insertionSort(int numbers[], int size); 
void display(const int numbers[], int size); 
int main(){ 

//command 
string command; 
//add 
string names[20] = { "" }; 
int sections[20]; 
int grades[20]; 


cout << "Enter a command (add, print, drop, sort, average, quit):\n"; 
getline(cin, command); 

while (command != "quit"){ 
    if (command == "add"){ 
     add(names,sections, grades); 
    } 
    else if (command == "print"){ 
     printRecords(names, sections, grades, 20); 
    } 
    else if (command == "drop"){ 
     cout << command; 
    } 
    else if (command == "sort"){ 
     cout << command; 
    } 
    else if (command == "average"){ 
     cout << command; 
    } 
    else if (command == "smallest"){ 
     cout << command; 

    } 
    else{ 
     cout << "Invalid command.\n"; 
    } 

    getline(cin, command); 

} 
cout << "Quitting the program.\n"; 
return 0; 
} 

void printRecords(const string names[], const int sections[], const int grades[], int size) { 
    int i; 
    for (i = 0; i < size; i = i + 1) { 
     cout << "[" << i << "]:\t"; 
     cout << setw(20) << left << names[i]; 
     cout << setw(5) << sections[i]; 
     cout << setw(10) << grades[i]; 
     cout << "\n"; 
    } 
    return; 
} 
void insertionSort(int numbers[], int size) { 
    int i; 
    int j; 
    int toInsert; 
    // outer loop i is the element we are to insert in to the sorted portion. 
    // [0, i-1] is the sorted range. 
    for (i = 1; i < size; i = i + 1) { 
     toInsert = numbers[i]; 
     j = i - 1; 
     while (j >= 0 && numbers[j] > toInsert) { 
      numbers[j + 1] = numbers[j]; 
      j = j - 1; 
     } 
     // either j >= 0 and numbers[j] <= toInsert 
     // or j = -1 here 
     // we want to store toInsert at numbers[j+1] 
     numbers[j + 1] = toInsert; 
    } 
    return; 
} 
void add(string names[], int sections[], int grades[]){ 
    int i = 0; 
    while (names[i] != "" && i < 20){ 
     i = i + 1; 
    } 

    getline(cin, names[i]); 
    cin >> sections[i]; 
    cin.ignore(); 
    cin >> grades[i]; 
cin.ignore(); 
    return; 
} 
0

問題是,如果你試圖讀入一個整數,例如cin >> sections[i];,但您鍵入的字符不是數字,則該操作失敗,並且cin被置於失敗狀態

之後,cin保持失敗狀態,直到您通過執行cin.clear()取消該狀態。此操作不會從流中刪除有問題的字符,因此您通常需要將clear()與另一個讀取操作結合使用來讀取和丟棄問題字符。

如果您只有在程序期望整數時鍵入整數,那麼您的代碼將不會「無休止地循環」。顯然,當你期望整數時,你已經鍵入了單詞。

要解決這個問題,有兩件事情要做。首先,總是測試你的輸入功能失敗。做cin >> command;之後,也這樣做:

if (!cin) 
{ 
    break; // exit instead of infinitely looping 
} 

不過這是好事,只要它出現檢測到故障狀態。裏面的add功能,最後cin後,你應該做的:

if (!cin) 
{ 
    cout << "Invalid input entered - discarding entry\n"; 
    names[i] = ""; 
    cin.clear(); 
    string junk; 
    getline(cin, junk); 
} 

(還有其他的方法忽略垃圾,但我覺得這個是很容易理解)。

最後,add函數使用格式化提取(即>>),因此您無法確定該人是否按Enter鍵。如果你想讓它們按住Enter鍵總是意味着他們已經完成了輸入當前條目(並且無效條目將被丟棄),那麼你將需要使用std::getline來將整行存儲在一個字符串中,然後使用一個stringstream從該行進行格式化提取。

例如,add功能可能是:

void add(string names[], int sections[], int grades[]) 
{ 
    int i = 0; 
    while (names[i] != "" && i < 20){ 
     i = i + 1; 
    if (i == 20) 
     return; 

    string line; 
    getline(cin, line); 
    istringstream iss(line); 
    iss >> names[i] >> sections[i] >> grades[i]; 
    if (!iss) 
    { 
     names[i] = ""; 
     cerr << "Invalid entry ignored.\n"; 
    } 
} 

您可能需要#include <sstream>這一點。請注意,在此版本中,您不需要丟棄「垃圾」,因爲垃圾在正在銷燬的字符串流中。

作爲一個經驗法則,如果你是混合字符串和整數輸入,然後使用getline加上stringstream將使你的程序更加用戶友好。