### Quick question on a return

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202`` ``````// Includes #include "stdafx.h" #include #include #include #include using namespace std; // Prototypes int addition(); int subtraction(); int multiplication (); int division(); int exit(); // Addition Function int add() { srand ((unsigned int) time(NULL)); int num1 = 1 + rand() % 1000; int num2 = 1 + rand() % 1000; double answer; double correct; cout << "This is an addition math tutor.\n\n"; cout << " " << num1 << endl; cout << " + " << num2 << endl; cout << "\n\nEnter your answer: "; cin >> answer; correct = num1 + num2; cout << "\n\n"; if ( answer != correct) { cout << "You got it wrong." << endl; cout << "The correct answer is " << correct << endl; } else if ( answer == correct) { cout << "Congratulations, your got it right." << endl; } _getch(); return main(); } // Subtraction Function int subtraction() { srand ((unsigned int) time(NULL)); int num1 = 1 + rand() % 1000; int num2 = 1 + rand() % 1000; double answer; double correct; cout << "This is an addition math tutor.\n\n"; cout << " " << num1 << endl; cout << " - " << num2 << endl; cout << "\n\nEnter your answer: "; cin >> answer; correct = num1 - num2; cout << "\n\n"; if ( answer != correct) { cout << "You got it wrong." << endl; cout << "The correct answer is " << correct << endl; } else if ( answer == correct) { cout << "Congratulations, your got it right." << endl; } _getch(); return main(); } // Multiplication Function int multiplication() { srand ((unsigned int) time(NULL)); int num1 = 1 + rand() % 1000; int num2 = 1 + rand() % 1000; double answer; double correct; cout << "This is an addition math tutor.\n\n"; cout << " " << num1 << endl; cout << " * " << num2 << endl; cout << "\n\nEnter your answer: "; cin >> answer; correct = num1 * num2; cout << "\n\n"; if ( answer != correct) { cout << "You got it wrong." << endl; cout << "The correct answer is " << correct << endl; } else if ( answer == correct) { cout << "Congratulations, your got it right." << endl; } _getch(); return main(); } // Division Function int division() { srand ((unsigned int) time(NULL)); int num1 = 1 + rand() % 1000; int num2 = 1 + rand() % 1000; double answer; double correct; cout << "This is an addition math tutor.\n\n"; cout << " " << num1 << endl; cout << " / " << num2 << endl; cout << "\n\nEnter your answer: "; cin >> answer; correct = num1 / num2; cout << "\n\n"; if ( answer != correct) { cout << "You got it wrong." << endl; cout << "The correct answer is " << correct << endl; } else if ( answer == correct) { cout << "Congratulations, your got it right." << endl; } _getch(); return main(); } // Exit Function int exit() { cout << "Progam is exiting."; _getch(); return (0); } // Main Function int main() { int choice; cout << "This program is a math tutor.\n\n"; do { cout << "-----------------------------------" << endl; cout << "| |" << endl; cout << "| |" << endl; cout << "| 1. Addition |" << endl; cout << "| 2. Subtraction |" << endl; cout << "| 3. Multiplication |" << endl; cout << "| 4. Division |" << endl; cout << "| 5. Exit |" << endl; cout << "| |" << endl; cout << "| |" << endl; cout << "-----------------------------------" << endl; cout << "\n\nEnter a choice: "; cin >> choice; while ( choice < 1 || choice > 5) { cout << "Please enter 1, 2, 3, 4, or 5: "; cin >> choice; } switch (choice) { case 1: addition(); break; case 2: subtraction(); break; case 3: multiplication(); break; case 4: division(); break; case 5: exit(); break; } } while (choice != 5); _getch(); return 0; }``````

How can i make the addition, subtraction, multiplication and division function make a return to main function so the user when done can repick a choice.
Last edited on
You declared the functions such a way that they shall return a value of type int. So each function shall contain a return statement with an expression of type int.

But if to follow the logic of the functions it is better if they wouldl return nothing that is if they have return type void

For example

 ``12345678910111213141516171819202122232425262728293031`` ``````void add() { srand ((unsigned int) time(NULL)); int num1 = 1 + rand() % 1000; int num2 = 1 + rand() % 1000; int answer; int correct; cout << "This is an addition math tutor.\n\n"; cout << " " << num1 << endl; cout << " + " << num2 << endl; cout << "\n\nEnter your answer: "; cin >> answer; correct = num1 + num2; cout << "\n\n"; if ( answer != correct) { cout << "You got it wrong." << endl; cout << "The correct answer is " << correct << endl; } else if ( answer == correct) { cout << "Congratulations, your got it right." << endl; } _getch(); }``````
Last edited on
thank you vlad. all done with that program.
actually i do have 1 more question, why is the right side of my box in the main function all messed up, its off alignment. Even when i run the program its off alignment.
Last edited on
It is because you are using TAB symbols instead of spaces.
Here are codes of one of your lines

124 9 9 9 9 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 124

that corresponds to

 ``12`` `````` cout << "| |" << endl; ``````
As you see in the very beginning of the line there are numbers 9 that correspond to the TAB symbol. 32 is the code of the space. You should use spaces instead of tabs.
Last edited on
oh ok, thank you.
Topic archived. No new replies allowed.