2013-10-20 30 views
4

如何用空格讀取輸入行(類型字符串)?我嘗試了getline,但它進入了無限循環。以下是我的代碼。用C++中的空格讀取字符串

#include <iostream> 
#include <cstring> 

#define MAX 50 //size of array 

//Used G++ 4.6.3 compiler 
using namespace std; 

int main() { 

struct Manager { 
string name; 
int age; 
int working_years; 
string phone; 
int salary; 
}info[MAX]; 

char inp; //To choose options 
int array_pos = 0; //Current position in array of Manager structure 
string search_name; //Manager name you want to search 

cout << "Press 'i' to insert manager information or 's' to search for manager information by name or 'a' to abort: "; 
cin >> inp; 

while(inp != 'a') { 
int search_num = 0; //array position at which search result is found 
int found = 0; 
if (inp == 'i' || inp == 's') { 
    if (inp == 'i') { 
     int k = array_pos; 
     cout << "Enter the information of the manager no "<<k+1<<" is : "; 

     cout << "Enter the Name : "; 
        //infinte loop occurs 
     getline(info[array_pos].name, '\n'); 
     //cin >> info[array_pos].name; 

     cout<<"Enter manager age : "; 
     cin >> info[array_pos].age; 

     cout << "Enter manage working years : "; 
     cin >> info[array_pos].working_years; 

     cout << "Enter manager phone no. : "; 
     cin >> info[array_pos].phone; 

     cout << "Enter manager salary : "; 
     cin >> info[array_pos].salary; 
     array_pos++; 
    } 
    if (inp == 's') { 
     cout << "Enter the manager name you want to search : "; 
     cin >> search_name; 
     for(int i = 0; i < array_pos; i++) { 
      //using str1.compare(str2) to compare manager name 
      if(info[i].name.compare(search_name) == 0) { //manager name found in array of structure 
       found = 1;     
       search_num = i;     
       cout << "Name : " << info[search_num].name << "\n"; 
       cout << "Age: " << info[search_num].age << "\n"; 
       cout << "Working Years: " << info[search_num].working_years << "\n"; 
       cout << "Phone No. : " << info[search_num].phone << "\n"; 
       cout << "Salary : " << info[search_num].salary << "\n"; 
      } //end of if loop for comparing string 
     } //end of for loop for searching 
     if(found == 0) 
      cout << "No Manager by this name exist in record" << "\n"; 

    } //end of if loop 

} //end of if loop for searching or insertion 
if(inp == 'a') 
    break; 

cout << "Press 'i' to insert manager information or 's' to search for manager information by name or 'a' to abort: "; 
cin >> inp; 
} //end of while loop 

return 0; 
} 
+0

由於您在讀取文件時沒有檢查錯誤(包括文件結尾),因此會出現無限循環。 –

回答

6

「我怎樣才能讀取空白輸入行(字符串類型)?」

std::string line; 
if (std::getline(std::cin, line)) { 
    ... 
} 

注意,除了檢查std:getline調用的返回值,你也應該避免混合>>運營商std::getline電話。一旦你決定讀一行文件中的行,這似乎是更清潔,更合理,只是讓一個巨大的環,同時使用字符串流對象,如做額外的解析:

std::string line; 
while (std::getline(std::cin, line)) { 
    if (line.empty()) continue; 
    std::istringstream is(line); 
    if (is >> ...) { 
     ... 
    } 
    ... 
} 
3

解決方案#1:

char c; 
cin >> noskipws; // Stops all further whitespace skipping 
while (cin >> c) { // Reads whitespace chars now. 
    count++; 
} 

解決方案2:

char c; 
while (cin.get(c)) { // Always reads whitespace chars. 
    count++; 
} 
4
讀取字符串用空格沒有理會std名字空間

簡單的方法是如下

#include <iostream> 
#include <string> 
using namespace std; 
int main(){ 
    string str; 
    getline(cin,str); 
    cout<<str; 
    return 0; 
}