#include <iostream>
#include <regex>
using namespace std;
void main_menu();
int get_user_input();
void select_menu_item(int input);
void print_main_menu();
void go_back_to_main_menu();
bool is_integer(std::string num);
void menu_item_1();
void menu_item_2();
void menu_item_3();
void menu_item_4();
int main(int argc, char const *argv[]) {
//int num1, num2, choice, i;
main_menu();
return 0;
}
void main_menu() {
char fav;
int num1,num2,choice,i;
cout<<"\n Enter 1st Number: ";
cin>>num1;
cout<<"\n Enter 2nd Number: ";
cin>>num2;
print_main_menu();
int input = get_user_input();
select_menu_item(input);
//void menu_item_1();
}
int get_user_input() {
int input;
std::string input_string;
bool valid_input = false;
int menu_items = 5;
do {
std::cout << "\nSelect item: ";
std::cin >> input_string;
valid_input = is_integer(input_string);
// if input is not an integer, print an error message
if (valid_input == false) {
std::cout << "Enter an integer!\n";
} else { // if it is an int, check whether in range
input = std::stoi(input_string); // convert to int
if (input >= 1 && input <= menu_items) {
valid_input = true;
} else {
std::cout << "Invalid menu item!\n";
valid_input = false;
}
}
} while (valid_input == false);
return input;
}
void select_menu_item(int input) {
switch (input) {
case 1:
menu_item_1();
break;
case 2:
menu_item_2();
break;
case 3:
menu_item_3();
break;
case 4:
menu_item_4();
break;
default:
exit(1);
break;
}
}
void print_main_menu() {
std::cout << "\n----------- Main menu -----------\n";
std::cout << "|\t\t\t\t\t\t|\n";
std::cout << "|\t1. Menu item 1\t\t|\n";
std::cout << "|\t2. Menu item 2\t\t|\n";
std::cout << "|\t3. Menu item 3\t\t|\n";
std::cout << "|\t4. Menu item 4\t\t|\n";
std::cout << "|\t5. Exit\t\t\t\t|\n";
std::cout << "|\t\t\t\t\t\t|\n";
std::cout << "---------------------------------\n";
}
void go_back_to_main() {
std::string input;
do {
std::cout << "\nEnter 'b' or 'B' to go back to main menu: ";
std::cin >> input;
} while (input != "b" && input != "B");
main_menu();
}
// https://codereview.stackexchange.com/questions/162569/checking-if-each-char-in-a-string-is-a-decimal-digit
bool is_integer(std::string num) {
return std::regex_match(num, std::regex("[ -]?[0-9] "));
}
void menu_item_1() {
int num1, num2, choice, i;
std::cout << "\n>> Menu 1\n";
std::cout << "\n Sum of "<< num1 <<" and "<< num2;
cout << " = "<<num1 num2;
//std::cin>> Menu_item 1;
// you can call a function from here that handles menu 1
go_back_to_main();
}
void menu_item_2() {
std::cout << "\n>> Menu 2\n";
std::cout << "\nSome code here does something useful\n";
// you can call a function from here that handles menu 2
go_back_to_main();
}
void menu_item_3() {
std::cout << "\n>> Menu 3\n";
std::cout << "\nSome code here does something useful\n";
// you can call a function from here that handles menu 3
go_back_to_main();
}
void menu_item_4() {
std::cout << "\n>> Menu 4\n";
std::cout << "\nSome code here does something useful\n";
// you can call a function from here that handles menu 4
go_back_to_main();
}
output:
Enter 1st Number: 56
Enter 2nd Number: 87
----------- Main menu -----------
| |
| 1. Menu item 1 |
| 2. Menu item 2 |
| 3. Menu item 3 |
| 4. Menu item 4 |
| 5. Exit |
| |
---------------------------------
Select item: 1
>> Menu 1
Sum of 0 and 6596224 = 6596224
Enter 'b' or 'B' to go back to main menu:
I feel I am close to getting the first menu function working however I can't work out why the output for Menu 1 keeps returning "Sum of 0 and 6596224 = 6596224". I am not sure which part of the code needs rearranging.
I am aware I have only completed the addition but once I have figured out what is going wrong here I'm sure I will be able to complete the rest of it.
Thanks
CodePudding user response:
num1
in main
and num1
in menu_item_1()
are two completely unrelated variables. Same for num2
. Both are not initialized in menu_item_1
.
If you already read the values before, you can pass them to the function:
void menu_item_1(int num1, int num2) {
std::cout << "\n>> Menu 1\n";
std::cout << "\n Sum of " << num1 << " and " << num2;
std::cout << " = " << num1 num2;
}
I do not understand the purpose of go_back_to_main
, because it keeps the user in an infinite loop and the only choice they have is to confirm by typing either 'b'
or 'B'
, hence I removed it.
CodePudding user response:
Use of uninitialized variables is similar to use of uninitialized memory and might be a source of errors of different kinds to occur during program execution.
Like the previous solution, you need values entered by the user to the functions menu_item_
*.
void menu_item_1() {
int num1, num2, choice, i;
cin >> num1 >> num2;
cout << ">> Menu 1\n Sum of " << num1 << "and " << num2 << " = " << num1 num2 << endl;
go_back_to_main();
}
output:
Enter 1st Number: 1
Enter 2nd Number: 1
----------- Main menu -----------
| |
| 1. Menu item 1 |
| 2. Menu item 2 |
| 3. Menu item 3 |
| 4. Menu item 4 |
| 5. Exit |
| |
---------------------------------
Select item: 1
Enter number 1 and number 2
4512 4123
>> Menu 1
Sum of 4512 and 4123 = 8635
Enter 'b' or 'B' to go back to main menu:
run here