否则如果循环窘迫

时间:2013-03-18 07:18:48

标签: c++ for-loop if-statement

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

    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功能并在病房后,返回原始查询。虽然不行。欢迎并表示赞赏。

1 个答案:

答案 0 :(得分: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. 尝试将每个任务分解为函数,并调用它们以使主函数可读。事实上,对于你的主要功能来说,尽可能小的编程实践是很好的。

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