【问题标题】:else if looping quandryelse if 循环 qudry
【发布时间】:2013-03-18 07:18:48
【问题描述】:

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

    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 功能,然后返回原始查询。但它不起作用。欢迎并感谢您提供帮助。

【问题讨论】:

  • 我在这里看不到循环。此代码是否在您真实代码的循环中?如果是这样,请删除 return 0
  • 我知道我没有循环。那是我的困惑。我正在尝试在这种情况下创建一个,但我不知道我哪里出错了。我把 return 0 拿走了,但它什么也没做
  • 呃,最后} 关闭是什么?我的语法检查器快疯了……
  • 你的循环在我看来确实是永恒的。第二个while 中的条件始终为真。 (第一个while 也是错误的。你永远不会在inFile.eof() 上循环。)
  • 你的外部|| 应该是&amp;&amp;。 (所有字符都不等于“q”或“Q”。)

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


【解决方案1】:

菜单几乎总是需要循环 - 尤其是需要用户输入正确选择输入的菜单。在这种情况下,最适用的是 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++ 书籍中做更多的例子。

【讨论】:

  • 宾果游戏,谢谢。我一直盯着这个屏幕太久了,错过了那个选项。我想我会去商店散散步,呼吸一下新鲜空气,然后把这个坏孩子干掉。感谢您的帮助!
  • *这个屏幕是我的xcode窗口,不是这个网站的页面哈哈
  • 我说得太早了。此解决方案不起作用。我碰到了一个障碍。我将编辑我的帖子以显示我的整个 int main() 以便您了解我的意思。
  • 解决方案不起作用,因为您发布了问题的部分,而不是全部。话虽如此,我已经编辑了我的原始帖子以回答您更新的问题,@Jason。
猜你喜欢
  • 2015-10-28
  • 1970-01-01
  • 1970-01-01
  • 2021-09-16
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-02
相关资源
最近更新 更多