【问题标题】:How to fix string user input without using cin.ignore() and clear()-- C++? [duplicate]如何在不使用 cin.ignore() 和 clear()--C++ 的情况下修复字符串用户输入? [复制]
【发布时间】:2016-05-16 02:25:21
【问题描述】:

这里是初级程序员,我遇到了字符串输入问题 程序的结构——我无法成功存储字符串 年份、流派和导演的用户输入。我尝试不使用 cin >> with getline,只使用 get() 和 getline() - 但错误仍然存​​在 字符串输入未正确存储。 EX:如果用户输入选项“B”, 并将输入的年份字符串“2015”存储到字符串变量whatYear中,它 导致未正确存储的错误。如何解决?

#include <iostream>
#include <cctype>
#include <string>

void printYear();
void printGenre();
void printDirector();

using namespace std;

int main(){

char choice;

用户将输入一个选项:“B”代表年份,“C”代表流派,“D”代表导演。

cout << "Hello movie'goer, please select one of the following choices:\n\n"
 << "B - Display movies in a specific year\n"
 << "C - Display movies in a specific genre\n"
 << "D - Display movies from a specific director\n\n"
 << "Enter your choice: ";

choice = cin.get();

//Input validation
while (toupper(choice) != 'B' && toupper(choice) != 'C' &&
   toupper(choice) != 'D') {

cout << "Choice is invalid, the choice must be one of the following:'B',   'C', or"
     << " 'D'. Re-enter your choice: ";
 choice = cin.get();
}

cout << endl << endl;

这是它处理选择的地方。 “B”将打印字符串年份输入,“C”将打印字符串流派输入,“D”将打印字符串导演姓名输入。

switch(choice)
{
case 'a':
case 'A': break;
case 'b':
case 'B': printYear(); break;
case 'c':
case 'C': printGenre(); break;
case 'd':
case 'D': printDirector(); break;
}

}

当为年份输入选项“B”时,我无法将年份输入存储到字符串变量 whatYear 中。

void printYear()
{
string whatYear;
cout << "Enter a specific year: " << endl;
getline(cin, whatYear);

我检查了字符串输入是否存储成功,但是失败了。

cout << "The input stored in string whatYear is: " << whatYear;
}

我在这里也遇到了同样的问题,字符串输入没有成功存储到字符串变量 whatGenre 中。

 void printGenre()
{
string whatGenre;
cout << "Enter a specific genre: " << endl;
getline(cin, whatGenre);

cout 语句在此处显示不正确。

cout << "The input stored in string whatGenre is: " << whatGenre;
}

其他两个函数也有同样的问题。

void printDirector()
{
string whatDirector;
cout << "Enter a specific director " << endl;
getline(cin, whatDirector);

cout << endl << "The input stored in string whatGenre is: " << whatDirector;
}

【问题讨论】:

    标签: c++ string function vector input


    【解决方案1】:

    除了不将operator&gt;&gt;getline() 一起使用之外,也不要将get()getline() 一起使用,原因相同。只需始终使用getline() 来处理交互式输入,以避免这种混乱。

    当您使用get() 读取菜单选项并输入“B”后输入“B”时,get() 自愿使用“B”字符,但换行符仍未读取,随后的getline() 将其处理为空字符串,而不是处理输入的年份。

    始终始终如一地使用getline() 以交互方式阅读std::cin

    【讨论】:

      【解决方案2】:

      一种可能的解释是cin.get() 函数在您键入两个字符(字母并回车)时只读取一个字符。 第一个存储在 choice 中,而第二个保存在缓冲区中,并为您的 getline() 调用发出一个空行。

      在使用fflush(stdin); 发出getline 命令之前尝试清理缓冲区

      更糟糕的是:某些操作系统(MS windows - 我在看着你)在您单击 Enter 时使用两个字符:\r 和 \n

      【讨论】:

        【解决方案3】:

        好的,使用 get() 和 getline() 是个问题。我的问题已通过用 getline() 替换所有内容并将 get() 转换为将菜单选项存储为 char 数组以使用 cin.getline() 得到解决。 感谢所有回答者的帮助,@Sam Varshavchik @kiwi

        【讨论】:

        • 请注意,Stack Overflow 并不是作为解决您个人问题的帮助台,而是作为常见问题解答之类的资源,以供将来研究。您之前曾多次询问过您的问题,其中最受好评的问答对之一已经包含了答案。因此,我将您的问题标记为重复。
        猜你喜欢
        • 2015-02-13
        • 1970-01-01
        • 1970-01-01
        • 2022-01-25
        • 2019-12-18
        • 2021-06-11
        • 1970-01-01
        • 2019-06-15
        • 1970-01-01
        相关资源
        最近更新 更多