2017-09-26 921 views
0

我正在写一个C++程序,显示一个男人的2个输出,使它看起来像他在跳跃,如果你按下输入。如果输入'q',程序应该停止。这是我所得到的。按Enter键继续或q退出C++

//此程序将显示跳跃的人。

包括

包括

使用命名空间std;

int main() {
string user_input;

do 
{ 
    cout << endl << endl << endl << endl << endl << endl << endl 
     << endl << endl << endl << endl << endl << endl << endl 
     << endl << endl << endl << endl << endl << endl << endl 
     << endl << endl << endl << endl << endl << endl; 

    cout << " O" << endl; 
    cout << " /|\\" << endl; 
    cout << " ()" << endl; 
    cout << "------------------------------------------------------------" << endl; 

    cout << "Press ENTER to continue or enter q to quit:"; 

    getline(cin, user_input); 

    cout << endl << endl << endl << endl << endl << endl << endl 
     << endl << endl << endl << endl << endl << endl << endl 
     << endl << endl << endl << endl << endl << endl << endl 
     << endl << endl << endl << endl << endl << endl; 

    cout << " \\O/" << endl; 
    cout << " | " << endl; 
    cout << "/\\" << endl << endl; 
    cout << "------------------------------------------------------------" <<endl; 
    cout << "Press ENTER to continue or enter q to quit:"; 
} while(getline(cin, user_input)); 

return 0; 

}

我一直在尝试了几个小时,我仍然无法弄清楚如何,如果你输入q停止程序。

我已经试过while语句的变体,如

而(函数getline(CIN,USER_INPUT)& & USER_INPUT =! 'Q')

,但它不工作。任何帮助都将不胜感激。

回答

0

这似乎是作业,这意味着这不是问的地方。但我会认为它可能与没有任何if语句来检查用户给出的值有关。而你的另一个尝试失败了,因为getline在这种情况下获取整个换行符不仅仅是'q'。这是更多Getline keeps on getting newline character. How can I avoid this?。您可以替换您尝试使用的支票while(getline(cin,string)& & string!=“q \ n”) 编辑:我希望这是一个评论,但缺乏声望。

+0

这不是家庭作业,我只是想学习C++编码,而且在我正在学习的书的早期活动中。尽管谢谢你的帮助。 –

+0

好吧,你可能想要做的是重构你的循环,以便检查是不是像这样(从cin和检查读取)更像是:{循环代码..... getline(cin,user_input); bool input = user_input!=“q \ n”;} while(input)“\ n”是新行的转义字符,打印结果与endl相同,但包含在字符串“ – JoyStickFanatic

+0

”我不确定你的意思。或者更详细地描述一下? –

0

所以我终于在今天早上想通了,以为我会分享以供将来参考。

包括

包括

使用命名空间std;

INT主() {

串USER_INPUT; int counter = 0;

do 
{ 
    if (counter % 2 == 0) 
    { 
     cout << endl << endl << endl << endl << endl << endl << endl 
     << endl << endl << endl << endl << endl << endl << endl 
     << endl << endl << endl << endl << endl << endl << endl 
     << endl << endl << endl << endl << endl << endl; 

     cout << " O" << endl; 
     cout << " /|\\" << endl; 
     cout << " ()" << endl; 
     cout << "------------------------------------------------------------" << endl; 
    } 

    if (counter % 2 != 0) 
    { 
     cout << endl << endl << endl << endl << endl << endl << endl 
     << endl << endl << endl << endl << endl << endl << endl 
     << endl << endl << endl << endl << endl << endl << endl 
     << endl << endl << endl << endl << endl << endl; 

     cout << " \\O/" << endl; 
     cout << " | " << endl; 
     cout << "/\\" << endl << endl; 
     cout << "------------------------------------------------------------" <<endl; 
    } 

    cout << "Press ENTER to continue or enter q to quit:"; 
    getline (cin, user_input); 

    if (user_input == "q") 
{ 
    return 0; 
} 
counter = counter + 1; 
} while (user_input != "q"); 
return 0; 

如果循环重复,用int计数器表示,每个循环都加上一个循环,甚至程序显示该人站立。然后,显示提示“按ENTER继续或'q'退出:”。如果用户输入ENTER,getline(cin,user_input)会继续。如果用户输入'q',则循环中断(返回0;)。如果循环被重复,如果用户按下ENTER,则计数器现在是奇数,程序显示男子跳跃。