2016-02-28 75 views
0

我第一次使用cin.get來將一個字符一次抓到一個字符串「word」中。但由於某種原因,我無法將期限設置爲退出循環命令。不能使用cin.get來斷開循環來檢測句點

#include <cstdio> 
#include <stdio.h> 
#include <cstdlib> 
#include <iostream> 
#include <fstream> 
#include <ctype.h> 
#include <string> 
using namespace std; 

bool isAlpha (char ch); 
string replace (string ch); 
bool sexist (string ch); 

int main() 
{ 
    // Exercise 2 
    string word = ""; 
    string sentence = ""; 
    char next; 
    cout << "Type your sentence " << endl; 


    while(next != '.') 
    { 
     while(true) 
     { 
      cin.get(next); 

      if(isAlpha(next)) // If alphabet then add the char to word 
      { 
       word = word + next; 
      } 

      if(isAlpha(next) == false) // If not alphabet then put the char back and stop getting input 
      { 
       cin.putback(next); 
       break; 
      } 
     } 


     if(sexist(word)) // If word is sexist, replace word 
     { 
      word = replace(word); 
     } 
     sentence = (sentence + " " + word); // Tacking on words to the sentence 

     word = ""; // Resetting word 
    } 

    cout << "Word = " << word << endl; 
    cout << "Sentence = " << sentence << endl; 



    return 0; 
} 

bool isAlpha (char ch) 
{ 
    if(isalpha(ch)) 
    { 
     return true; 
    } 
    else return false; 
} 

bool sexist (string ch) 
{ 
    if(ch == "he" || ch == "she") 
    { 
     return true; 
    } 
    if(ch == "him" || ch == "her") 
    { 
     return true; 
    } 
    if(ch == "his" || ch == "hers") 
    { 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
} 
string replace (string ch) 
{ 
    if(ch == "he" || ch == "she") 
    { 
     ch = "he or she"; 
    } 
    if(ch == "him" || ch == "her") 
    { 
     ch = "him or her"; 
    } 
    if(ch == "his" || ch == "hers") 
    { 
     ch = "his or her(s)"; 
    } 
    return ch; 
} 

解釋我的代碼更多:我試圖在同一時間搶字,通過每次抓住一個字符,並改變是「性別歧視」到「中性」的任何話。抓住這個詞後,如果是性別歧視,我會改變它,否則我不會改變它,然後將它添加到「句子」字符串中。我想要附帶一段時間的最後一個單詞突破外部while循環並進入我的最終輸出行。

但嘗試不同的循環和不同的代碼後,我不能擺脫while循環。是因爲get命令嗎?我對C++非常陌生,所以我可能不瞭解一些基本規則。我曾嘗試在下一次檢測到句點時使用bool將外部while循環設置爲false。我已經嘗試使用goto命令來獲取循環外部的輸出。

+1

無法[複製](http://coliru.stacked-crooked.com/一/ 46c47cc209c18102)。當然,我已經修復了未初始化的「next」。 – LogicStuff

+0

作爲一個側面說明,很容易使用非性別他們/它們/他們不是累贅「他或她。」 –

+0

@SamiKuhmonen是啊肯定是有道理的,這只是我的實驗室提示的一部分。我在一個基本的C++課程。 –

回答

0

有很多問題,你的代碼(除了你的主要問題):

#include <cstdio> 
#include <stdio.h> 

cstdiostdio.h C++版本。將兩者都包含在內是一個錯誤 - 在需要包含它時第一次出現,但在這種情況下,您不需要在代碼中使用任何相關功能...

然後,爲什麼包含所有這些頭文件?不要使用大多..你只需要這些:

#include <iostream> // for cout/cin 
#include <cctype> // the c++ version of ctype.h, for std::isalpha 
#include <string> // for std::string 

也不要使用空間std!如果你需要理解爲什麼,你會在stackoverflow中找到許多相關的帖子。

然後,你並不需要一個isAplha函數,你自己的版本不會超過std::isalpha那麼爲什麼不直接使用它呢?

然後你有這樣的:

string replace (string ch); 
bool sexist (string ch); 

首先要注意的是,你是按值傳遞的std :: string,你真的不應該在這兩種情況下。在sexist的情況下,您應該通過引用來傳遞const。但你並不真的需要這兩種不同的功能..你是重複的代碼,我會做這樣的事情:

void replace (std::string& ch) { 
    if(ch == "he" || ch == "she") 
     ch = "he or she"; 
    else if(ch == "him" || ch == "her") 
     ch = "him or her"; 
    else if(ch == "his" || ch == "hers") 
     ch = "his or her(s)"; 
} 

,然後你可以重寫你的循環中更地道的風格,像這樣:

while(next != '.') { 
    while(std::cin.get(next)) { 
     if(std::isalpha(next)) 
    word = word + next; 
     else { 
     cin.putback(next); 
     break; 
     } 
    } 
    replace(word); 
    } 

有了這個,如果你只使用只有字母值和句點的輸入,你會沒事的。你不能逃避這個循環的原因是,當std::isalpha爲假時,你將放回該字符,以便下次重新讀取,這將導致程序在與std::isalpha相同的調用之間無限次地振盪。考慮到空間是不是一個字母字符..所以輸入如He is.將永遠振盪一旦空間達到..

(順便說一句,一旦我使用了一個調試器,我發現這個錯誤在30秒..使用一個DEBUGGER!它會簡化你的編碼生活)

爲了避免這種情況,你需要一種不同的方法來處理你的程序。我建議使用getline讀取行而不是字符,然後分析它們,你並不真正需要的字符,你的情況看性格,無論你希望多行輸入。讓一旁,我會認爲這是錯誤的期待您的客戶終止所有輸入的句子用了一段對你的程序行爲正確..

+0

你好水手,我相信你回答了我的問題一個又一個一個星期前,謝謝。你的回答一直是......睜開眼睛。我在我的代碼做了很多事情的原因是因爲我的老師是要求具體的指示(可能是因爲它是一個非常簡單並且比大多數人會使用這個程序更根本的)。我認爲輸入'.'會在它認爲是錯誤並退回角色之前退出循環。謝謝,我會嘗試所有的建議。 –