2013-04-24 66 views
3

所以它經常出現在我的課程中,我需要能夠有一個程序,用戶可以多次執行任務。我應該如何寫一個雄辯的繼續?

所以我喜歡寫東西

boolean continue=true; 

while (continue) { 
    //do code 
    boolean isAnswerGood=false; 
    while (!isAnswerGood) { 
     cout << "Do you wish to continue" << endl; 
     string answer; 
     getline(cin, answer); 
     if (answer=="y") //or something else 
      isAnswerGood=true; 
     else if (answer=="n") { 
      isAnswerGood=true; 
      continue=false; 
     } else 
      cout << "That wasnt "y" or "n" please type something again" << endl; 
    } 
} 

這看起來臃腫的東西很多代碼那麼簡單。我願意在這個盒子外面思考,所以如果任何人都可以給我一個線索,以更好地解決這個問題,身份證欣賞它。

+3

我會避免使用單詞'continue'一個變量名,因爲它是在類似C語言的關鍵字。 '重試'會更好。 – Patashu 2013-04-24 00:49:26

+0

這是什麼語言?有可能會有語言結構解開這個問題。我沒有看到它是如何被「無法使用」的。您可以移動代碼,也許使用布爾邏輯而不是if/then,但這實際上是一個編碼器偏好,並且可能不會在您的代碼中受支持。即如果你的語言支持它,你可以在主循環之外加一個continue =(answer!=「n」);但是我不知道你會同意它更簡單。我認爲這是我自己。 – 2013-04-24 01:13:00

+0

這裏的許多答案似乎認爲你想要做很多次的事情。你的代碼只想做一件事,但想要與用戶覈對,直到他們輸入正確的迴應。 – 2013-04-24 07:57:00

回答

3

對不起,但這是關於正確的代碼量。與其他一些語言相比,C和C++相對比較冗長。

但是,如果這是一個常見問題,您可以將其抽象出來,並從中創建一個函數。該功能將是非常相似,你已經有了,但你會說它是這樣的:

boolean again = true; 
while (again) { 
    // do code 
    again = ask_user("Do you wish to continue?", "y", "n"); 
} 

ask_user()的參數應該問這樣的問題,接受答案的意思是「是的」,接受這個答案意味着「不」。錯誤消息(關於意外輸入)可以使用第二個和第三個參數,因此我們並不需要將其傳入。

當然,問題可能會比這更復雜...如果您的代碼將被非英語用戶使用,該怎麼辦?如果您需要處理本地化,則可以創建一個基本函數,其中包含所有字符串(包括錯誤消息),然後製作用戶使用語言規範調用的包裝器。下面是一個示例,這次使用C++中的「無限循環」:

for (;;) { 
    // do code 
    if (!ask_user_yesno("en_us")) // locale: English, USA 
     break; 
} 
+0

continue是一個關鍵字。第一塊代碼不會編譯,至少不會與gcc一起編譯。 – Matt 2013-04-24 01:15:49

+0

@Matt,你是對的。我只是用這個詞,因爲它是在這個例子中,但現在你指出了我改變了代碼。 – steveha 2013-04-24 07:20:33

4

將其分解爲單獨的函數。 (幾乎總是回答 「我怎麼寫雄辯的...?」)

例如:

do { 
    // something 
} while (UserWantsMore()); 

///////// 

bool UserWantsMore() { 
    std::string answer = GetAnswer("Continue?"); 
    while (!GoodAnswer(answer)) 
    answer = GetAnswer("Please answer 'yes' or 'no'!"); 
    return IsAnswerYes(answer); 
} 

bool GoodAnswer(const std::string& answer) { 
    return !answer.empty() && (answer[0] == 'y' || answer[0] == 'n'); 
} 

bool IsAnswerYes(const std::string& answer) { 
    return !answer.empty() && answer[0] == 'y'; 
} 

std::string GetAnswer(const char* prompt) { 
    std::cout << prompt << std::cend; 
    std::string answer; 
    std::getline(std::cin, answer); 
    return answer; 
} 
+0

完全矯枉過正的男人!這實際上使得閱讀比簡單的情況更難。 – Matt 2013-04-24 01:03:32

+0

閱讀起來可能會稍微困難一點,但它更易於理解,因爲函數名稱可以很好地指示作者的意圖。這使得錯誤更容易被發現。 – 2013-04-24 05:16:11

0

如何像:

void DoYesCode() 
{ 
    // Do the code for the yes stuff... 
} 

... 

do{ 
    cout << "Do you wish to continue" << endl; 
    string answer; 
    getline(cin, answer); 
    if (answer=="y") 
     DoYesCode(); 
    else if (answer=="n") 
     break; 
    else 
     cout << "That wasnt 'y' or 'n' please type something again" << endl; 
} while(true); 
2

如果你只是想爲了讓你已經用少一點的代碼做同樣的事情,這將會起作用。

string answer="y"; 

while (answer=="y") { 
    //do code 
    for(;;) { 
     cout << "Do you wish to continue" << endl; 
     getline(cin, answer); 
     if ((answer=="y")||(answer=="n")) break; 
     cout << "That wasnt \"y\" or \"n\" please type something again" << endl; 
    } 
} 

略少於代碼,但稍微混淆:

string answer="y"; 

while (answer!="n") { 
    if (answer=="y") { 
     //do code 
    } else { 
     cout << "That wasnt \"y\" or \"n\" please type something again" << endl; 
    } 
    cout << "Do you wish to continue" << endl; 
    getline(cin, answer); 
} 

下面是一個使用<termios.h>得到答案的一個版本。它使用更多的代碼,但表現更「雄辯」。

int getch (void) 
{ 
    int c; 
    struct termios oldt; 
    struct termios newt; 
    tcgetattr(STDIN_FILENO, &oldt); 
    newt = oldt; 
    newt.c_lflag &= ~ICANON; 
    tcsetattr(STDIN_FILENO, TCSANOW, &newt); 
    c = getchar(); 
    tcsetattr(STDIN_FILENO, TCSANOW, &oldt); 
    return c; 
} 

bool again (std::string prompt, std::string yes, std::string no) 
{ 
    bool doyes = false; 
    bool dono = false; 
    for (;;) { 
     std::cout << prompt; 
     int c = getch(); 
     std::cout << std::endl; 
     doyes = (yes.find(c) != yes.npos); 
     dono = (no.find(c) != no.npos); 
     if (doyes || dono) break; 
     std::cout << "Type [" << yes << "] for yes, or [" << no << "] for no."; 
     std::cout << std::endl; 
    } 
    return doyes; 
} 

你可以使用它作爲其他人所說:

do { 
    // the interesting code 
} while (again("Do you wish to continue? ", "y", "n"));