ANYONE KNOWS THE PROBLEM?

Anyone knows how to solve the problem I'm having

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include<iostream>
#include<cmath>
using namespace std;
#define PI 3.1415
float menus();
float options(int choice);
int main()
{
	double meN, opT;

	meN = menus();
	opT = options(choice); //PROBLEM HERE APARENTLY with the (choice)

	system("pause");
	return 0;
}

float menus()
{
	int option;

	cout << "::Menu::\n"
		<< "1. Circle Area\n"
		<< "2. Rectangle Area\n"
		<< "3. Triangle Area\n"
		<< "4. Quit\n\n"
		<< "Select (1-4): ";
	cin >> option;

	return option;
}

float options(int choice)
{
	float AC, AR, AT, r, L, W, b, h;

	switch (choice)
	{
	case 1:
		cout << "Enter the radio: ";
		cin >> r;

		if (r <= 0.0)
		{
			cout << "Error. Re-enter: ";
			cin >> r;
		}

		AC = (PI * pow(r, 2));

		cout << AC << endl;

		system("pause");
		break;

	case 2:
		cout << "Enter the length: ";
		cin >> L;

		while (L <= 0.0)
		{
			cout << "Error. Re-enter: ";
			cin >> L;
		}

		cout << "Enter the width: ";
		cin >> W;

		while (W <= 0.0)
		{
			cout << "Error. Re-enter: ";
			cin >> W;
		}

		AR = L * W;

		cout << AR << endl;

		system("pause");
		break;

	case 3:
		do {
			cout << "Enter the base: ";
			cin >> b;
		} while (b <= 0.0);

		do {
			cout << "Enter the height: ";
			cin >> h;
		} while (h <= 0.0);

		AT = (b * h) / 0.5;

		cout << AT << endl;

		system("pause");
		break;

	case 4:
		break;

	default:
		cout << "Invalid option" << endl;

		return choice;
	}
}
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 ;)
Last edited on
Topic archived. No new replies allowed.