HI Javyesco...
Your "option choice" is read into the meN variable. So probably what you wanted to do is ==> options(meN) ^_^.
--
Are you very new to C++ ?
You've got some warning that you might want to handle and take care now.
For example, change the prototype of your two functions options() and menus() to :
int menus()
and
int options(int)
Also change your PI definition to ==> #define PI 3.1415f
And in fact this definition would be better ==> float const pi = 3.141592653589793f;
The final "f" on a number means that it is a float.
Also, at the end of the program, of the function options(), do this instead :
1 2 3 4 5
|
default:
std::cerr << "Invalid option" << endl;
}
return choice;
}
| |
It's better to return at the end for fallback value.
And use cerr instead of cout for you error messages ^_^, they will go directly to the error standard output in your shell.
Very important, don't use the "using namespace std;"... it's a bad smell and it will become error prone in the future.
But for example, you've got a long function options(), inside of it, you can "use namespace std;" --> it will be localized to that function only and not "pollute" your entire program.
And finally, you can also change your value "0.5" to "0.5f" to make it a float.
And the formula for the triangle is incorrect since it is "* 0.5" not "/ 0.5".
--
Back to your question...
If you want to get rid of your "option's choice", you can send that option to the program directly when you call it by sending a parameter.
For example, il your program's name is "computeSurface" then you can call in your shell :
$> computeSurface 1
or better
$> computeSurface disc
To explain that... here's the detail bellow for the two cases. And from that moment on, your "menus()" function will be used (for example) only when the parameter send to your program is wrong.
case 1 : sending a numerical parameter (1, 2 or 3)
If you want to give the option to your program, you can do it by "parsing" your program's parameter and send the "option value" to your function options().
For that :
1) change the main() prototype to : int main(int nbofArg, char* arg[])
2) read the option value... and suppose (for simplicity) that by default, you take the option 1. So add this line of code ==> auto option = atoi(arg[1]);
And keep in mind that the function atoi() is a bad smell too :oP~
Then, when you want to call your program (say the name is "computeSurface"), you can write this to your shell $> computeSurface 2
Or just call $> computeSurface and the default option value will be 1.
case 2 : sending "string literal" as program parameter
But better... "1", "2", "3" are not good arguments and names are the most important thing in a "program". So you can choose the options "disc", "rectangle" and "triangle" for example. You then can call your program using
$> computeSurface disc
You shall also rename your program to computeSurfaceFor. This way, it becomes very readable like :
$> computeSurfaceFor rectangle
Hope that will help ^_^
I don't know how to paste the code here so you can download it on my website here -->
https://www.punksheep.com/partage/C++/computeSurfaceFor.zip
Final remark : if you find a line like this ==> #include "stdafx.h"
just ignor it... I'm on Windows using Visual Studio. Just delete the line ;o). I tried my best to get rid of it but you know... >_<
Have fun ;)