-1

私はプロジェクトの第2フェーズにあり、プログラムをメニュー方式のアプリケーションに拡張して、.txtファイルにあるデータベースをクエリする必要があります。ですから、私の問題は、ループを永続的にすることができないということです。あるオプションから次のオプションに初期化すると、常に終了します。これが私のintメインである私のコードのスニペットです:

    int main ()
{
        char Q,q;
        char S,s;
        char task;
        string pathname;
        string z;
        int count=0;
        cout << "Welcome to Jason Rodriguez's Library Database." << endl;
        cout << "Please enter the name of the backup file: ";
        cin >> pathname;
        ifstream inFile(pathname.c_str());
        while(!inFile.eof())
        {
            getline(inFile,z);
            count++;
        }

    while (task != 'Q' || task != 'q') {

        cout << count << " records loaded successfully." << endl;
        cout << "Enter Q to (Q)uit, Search (A)uthor, Search (T)itle, (S)how All: ";
        cin >> task;
        if ((task == 'Q')||(task =='q'))
        {
            cout << "Program will now terminate";
            break;
        }
        else if ((task == 'S')||(task =='s'))
        {
            showAll (loadData (pathname));
            cout << endl;
            cout << "Enter Q to (Q)uit, Search (A)uthor, Search (T)itle, (S)how All: ";
            cin >> task;
        }

    }
}

これら2つに加えて、ループにさらに2つのオプションを追加する必要がありますが、最初に最初の2つを正しく機能させる必要があると考えました。その後、他の2つはプラグアンドチャグする必要があります。基本的に私がやろうとしていたのは、ユーザーがQまたはqを入力したら、プログラムを終了するということです。それ以外の場合、ユーザーがSまたはsを押した場合は、showall機能をアクティブにして、ワードの後で元のクエリに戻ります。しかし、それは機能していません。支援は大歓迎です。

4

1 に答える 1

0

メニューにはほとんどの場合ループが必要です。特に、ユーザーが正しい選択入力を入力する必要があるメニューはそうです。このような場合に最も適切なのはwhileループですが、基本的には他のループバリアントを使用できます。

アップデート:

int main ()
{
    char task;//this is the only char needed. Your other chars were redundant
    string pathname;
    string temp;//I changed z to temp to better reflect its purpose
    int count=0;

    cout << "Welcome to Jason Rodriguez's Library Database." << endl;
    cout << "Please enter the name of the backup file: ";
    cin >> pathname;

    ifstream inFile(pathname.c_str());//this is potentially a problem in that you aren't verifying that the pathname is a valid one

    //you did not check to see that your file was open, otherwise there is no way to tell that you successfully opened the file
    if (inFile.is_open()) {
        //while(!inFile.eof()) is a character by character read and comparison
        //made your life easier by shortening it down to this - which ensures
        //that a line is read. (Much faster and more readable)
        while(getline(inFile,temp)) 
        {
            count++;
        }
        inFile.close();//always close a file after you've used it

        //At this point the entire file has been read. So, this is where this message SHOULD be 
        cout << count << " records loaded successfully." << endl;
    }
    else {
        //if there was an error opening the file (i.e. wrong path, or it simply does not exist), this will be displayed
        cout << "There was a problem opening your file" << endl;
        exit(0);//and the program will terminate
    }

    while (task != 'Q' || task != 'q') {
        cout << "Enter Q to (Q)uit, Search (A)uthor, Search (T)itle, (S)how All: ";
        cin >> task;
        if ((task == 'Q')||(task =='q'))
        {
            cout << "Program will now terminate";
            break;
        }
        else if ((task == 'S')||(task =='s'))
        {
            string author;
            //showAll (loadData (pathname));
            cout << endl;
            cout << "Search an Author" << endl;
            cin >> author;//get the author name to search from the user

            //write code to search an author here
        }
    }
}

あなたが投稿したコードにはいくつかの問題がありますが、簡潔にするために省略します。したがって、次の点に注意してください。

コードはオプションごとに同じメッセージを出力していました(終了を除く)。もちろん、それは機能しなかったように見えます。各オプションは異なるタスクです。各タスクが行うことを印刷します(私が行ったことと同様)。

  1. ファイルで作成者を検索したいのですが、保存していません。あなたのインストラクターをなだめるそれを保存する方法を調べてください。

  2. switchコードの複雑さが増していることを考えると、この場合に使用するのが理想的です。

  3. 各タスクを関数に分割し、それらを呼び出してメイン関数を読みやすくしてみてください。実際、メイン関数をできるだけ小さくすることは、プログラミングの良い習慣です。

そして、juanchopanzaが非常に正しく指摘しているように、C++にはいくつかの根本的な問題があります。さらにいくつかの演習を行って、優れたC++の本からさらに多くの例を実行してみてください。

于 2013-03-18T07:23:00.397 に答える