垃圾留在cin缓冲区 [英] junk left in cin buffer

查看:110
本文介绍了垃圾留在cin缓冲区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要防止垃圾留在缓冲区中,因为在用户输入的菜单调用的函数中使用开关盒菜单的值。



菜单代码

  void menu()
{
bool done = false;
string input;
while(!done)
{
cout< 欢迎来到DVD数据库。 << endl;
cout<< 1.添加A DVD< endl;
cout<< 2.删除DVD。 << endl;
cout<< 3.编辑DVD。 << endl;
cout<< 4.按类别列出。 << endl;
cout<< 5.通过标题的DVD检索。 << endl;
cout<< 6.按年份显示收集< endl;
cout<< 7.按标题显示收集< endl;
cout<< -999。退出程序< endl;
cout<< 请选择一个选项,方法是输入相应的数字< endl;
cin>>输入;
int value = atoi(input.c_str());
switch(value)
{
case 1:addDVD();打破;
case 2:deleteDVD();打破;
// case 3:editDVD();打破;
case 4:listByCategory(); break;
case 6:displayByYear(); break;
case 7:displayByTitle(); break;
case -999:writeToFile(); exit(0);打破;
default:cout<<Invalid entry<< endl;打破;
}
}
}

void retrieveByTitle()
{
string search;
int size = database.size();
int index = 0;
bool found = false;
cin.ignore();
cout< 请输入您要检索的DVD的标题:< endl;
getline(cin,search);
cout<搜索;
while(!found&& index< size)
{
if(database.at(index) - > getTitle()。compare(search)== 0)
{
cout<< database.at(index) - > toString();
break;
}
}
cout<< endl;
}

如果在菜单中输入5,程序跳过用户输入方法

解决方案

当处理交互式用户输入时,应该使用std :: getline p>

每当您点击< enter>时,std :: cin就会刷新到应用程序。所以这是你应该从用户读取数据的逻辑垃圾。

  std :: string answer; 
std :: cout<< 问题:\\\
;
std :: getline(std :: cin,answer);

这将为您提供用户在上一个问题回答中提供的一切。



一旦你有了输入,你应该得到你认为输入的值。一旦你有这个,你应该检查输入上是否有任何其他垃圾(如果有,然后中止和重试)否则验证你预期的数据。



如果你需要一个整数;

  std :: stringstream linestream(answer); 
int value;
std :: string junk;

if((answer>> value))&&& (!(answer>> junk)))
{
//如果你有数据
//并且没有垃圾,你现在可以去
}

在您的具体示例中,已经有一个简单的方法:

  std :: getline(std :: cin,input); 
int value = boost :: lexical_cast< int>(input); //如果没有$则抛出异常
//输入上有一个int(没有垃圾)


i need to prevent the junk left in the buffer as entering a value for a switch case menu from being used in a function called by the menu where their is user input.

menu code

void menu()
{
bool done = false; 
string input;
while(!done)
{
    cout << "Welcome to the DVD database." << endl;
    cout << "1. Add A DVD" << endl;
    cout << "2. Delete a DVD." << endl;
    cout << "3. Edit a DVD." << endl;
    cout << "4. List By Category." << endl;
    cout << "5. Retrieve by a DVD by Title." << endl;
    cout << "6. Display collection by year" << endl;
    cout << "7. Display collection by title" << endl;
    cout << "-999. Exit program" << endl;
    cout << "Please choose an option by entering the corresponding number" << endl;
    cin >> input;
    int value = atoi(input.c_str());
    switch(value)
    {
        case 1:addDVD(); break;
        case 2:deleteDVD(); break;
       // case 3:editDVD(); break;
        case 4:listByCategory();break;
        case 6:displayByYear();break;
        case 7:displayByTitle();break;
        case -999: writeToFile(); exit(0); break;
        default : cout <<"Invalid entry"<< endl; break;
    }
}
}

void retrieveByTitle()
{
string search;
int size = database.size();
int index = 0;
bool found = false;
cin.ignore();
cout << "Please enter the title of the DVD you would like to retrieve: " << endl;
getline(cin,search);
cout << search;
while(!found && index<size)
{
    if(database.at(index)->getTitle().compare(search)==0)
    {
        cout << database.at(index)->toString();
        break;
    }
}
cout << endl;
}

if 5 is entered in the menu, the program skips the user input in the method

解决方案

When dealing with interactive user input you should use std::getline()

The std::cin is flushed to the application every time you hit <enter>. So this is the logical junks you should read data from the user in.

std::string answer;
std::cout << "Question:\n";
std::getline(std::cin, answer);

This gets you everything the user provided in response to the previous question.

Once you have the input you should get the value you think is on the input. Once you have this you should check if there is any other junk on the input (if there is then abort and re-try) otherwise validate the data you expected.

If you were expected an integer;

std::stringstream linestream(answer);
int               value;
std::string       junk;

if ((answer >> value)) && (!(answer >> junk)))
{
    // If you got data
    // and there was no junk on the line you are now good to go
}

In your specific example there is already a simple way to do this:

std::getline(std::cin, input);
int value = boost::lexical_cast<int>(input);  // throws an exception if there is not
                                              // an int on the input (with no junk)

这篇关于垃圾留在cin缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆