失控循环

时间:2019-03-23 17:45:58

标签: visual-c++

我正在尝试创建功能集合,并且一切正常,除了在用户选择选项2-4之后我的菜单多次重复之外。有时它将使菜单循环多达20次

#include "pch.h" 
#include <iostream>
#include <string>

using namespace std;

/*
* menu
* Display a menu
* Accepts no parameters
* Returns the character entered by the user.
*/
char menu() {
// Display a menu
std::cout << "=== 1)Indicates Normal 2)Indicates Caution 3)Indicates an 
Emergancy 4)Indecates you would like to Quit ===\n";
// Return the char entered by user
char ch;
std::cin >> ch;
return ch;
}
// Displays a text on the screen after the user selects the number 1 
indicating things are normal
void normal_screen() { 
std::cout << "Great job not messing up! All systems are normal.\n";
system("pause");
}
// Displays a text on the screen after the user selects the number 2 
indicating caution
void caution_screen() {
std::cout << "You made a mess of things but this caution warrning is not 
the end of the world.\n";
system("pause");
}
//Displays a text on the screen after the user selects the number 3 
indicating an emergency
void emergancy_screen() {
std::cout << "Emergency protocols have been activated. This computer will 
self-destruct in five seconds.\n";
system("pause");
std::cout << "4 seconds.\n";
system("pause");
std::cout << "3 seconds.\n";
system("pause");
std::cout << "2 seconds.\n";
system("pause");
std::cout << "Just kidding. Have a nice day.\n";
system("pause");
}
// Main
// Runs a command loop that allows users to answer the questions asked
int main() {
bool done = false; // Initially not done
char answer = 'p';
do {
    switch (menu()) {
    case '1': // Normal
        normal_screen();
        break;
    case '2': // Caution
        std::cout << "Caution! Caution! Please indicate what you have 
done:  ";
        std::cin >> answer ;
        caution_screen();
        break;
    case '3': // Emergancy
        std::cout << "Danger! Danger! Please indicate what you have done:  
";
        std::cin >> answer ;
        emergancy_screen();
        break;
    case '4': // Quit the program
        std::cout << "Are you really a quiter who wants to quit this 
program?";
        done = true;
        break;
    }
} while (!done);
}

我期望的结果是菜单重复一次,实际结果菜单最多重复17次

0 个答案:

没有答案