2013-10-01 40 views
3

我給我的教師發送了一封求助於我的cin.ignore的消息,這就是她給我發回的消息。問題是,當你最後閱讀 - 它包括逗號。所以你的cin.ignore需要2個參數,100,\ n。在這種情況下,你不需要cin.ignore。剛剛閱讀,然後中間。然後使用字符串函數「查找」逗號並創建一個從最後一個位置0開始到逗號前的位置的子字符串。問題是我不知道她在說什麼。我知道如何放置查找功能,但我沒有得到第二部分。所以我已經把我的程序查找和SUBSTR顯然不工作使用find和substr從我的輸出中刪除逗號

#include <iostream> 
#include <string> 

using namespace std; 

char chr; 
int main() 
{ 
    string last, first, middle; 

    cout << "Enter in this format your Last name comma First name Middle name. " << endl; 
    // Input full name in required format 
    last.find(","); 
    last.substr(0); 
    cin >> last; 

    // receiving the input Last name 
    cin >> first; 
    // receiving the input First name 
    cin >> middle; 
    // receiving the input Middle name 
    cout << first << " " << middle << " " << last; 
    // Displaying the inputed information in the format First Middle Last name 

    cin >> chr; 

    return 0; 
} 
+0

格式被固定爲「米勒,約翰亨利」還是米勒,約翰亨利(無空格)或米勒,約翰亨利(逗號兩邊的空格)?如果您要以這種方式解決問題,空間的位置和數量很重要。 – us2012

+0

可能最好是用'getline'將逗號分隔爲逗號,然後修剪任何多餘的空格。 –

回答

1

你似乎在這裏有一些基本的誤解。

last.find(",");last.substr(0);都不會自己做任何事情。他們都返回一個結果,但是如果你不把它分配給任何東西,結果就會丟失。此外,substr需要兩個參數。如果你省略第二個,last.substr(0)將簡單地返回全部last

你的老師的意思可能是last = last.substr(0, last.find(","));。請注意,之後您從cin中讀取了字符串。讓我們藉此聲明除了:

  • last.find(",")將返回位置的逗號是
  • last.substr(0, last.find(","))last前面的部分逗號
  • 最後,分配last = ...將確保last實際上得到改變。

請注意,這隻適用於逗號直接附加到姓氏(如"Miller, John Henry")。

+0

find(「,」)發現第一個逗號後的字符怎麼樣?這些角色應該是某個名字的一部分嗎? –

+0

這解決了我的問題,讓我更好地瞭解我在做什麼感謝 –

-3

也許你可以嘗試把你的

last.find(","); 
last.substr(0); 

你CIN後?

+0

對不起,這不會有什麼區別。 –

0

好吧,我想這是你想要什麼:

#include <iostream> 
#include <string> 
#include <vector> 
#include <boost/algorithm/string.hpp> 

using namespace std; 

int main() 
{ 
    // Read a line from stdin 
    string line; 
    getline(cin, line); 

    vector<string> str_vec;                                       
    boost::split(str_vec, line, boost::is_any_of(" ,")); 
    if (str_vec.size() == 3) { 
     string last = str_vec[0]; 
     string first = str_vec[1]; 
     string middle = str_vec[2]; 
     std::cout << "First=" << first << " Last=" << last << " Middle=" << middle << endl; 
    } 
} 

如果你不想使用(或沒有)Boost庫,你可以查看下面的替代版本。評論應該有助於你理解代碼的每個塊。

#include <iostream>                                         
#include <string> 

using namespace std; 

int main() 
{ 
    // Read a line from stdin 
    string line; 
    getline(cin, line); 

    // Erase all commas 
    while (true) { 
     size_t comma_pos = line.find(","); 
     if (comma_pos != std::string::npos) { 
      line.erase(comma_pos, 1); 
      line.insert(comma_pos, " "); 
     } 
     else break; 
    } 

    // Change all two spaces to only one space 
    while (true) { 
     size_t space_pos = line.find(" "); 
     if (space_pos != std::string::npos) 
      line.erase(space_pos, 2); 
     else break; 
    } 

    // Tokenize the names 
    size_t end_last_pos = line.find(" "); 
    if (end_last_pos == string::npos) { 
     cout << "Missing last name" << endl; 
     return 1; 
    } 
    string last = line.substr(0, end_last_pos); 

    size_t end_first_pos = line.find(" ", end_last_pos + 1); 
    if (end_first_pos == string::npos) { 
     cout << "Missing first name" << endl; 
     return 1; 
    } 
    string first = line.substr(end_last_pos + 1, end_first_pos - end_last_pos - 1); 

    size_t end_middle_pos = line.find(" ", end_first_pos + 1); 
    string middle; 
    if (end_middle_pos == string::npos) 
     middle = line.substr(end_first_pos + 1); 
    else 
     middle = line.substr(end_first_pos + 1, end_middle_pos - end_first_pos - 1); 

    // Print the names 
    std::cout << "First='" << first << "' Last='" << last << "' Middle='" << middle << "'" << endl; 
}