否则会陷入循环困境

else if looping quandry

本文关键字:循环 困境      更新时间:2023-10-16

我在一个项目的第二阶段,我需要扩展我的程序到一个菜单驱动的应用程序来查询数据库,我有一个。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功能,然后返回到原始查询。但这行不通。欢迎协助并感谢。

菜单几乎总是需要循环 - 特别是需要用户输入正确的选择输入的菜单。在这种情况下,最适用的循环是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
        }
    }
}

你发布的代码有很多问题,为了简洁起见,我将放弃。因此,请注意以下内容:

你的代码对每个选项打印相同的消息(除了quit)。当然,它看起来不工作。每个选项都是一个不同的任务。打印每个任务所做的(类似于我所做的)。

  1. 您希望在文件中搜索作者,但是您还没有存储它。寻找一种能让你的老师满意的存储方法。

  2. 考虑到代码日益增加的复杂性,在这种情况下使用switch是最理想的。

  3. 尝试将每个任务分解为函数,并调用它们以使主函数可读。实际上,main函数越小越好,这是一个很好的编程习惯。

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