C++quit()函数中可能存在作用域问题
C++ Possible scope issue in the quit() function
尝试声明和初始化选择以具有除"Q"之外的另一个值,并返回该值,以便do-white循环继续运行。我似乎无法从名为quit((的函数内部更改选择变量的值。选择变量是在get_selection((函数中定义的。我对编码和C++还很陌生,有人能帮我吗?我不知道一旦有人输入"q"或"q"退出循环,如何更改select的值以保持do-white循环运行。源代码如下。这是我的第一个堆栈溢出问题。如果我问这个问题的方式不好,请给予建设性的反馈,以便我从中吸取教训,下次做得更好。非常感谢你抽出时间,希望你有一个幸福的一天。
#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 << "nn============================================nn";
}
void add_number(vector<int> &list) {
// if the user selects A it will Add a Number to the list
int number {};
cout << "n==========================================================nn";
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 listnn";
cout << "==========================================================nn";
}
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.nn";
cout << "==========================================================================================================nn";
}
else if (choice == 'Y' || choice == 'y'){
list.pop_back();
cout<< "- The last number you entered was removed from the listnn";
cout << "==========================================================================================================nn";
}
else if (choice == 'N' || choice == 'n') {
cout << "- The number survived the purge!!!nn";
cout << "==========================================================================================================nn";
}
else {
cout << "- You did not enter a valid response, no action was takennn";
cout << "==========================================================================================================nn";
}
}
void display_mean(const vector<int> &list) {
// if the user selects M it will display the mean of the numbers
cout << "n===============================================================nn";
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=========================================================nn";
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=========================================================nn";
}
void largest_number(const vector<int> &list) {
// Displays the largest number
cout << "n=========================================================nn";
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=========================================================nn";
}
char quit() {
// Are you sure you want to quit prompt
char quit_prompt {};
cout << "n==============================================nn";
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==============================================nn";
return 'a';
}
else if ( quit_prompt == 'N' || quit_prompt == 'n') {
cout << "Choose a optionn";
cout << "n==============================================nn";
display_menu();
return get_selection();
}
else if ( quit_prompt == 'Q' || quit_prompt == 'q') {
cout << "Have a blessed day!!!n";
cout << "n==============================================nn";
return 'a';
}
else {
cout << "You entered a invalid response, no action was takenn";
cout << "n==============================================nn";
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;
}
您从quit
函数返回一个值,但在main
函数中不保存或执行它。
您可能应该而不是从quit
函数返回值,也不应该像在main
函数中那样显示菜单或获取选择。相反,我会建议一些类似的东西
void quit() {
// Are you sure you want to quit prompt
char quit_prompt {};
cout << "n==============================================nn";
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==============================================nn";
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 takenn";
cout << "n==============================================nn";
}
}
相关文章:
- 未在作用域中声明unordered_map
- 有没有一种方法可以在编译时获得作用域类名
- C++quit()函数中可能存在作用域问题
- 未在此作用域OpenCV3.4中声明cvSaveImage
- 全局作用域中函数指针的赋值
- 在类函数中初始化外部作用域变量
- 不同作用域中的静态变量和全局变量
- 是同一作用域的函数部分中的函数调用
- 未在此作用域中声明的函数和变量 (C++)
- 类作用域的类型别名"using":[何时]方法中的用法可以先于类型别名?
- 将作用域枚举转换为基础类型
- 表达式必须具有完整或无作用域的枚举图
- 在构造函数中输入对象时C++类成员作用域
- 无法让"std::enable_if"适用于无作用域枚举
- C2386:"void_t":当前作用域中已存在具有此名称的符号
- 未在此作用域中声明函数,即使存在头文件也是如此
- 为什么编译器对此模板存在作用域问题
- 如果在同一作用域中存在同名的类和变量,如何指定该类
- 如果存在,则使用它/检测C++中当前作用域的调用约定(thiscall vs cdecl)
- 模板类的实例之间是否存在共享的作用域