2016-02-07 24 views
-3
enter code here 
int main() 
{ 

std::string input; 
std::cin >> input; 

while (input != "quit") 
{ 
// do stuff 
std::cin >> input; // get another input 

} 

return EXIT_SUCCESS; // if we get here the input was quit 

問題是,它不會提示用戶輸入文字。如果我輸入「退出」,它結束,所以這工作正常。否則,如果我輸入其他任何內容,則輸入quit,它也會退出。我應該怎麼做才能糾正這個問題?需要一個句子並輸出元音,輔音等元素的程序

通過我的研究,我在這裏找到了一個類似的程序,它使用大小寫,但對我來說似乎有點乏味。我被指示使用isalpha函數,該函數接受單個字符作爲參數,並返回布爾指示符,以確定該字符是否是字母。

+2

請讓你的標題與您無關,實際上是試圖問題問(不管可能如何)。 – juanchopanza

+1

需要在某處使用'std :: cout' - 我猜 –

+0

而不是'cin'嘗試使用'getline(..)'。 – Poriferous

回答

0

試試這個小程序說明:

#include <iostream> 
#include <cstdlib> 

int main(void) 
{ 
    std::cout << "This is a prompt, enter some text:\n"; 
    std::string the_text; 
    std::getline(std::cout, the_text); // Input the text. 
    std::cout << "\n" 
      << "The text you entered:\n"; 
    std::cout << the_text; 
    std::cout << "\n"; 

    // Pause the program, if necessary. 
    std::cout << "\n\nPaused. Press Enter to continue...\n"; 
    std::cin.ignore(10000000, '\n'); 

    // Return status to the Operating System 
    return EXIT_SUCCESS; 
} 

正如你可以看到,之前的輸入被稱爲提示用戶輸出指示用語。

編輯1:在while循環
在你的情況提示,需要輸入之前提示用戶:

while (input != "quit") 
{ 
    // Do stuff 

    std::cout << "Enter text or \"quit\" to quit: "; 
    std::cout.flush(); // Flush buffers to get the text on the screen. 
    std::cin >> input; 
} 
相關問題