【问题标题】:C++ user input closes program debugC++ 用户输入关闭程序调试
【发布时间】:2014-07-03 10:04:42
【问题描述】:

我有一个 C++ 程序,它等待用户输入,只有在用户输入 'q' 时才会关闭,如果输入任何其他内容,它将循环返回菜单。

至少这是它应该做的。程序无论如何都会关闭,而不是循环返回。

int main()
{   
    Hierarchy roster;
    char input=' ';
    bool done = false;
    string first, last, full, boss, title;
    while (done != true){
    cout << "Organizational Chart builder, version 0.1" << endl;
    cout << "Please choose your next command: /n";
    cout << "    q   to quit the program " << endl;
    cout << "    a   to add a person " << endl;
    cout << "    c   to count the number of people under someone" << endl;
    cout << "    p   to print the hierarchy " << endl;
    cout << "    r   to remove someone from the hierarchy " << endl;

    cin >> input;

    switch(input)
    {
    case 'q':
        done = true;
        break;
    case 'a':

        cout << "Please enter the person't first name:  ";
        cin >> first;
        cout << "Please enter the person's last name:  ";
        cin >> last;
        cout << "Please enter the person's title";
        cin >> title;
        cout << "Please enter " + first + last +"'s boss. Please enter the full name and title.  If there is none, type none:";
        cin >> boss;
        if (boss == "none")
        roster.insert(full);
        else 
        roster.contains(boss);
        roster.insert(full);
        break;

    case'c':

        cout << "Enter the first name of the person you are searching for:   ";
        cin >> first;
        cout << "Enter the last name:  ";
        cin >> last;
        cout << "What is the person's title:";
        cin >> title;
        full = first + " " + last + " " + title;
        roster.contains(full);
        roster.countb(full);
        break;

    case 'p':
        roster.print();
        break;

    case 'r':

        cout << "Please enter the first, last, and title of the person you want removed:  ";
        cin >> full;
        roster.removeNode(full);
        break;

    }
  }
cout << "Program closed.  " << endl;
return 0;
    }

编辑:现在可以工作了。谢谢

【问题讨论】:

  • 你应该更好地缩进你的代码,这在这种情况下很有帮助。

标签: c++ debugging loops while-loop switch-statement


【解决方案1】:

您的 return 语句在您的 while 循环中。把它拿出来,你应该没事的。

即。 return 0;}} -> } return 0;}

【讨论】:

    猜你喜欢
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 2018-09-12
    • 1970-01-01
    • 1970-01-01
    • 2017-04-26
    • 2015-01-23
    • 1970-01-01
    相关资源
    最近更新 更多