I recently took a break from programming(like a month, month an a half maybe). So as a little warmup to get back in the habit of programming, I made this small program. You just chose a category(addition, subtraction, mulitiplication, or division) and then enter 2 numbers. The 2 numbers are manipulated differently, depending on which category you chose. Then it gives you the answer and you go back to the main menu.
Its a really simple program, but for some reason its not working properly. It compiles just fine. But after you select one of the categories it just closes the program. I dont see why though. But I bet its something simple im overlooking.
Where the IF statement is at the end, that was originally a switch case. But since it wasnt working I figured I did the switch case incorrectly, so I changed it to an IF.Code:#include <iostream> using namespace std; void add(){ int x; int y; int result; result = x + y; cout << " Enter a number: "; cin >> x; cin.get(); cout << "\n\n Enter another number: "; cin >> y; cin.get(); cout << " " << x << " + " << y << " = " << result; cin.get(); cout << "\n\n\n"; } void sub(){ int x; int y; int result; result = x - y; cout << " Enter a number: "; cin >> x; cin.get(); cout << "\n\n Enter another number: "; cin >> y; cin.get(); cout << " " << x << " - " << y << " = " << result; cin.get(); cout << "\n\n\n"; } void mult(){ int x; int y; int result; result = x * y; cout << " Enter a number: "; cin >> x; cin.get(); cout << "\n\n Enter another number: "; cin >> y; cin.get(); cout << " " << x << " * " << y << " = " << result; cin.get(); cout << "\n\n\n"; } void div(){ int x; int y; int result; result = x/y; cout << " Enter a number: "; cin >> x; cin.get(); cout << "\n\n Enter another number: "; cin >> y; cin.get(); cout << " " << x << " / " << y << " = " << result; cin.get(); cout << "\n\n\n"; } int main(){ int lesson; cout << "\n\n\n"; cout << " Welcome to Math Fun Lessons 1 - 4 \n\n\n"; cout << " Please select a lesson.\n\n"; cout << " 1. Addition \n"; cout << " 2. Subtraction \n"; cout << " 3. Multiplication \n"; cout << " 4. Division \n\n"; cout << " Lesson: "; cin >> lesson; if(lesson == 1){ cout << "\n\n"; void add(); } else if(lesson == 2){ cout << "\n\n"; void sub(); } else if(lesson == 3){ cout << "\n\n"; void mult(); } else if(lesson == 4){ cout << "\n\n"; void div(); } else{ cout << " Invalid Syntax. Try again.\n\n"; } }



LinkBack URL
About LinkBacks



