gpt4 book ai didi

c++ - 多次询问用户输入

转载 作者:行者123 更新时间:2023-12-02 10:07:54 25 4
gpt4 key购买 nike

我目前正在编写一个程序,将普通文本转换为鲸鱼说话(如果您想知道鲸鱼说话是什么,它是将辅音与u和e翻倍的语句)

我成功编写了程序。但是我希望再次重复执行相同的过程,直到我们手动退出它为止。

为此,我使用了while循环,并使用exit作为变量。但是它没有像第二次那样按预期运行,它没有要求输入,而是显示了提示文本,并跳到了退出/不退出行。
您能告诉我程序有什么问题吗?这是我的代码(您可以尝试自己编译并执行):

#include <iostream>
#include <string>
#include <vector>

int main(){
bool exit = false;
//Open
std::cout<<"==========\n";
std::cout<<"WHALE TALK\n";
std::cout<<"==========\n";
std::cout<<"\n";
//main loop

while (!exit){

//Variables
std::vector<char> whaletalk;
//input
std::string input;
std::cout<<"Type the text you want to translate to whale language: ";
getline(std::cin,input);
std::cout<<"\n\nWhale talk: ";

//vowels
std::vector <char> vowels = {'a','e','i','o','u'};

//sorter
//iterating through string
for (int i = 0; i < input.size(); i++){
//iterating through vowels
for (int j = 0; j < vowels.size(); j++){
//case of vowels
if(input[i] == vowels[j]){
//case of u and e
if ((input[i] == 'u') or (input[i] == 'e')){
whaletalk.push_back(input[i]);
whaletalk.push_back(input[i]);
}
//case of vowels other than u and e
else {whaletalk.push_back(input[i]);}
}
}
}
//Output
for (int k = 0; k < whaletalk.size(); k++ ){

std::cout<<whaletalk[k];
}
std::cout<<"\n";
// exit/no exit
std::string response;
std::cout<<'\n';
std::cout<<"Do you have more to translate?(yes/no)\n\nYour response: ";
std::cin>>response;
if (response == "NO" or response == "no" or response == "No"){
exit = true;
}




}
}

Here is an image of the bug

最佳答案

当您使用>>运算符读取字符串时,它仅读取下一个单词(最多为空白字符),并将所有其他内容保留在输入流中,但是std::cin仍然阻塞直到换行(按Enter)。

std::string response;
std::cout << '\n';
std::cout << "Do you have more to translate?(yes/no)\n\nYour response: ";
std::cin >> response;
if (response == "NO" or response == "no" or response == "No") {
exit = true;


如果您只键入一个单词,这仅将换行符留在缓冲区中(如果您确实说了“ab”,那么它将保留“b \ n”,而 response将为“a”,而不是“ab”),因此下一个 std::getline将给您剩下的,在这种情况下为空行。
int main()
{
std::string str;
std::cout << "Enter word: ";
std::cin >> str;
std::cout << "Entered '" << str << "'" << std::endl;

std::cout << "Enter line: ";
std::getline(std::cin, str);
std::cout << "Entered '" << str << "'" << std::endl;
}

Enter word: aEntered 'a'Enter line: Entered ''


或者,如果您有空格字符:

Enter word: aa bbEntered 'aa'Enter line: Entered ' bb'


您可以仅在读取字符串响应时始终使用 std::getline,以避免意外丢失任何内容(例如“bb”)。
 std::string response;
std::cout << '\n';
std::cout << "Do you have more to translate?(yes/no)\n\nYour response: ";
std::getline(std::cin, response);
if (response == "NO" or response == "no" or response == "No") {
exit = true;

或在执行其他 >>操作之后,获取“剩余”内容并继续下一行。

另一种可能性是使用 std::istream.ignore()忽略剩余的输入(最大长度和定界符):
 std::string response;
std::cout << '\n';
std::cout << "Do you have more to translate?(yes/no)\n\nYour response: ";
std::cin >> response;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
if (response == "NO" or response == "no" or response == "No") {
exit = true;

在这种情况下, std::numeric_limits<std::streamsize>::max()表示任意数量,直至下一行 \n。如果用户输入说“no abc”,则“abc”将丢失。

关于c++ - 多次询问用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59265682/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com