-1

我处于项目的第二阶段,我需要将程序扩展为菜单驱动的应用程序,以查询我在 .txt 文件中拥有的数据库。所以,我的麻烦是我不能让我的循环是永久的。它总是在从一个选项初始化到下一个选项时终止。这是我的 int main 代码片段:

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

    }
}

我需要在这两个之上的循环中再添加两个选项,但我认为我应该首先让前两个正常工作。之后其他两个应该是即插即用。基本上我想做的是说如果用户输入 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. 尝试将每个任务分解为函数,并调用它们以使您的主函数可读。事实上,让你的 main 函数尽可能小是一种很好的编程习惯。

而且,正如 juanchopanza 非常正确地指出的那样:C++ 存在一些基本问题。尝试做更多的练习,并从一本好的 C++ 书中做更多的例子。

于 2013-03-18T07:23:00.397 回答