Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;quit()函数中可能存在的范围问题_C++_Scope - Fatal编程技术网

C++ C++;quit()函数中可能存在的范围问题

C++ C++;quit()函数中可能存在的范围问题,c++,scope,C++,Scope,尝试声明并初始化选择,使其具有除“Q”之外的另一个值,并返回该值,以便do while循环继续运行。我似乎无法从名为quit()的函数内部更改选择变量的值。选择变量在get_selection()函数中定义。我还是很新的编码和C++可以帮助我吗?我不知道当有人输入'q'或'q'退出循环时,如何更改selection的值以保持do while循环运行。下面是源代码。这是我的第一个堆栈溢出问题。如果我问这个问题的方式不好,请给出建设性的反馈,以便我从中吸取教训,下次做得更好。非常感谢你抽出时间,希望

尝试声明并初始化选择,使其具有除“Q”之外的另一个值,并返回该值,以便do while循环继续运行。我似乎无法从名为quit()的函数内部更改选择变量的值。选择变量在get_selection()函数中定义。我还是很新的编码和C++可以帮助我吗?我不知道当有人输入'q'或'q'退出循环时,如何更改selection的值以保持do while循环运行。下面是源代码。这是我的第一个堆栈溢出问题。如果我问这个问题的方式不好,请给出建设性的反馈,以便我从中吸取教训,下次做得更好。非常感谢你抽出时间,希望你有一个幸福的一天

#include <iostream>
#include <vector>

using namespace std;

// Function Prototypes
void display_menu();
char get_selection();
void print_numbers(const vector<int> &list);
void add_number(vector<int> &list);
void remove_number(vector<int> &list);
void display_mean(const vector<int> &list);
void smallest_number(const vector<int> &list);
void largest_number(const vector<int> &list);
char quit();
void handle_unknown();
// End of Function Prototypes

void display_menu() {
    cout << "-------------------------------------------\n";
    cout << "Please select one of the following choices:|\n ";
    cout << "P - Print Numbers                         |\n ";
    cout << "A - Add a Number                          |\n ";
    cout << "R - Remove a Number                       |\n";
    cout << " M - Display Mean of the Numbers           |\n ";
    cout << "S - Display the Smallest Number           |\n ";
    cout << "L - Display the Largest Number            |\n ";
    cout << "Q - Quit                                  |\n ";
    cout << "-------------------------------------------\n";
    cout << "\nEnter Selection Here: ";
}

char get_selection() {
    char selection {};
    cin >> selection;
    return toupper(selection);
}

void print_numbers(const vector<int> &list) {
     // if the User selects P it will display the list within brackets
    cout << "\n============================================\n"; 
    cout << "\n[ ";
    for ( auto index : list) {
        cout<< index << " ";
    }
    cout << "]";
    cout << "\n\n============================================\n\n";
}

void add_number(vector<int> &list) {
    // if the user selects A it will Add a Number to the list
    int number {};
    cout << "\n==========================================================\n\n";
    cout << "- Please enter a number to be ADDED to the list: ";
    cin >> number;
    list.push_back(number);
    cout << "- The number " << number << " was ADDED to the list\n\n";
    cout << "==========================================================\n\n";
}

void remove_number(vector<int> &list) {
    // if the user selects R it will REMOVE a Number from the list
    char choice {};
    cout << "\n- If you select Yes the last number you added to the list will be REMOVED from the list(Y/N): ";
    cin >> choice;
    if ( (choice == 'Y' || choice == 'y') && list.size() == 0) {
        cout << "- Sorry, but there is currently nothing in the list to remove.\n\n";
        cout << "==========================================================================================================\n\n";
    }
    else if (choice == 'Y' || choice == 'y'){
        list.pop_back();
        cout<< "- The last number you entered was removed from the list\n\n";
        cout << "==========================================================================================================\n\n";
    }
    else if (choice == 'N' || choice == 'n') {
        cout << "- The number survived the purge!!!\n\n";
        cout << "==========================================================================================================\n\n";
    }
    else {
        cout << "- You did not enter a valid response, no action was taken\n\n";
        cout << "==========================================================================================================\n\n";
    }
}

void display_mean(const vector<int> &list) {
    // if the user selects M it will display the mean of the numbers
    cout << "\n===============================================================\n\n";
    int sum {}; //all the integers in the list added together
    double mean {}; // the sum / list.size()
    for ( auto integer : list) {
        sum = sum + integer;
    }
    mean = static_cast<double>(sum) / list.size();
    cout << "- The Sum of all the numbers in the list is: " << sum << endl;
    cout << "- The Mean of all the numbers in the list is: " << mean << endl;
    cout << "\n===============================================================\n";
}

void smallest_number(const vector<int> &list) {
    // Displays the smallest number
    cout << "\n=========================================================\n\n";
    int min = list.at(0);
    for ( auto nums : list ) {
        if ( nums < min) {
            min = nums;
        }
    } 
   cout << "- The Min in the list is: " << min << endl;
   cout << "\n=========================================================\n\n";
}


void largest_number(const vector<int> &list) {
    // Displays the largest number
    cout << "\n=========================================================\n\n";
    int max = list.at(0);
    for ( auto nums : list ) {
        if ( nums > max) {
           max = nums;
        }
    } 
    cout << "- The Max in the list is: " << max << endl;
    cout << "\n=========================================================\n\n";
}

char quit() {
   // Are you sure you want to quit prompt
    char quit_prompt {};
    cout << "\n==============================================\n\n";
    cout << "Are you sure you want to quit (Y/N)? ";
    cin >> quit_prompt;
    if ( quit_prompt == 'Y' || quit_prompt == 'y') {
        cout << "Have a blessed day!!!" << endl;
        cout << "\n==============================================\n\n";
        return 'a';
    }
    else if ( quit_prompt == 'N' || quit_prompt == 'n') {
        cout << "Choose a option\n";
        cout << "\n==============================================\n\n";
        display_menu();
        return get_selection();

    }
    else if ( quit_prompt == 'Q' || quit_prompt == 'q') {
        cout << "Have a blessed day!!!\n";
        cout << "\n==============================================\n\n";
        return 'a';
    }
    else {
        cout << "You entered a invalid response, no action was taken\n";
        cout << "\n==============================================\n\n";
        display_menu();
        return get_selection();
    }
}

void handle_unknown() {
    cout << "Unknown selection - try again" << endl;
}

int main() {

    vector<int> list {1,2,3}; //have to create the list outside of the loop otherwise the loop will 
reset the list every single time the loop resets (lesson learned)
        // user input a character that will get saved to this selection variable
        char selection {};
        // run this loop unless the user inputs 'Q' or 'q'
        do {
        // tells the user to enter a valid choice if they don't
        display_menu();
        selection = get_selection();
        switch (selection) {
            case 'P':
                print_numbers(list);
                break;
            case 'A':
                add_number(list);
                break;
            case 'R':
                remove_number(list);
                break;
            case 'M':
                display_mean(list);
                break;
            case 'L':
                largest_number(list);
                break;
            case 'S':
                smallest_number(list);
                break;
            case 'Q':
                quit();
                break;
            default:
                handle_unknown();
        }
    } while ( selection != 'Q' ); // using the && logical statement because of the != comparison.

    return 0; 
}
#包括
#包括
使用名称空间std;
//功能原型
无效显示菜单();
char get_selection();
无效打印编号(常量向量和列表);
无效添加编号(矢量和列表);
作废删除编号(矢量和列表);
无效显示平均值(常数向量和列表);
无效最小_数(常量向量和列表);
无效最大_数(常量向量和列表);
char quit();
无效句柄_未知();
//功能结束原型
无效显示菜单(){

不能从
quit
函数返回一个值,但不能在
main
函数中保存或执行该值

您可能不应该从
quit
函数返回值,也不应该像在
main
函数中那样显示菜单或获取选择

void quit() {
   // Are you sure you want to quit prompt
    char quit_prompt {};
    cout << "\n==============================================\n\n";
    cout << "Are you sure you want to quit (Y/N)? ";
    cin >> quit_prompt;
    if ( quit_prompt == 'Y' || quit_prompt == 'y' ||
         quit_prompt == 'Q' || quit_prompt == 'q') {
        cout << "Have a blessed day!!!" << endl;
        cout << "\n==============================================\n\n";
        exit(0);  // Exit the program
    }
    else if ( quit_prompt == 'N' || quit_prompt == 'n') {
        // Do nothing
    }
    else {
        cout << "You entered a invalid response, no action was taken\n";
        cout << "\n==============================================\n\n";
    }
}
void quit(){
//确实要退出提示吗
字符退出提示{};
无法退出提示;
如果(退出提示='Y'| |退出提示=='Y'||
退出提示=='Q'| |退出提示=='Q'){

我是否刚刚意识到我可能应该只显示get_selection()、quit()和main()函数。quit()函数是问题所在,因为它不会更改get_selection()函数中选择变量的值,以保持do while循环在main()中运行每当有人从键盘输入'q'或'q'时,我都会使用此函数。
我似乎无法从名为quit()的函数内部更改选择变量的值.
您想更改哪个变量?
退出提示
?只是一个注释:当您知道问题是关于退出函数时,最好保留不相关的函数(最小值/最大值/显示值/删除值)在示例程序之外。你不需要发布整个程序来解决问题。问一个简明而中肯的问题将有助于你在SO中更快地得到答案。