2014-09-13 115 views
0

這是我第一次在這個網站上,所以忍受着我。我也是一名新手C++程序員。C++字符串與空格問題

我會直指點。

我試圖讀取一個輸入文件與多個數據,但它不正確輸出。我會給你舉一個我的問題的小例子。基本上這個文件中我有3單獨字符串在同一行(從左至右)

BNA Boston Red Stockings NA

的三個單獨的字符串是列出:BNABoston Red Stockings,和NA

的事實上,Boston Red Stockings2空格使我很難。我不能僅僅使用getline,因爲在同一行NA之後有不同的字符串,這會將Boston Red Stockings NA存儲到字符串2中,這不是我想要的。我想Boston Red Stockings存儲到字符串二和NA存儲到字符串三。我試圖儘可能的明確。任何幫助,將不勝感激。謝謝。

#include <iostream> 
#include <fstream> 
#include <string> 
using namespace std; 

int main() { 
    string id, name, active; 
    ifstream inFile; 
    inFile.open("test.dat"); 
    inFile >> id; 
    getline(inFile, name); 
    inFile >> active; 
    cout << endl << id << endl << name << endl << active << endl << endl; 

    system("pause"); 
    return 0; 
} 

這裏是文本文件行: BNA Boston Red Stockings NA

+0

第一個和第三個字符串是否有空格? – polarysekt 2014-09-13 00:22:59

+0

請添加您的代碼,以便更容易地檢測問題或幫助您。 – VicM 2014-09-13 00:24:42

+0

更明確的是向我們展示代碼。 – 0x499602D2 2014-09-13 00:31:27

回答

1

您的問題是有關您的輸入定義:

  • 你有3個串讀
  • 你有5個字由searated空格
  • 有6種方法可以將字組合成字符串

因此,如何將你的PROGRAMM猜測這是之間的權利conbination:

BNA, Boston, Red Stokings NA 
BNA, Boston Red, Stockings NA 
BNA, Boston Red Stockings, NA 
BNA Boston, Red, Stockings NA 
BNA Boston, Red Stockings, NA 
BNA Boston Red, Stockings, NA 

基本上有兩種可能性繼續。

備選方案1:引入分隔符(例如上面的逗號)。

然後,您可以procedd如下:

string team1, team2, team3; 
getline (cin /*or another sream*/, team1, ','); // use a delimiter 
getline (cin, team2, ','); // again stop reading at the delimiter 
getline (cin, team3); // here a delimiter is not expected, so read until NL. 

備選方案2:使用允許值

如果預期輸入,帶或不帶空格的列表是已知的,你可以的固定名單讀取字符串,並讓程序進行嘗試並驗證它的猜測是否僅與允許的值相對應。

這裏一個小例子:

set<string> teams { "BNA", "BOSTON RED STOCKINGS", "CHICAGO WHALES", "NA" }; // set of valid input strings 
string input; 
getline(cin, input);            // get input line 
transform(input.begin(), input.end(), input.begin(), ::toupper); // convert to uppercase (case sensitive might be to error prone) 
vector<string> parts { istream_iterator<string>{stringstream(input)}, istream_iterator<string>{} }; // tokenize in words 

              // test potential combination of words 
for (int i = 1; i < parts.size() - 1; i++) { // length in words of the first sub string 
    for (int j = 1; j < parts.size() - i; j++) { // length in words of the second sub string 
     string s1,s2,s3; 
     for (int k = 0; k < parts.size(); k++) {  // construct substring; 
      if (k < i) s1 += (k == 0 ? "" : " ") + parts[k]; 
      else if (k < i + j) s2 += (k == i ? "" : " ") + parts[k]; 
      else s3 += (k == i + j ? "" : " ") + parts[k]; 
     } 
     cout << "Possible entry: " << s1 << " // " << s2 << " // " << s3<<endl; 
     if (teams.find(s1) != teams.end() && teams.find(s2) != teams.end() && teams.find(s3) != teams.end()) 
      cout << "Valid entry found =======>" << s1 << " // " << s2 << " // " << s3 << endl; // FOUND => DO SOMETING 
    } 
} 
// IF END OF LOOP BUT NO VALID VALUE COMBINATION FOUND, there is an error in the input 
1

您可以使用字符串::找到空間第一次出現,並得到第一個值

下一頁使用字符串:: RFIND得到其他兩種令牌

1

你當然應該使用std::getline,與你最初的意圖相反。 std::getline會爲您讀取整行,並將其放入單個字符串中。標準C++庫中的其他功能將無法輕鬆,方便地完成此功能。

但是,您會發現,僅僅因爲您最初將整行放入單個字符串中,並不妨礙您以任何方式取得該單個字符串,並將其切分爲三個較小的字符串。

你說字符串中的第一個和第三個單詞從來沒有空格,所以你要抓住第一個和最後一個以空格分隔的單詞,然後是中間的所有東西。

1)使用std::string的find()方法查找字符串中的第一個空白。使用rfind()來查找字符串中的最後一個空格。 2)一旦你知道了第一個和最後一個空間的位置,在第一個空間之前提取所有的東西,最後一個空間之後的所有東西,然後是兩者之間的所有東西都變得非常容易。

3)你也應該想想會發生什麼,當

A)的字符串是空 B)的字符串開頭或空格結束 C)的字符串包含兩個或更少的空間

你應該準備好處理所有可能性,以便生成一個健全的,適當的解決方案。