Order of Operations Question
Just out of curiosity, in the code below, whenever the add(), sub(), mul(), and div() functions are called, do they return 0 after main() has been called, or do they function more like batch files in which until main() has finished or terminated the next line is not run in those functions?
For example:
Code:
@echo off
echo This program runs the windows calculator.
c:\windows\calc.exe
exit
In the batch file above, the exit command is not run until c:\windows\calc.exe has been terminated. Is this the same in C++?
Here is the code in question:
Code:
#include <iostream.h>
int add();
int sub();
int mul();
int div();
int main()
{
int input;
cout<<"Please select an operation:"<<endl;
cout<<"1. Addition"<<endl;
cout<<"2. Subtraction"<<endl;
cout<<"3. Multiplication"<<endl;
cout<<"4. Division"<<endl;
cout<<"5. Exit"<<endl;
cin>>input;
switch (input)
{
case 1: add();
break;
case 2: sub();
break;
case 3: mul();
break;
case 4: div();
break;
case 5: return 0;
}
}
int add()
{
int x, y;
cout<<"Please input two numbers to be added together."<<endl;
cin>>x>>y;
cout<<"The result is: "<<x+y;
main();
return 0;
}
int sub()
{
int x, y;
cout<<"Please input a number to be subtracted from another."<<endl;
cin>>x>>y;
cout<<"The resul is: "<<y-x;
main();
return 0;
}
int mul()
{
int x, y;
cout<<"Please input two numbers to be multiplied together."<<endl;
cin>>x>>y;
cout<<"The result is: "<<x*y;
main();
return 0;
}
int div()
{
int x, y;
cout<<"Please a number to be devided by another."<<endl;
cin>>x>>y;
cout<<"The result is: "<<x/y;
main();
return 0;
}