Выход из-под контроля - PullRequest
0 голосов
/ 23 марта 2019

Я пытаюсь создать набор функций, и все работает нормально, за исключением того, что мое меню повторяется несколько раз после того, как пользователь выбирает опции 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 раз

.
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...