0

スイッチケースメニューの値を入力するときにバッファーに残ったジャンクが、ユーザー入力であるメニューによって呼び出される関数で使用されないようにする必要があります。

メニューコード

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;
}

メニューに 5 が入力された場合、プログラムはメソッドでのユーザー入力をスキップします。

4

2 に答える 2

0

インタラクティブなユーザー入力を処理するときは、std :: getline()を使用する必要があります

<Enter>を押すたびに、std::cinがアプリケーションにフラッシュされます。したがって、これは、のユーザーからデータを読み取る必要がある論理的なジャンクです。

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

これにより、前の質問に答えてユーザーが提供したすべてのものが得られます。

入力を取得したら、入力にあると思う値を取得する必要があります。これを取得したら、入力に他のジャンクがないかどうかを確認する必要があります(ある場合は中止して再試行します)。そうでない場合は、期待するデータを検証します。

整数が期待された場合;

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
}

あなたの特定の例では、これを行う簡単な方法がすでにあります:

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)
于 2013-01-14T03:10:29.593 に答える
0

このコードは機能しますが、' cin.ignore() ' を削除すると、説明したのと同じ問題が発生します。これにより、cin >> 演算子によって無視される余分な区切り文字が削除されます。

#include <iostream>
#include <climits>
using namespace std;

int main() {
    string a, b;
    while (true) {

        cout << "write 'x' to exit: " << endl;
        cin >> a;
        if (a == "x") {
            break;
        }       
        cout << "read '" << a << "'" << endl;

        cout << "now write a line: " << endl;
        cin.clear();          // clears cin status
        cin.ignore(INT_MAX);  // clears existing, unprocessed input

        getline(cin, a);
        cout << "read '" << a << "'" << endl;
    }

    return 0;
}
于 2013-01-13T23:14:36.530 に答える