# Thread: Looking for efficiency in my app

1. ## Looking for efficiency in my app

Hey all,

I'm still working on my calculator and I'm wondering if I can find an alternative to using if() statements within declared functions. Here's the code:

Code:
```#include <iostream.h>
#include <stdlib.h>

void one(void)
{
int choice_1;

cout<<"Section one : Basic operations"<<endl<<endl;
cout<<"**********************************************************"<<endl;

}

void two(void)
{
int choice_2;

cout<<"Section two : Area configiration"<<endl<<endl;
cout<<"**********************************************************"<<endl;

}

void three(void)
{
int choice_3;

cout<<"Section three : Volumetrics"<<endl<<endl;
cout<<"**********************************************************"<<endl;
}

void four(void)
{
int choice_4;

cout<<"Section four : Trigonometry"<<endl<<endl;
cout<<"**********************************************************"<<endl;
}

void five(void)
{
int choice_5;

cout<<"Section five : Advanced algorithms and Calculus"<<endl<<endl;
cout<<"**********************************************************"<<endl;
}

void six(void)
{
int chocie_6;

cout<<"Section six : Unit conversion"<<endl<<endl;
cout<<"**********************************************************"<<endl;
}

int main()
{
int choice_main;
choice_main = 1 || 2 || 3 || 4 || 5 || 6;

cout<<"**********************************************************"<<endl;
cout<<"Please select a function from the list below:"<<endl<<endl;
cout<<"1 - Basic mathematical operations"<<endl<<"2 - Area configuration"<<endl<<"3 - Volumetrics"<<endl<<"4 - Trigonometrics"<<endl<<"5 - Advanced algorithms and calculus"<<endl<<"6 - Unit conversion"<<endl<<"7 - Exit Program"<<endl<<endl;

cin>>choice_main;
cout<<"**********************************************************"<<endl;
switch(choice_main)
{
case 1 : one();
break;
case 2 : two();
break;
case 3 : three();
break;
case 4 : four();
break;
case 5 : five();
break;
case 6 : six();
break;
case 7 : return 0;
break;
default: cout<<"Error, bad input, halting program..."<<endl<<endl;
}

system("PAUSE");
return 0;
}```
Inside the functions before int main(), I want to declare case statements for each, but they don't seem to want to work with the functions I want to use.

Code:
```void add(void)
{
blah blah
}

void subtract(void)
{
blargh bletch
}

//Hate to use comments. but begin section one

void one(void)
{
int choice;
cin>>choice;
switch(choice)
{
break;
case 2 : subtract();
break;
default : return 0;
}
}```
I Keep getting an error pertaining to the declaration of the value '1' for a case statement. Is there any way around this, or am I going to have to resort back to if() statements?

Thanks again

(Oh, and yeah, I'm working on a calculator, so... don't laugh)

2. Real quick...
You don't need return 0; in your case statement. It's a void function. Also, you don't prompt for input for choice.
And this
Code:
`choice_main = 1 || 2 || 3 || 4 || 5 || 6;`
doesn't look legal. An int is one value, you can't OR it like that.
Maybe it's a more advanced function than I know.

3. > choice_main = 1 || 2 || 3 || 4 || 5 || 6;

means

choice_main = 1;

4. ## Thanks a lot!

I wouldn't have noticed that return 0; in the case statement, but this is just a basic shell. I caught that logical-or immediately after I posted, but thanks anyway. I can't believe I messed up on that, though. Oh well, more sleep is in order, I guess.

Anyway, thanks for the input. I probably should get back to work on it, so I guess I'll see you all later.

5. Looking quickly over the code again, I noticed that a variable doesn't coencide with the label I placed on it:

Code:
```********"<<endl;
}

void six(void)
{
int chocie_6;

cout<<"Section six : Unit```
Thanks again, I'll just have to polish the skeleton off before I try anything drastic, though.

6. #include <iostream.h>
#include <stdlib.h>

If you want to comply with standards, you'd include these:

#include <cstdlib>
#include <iostream>

Make sure you put using namespace std; at the top of every function though.

one();
two();
three();
// ...

seem entirely unnecessary. As far as I can see, you only call these on one occasion, and they consist of nothing more then a few printouts. You could define the line printout as something, say:

#define stars() cout<<" **************************************************
********"<<endl;

That would significantly clean up your code. None of this is necessary, but I think it would be an improvement.

7. ## Thanks

I've been working on that skeleton for the past five or six hours and it's bulked up VERY much since I last posted it on the board. I still have half of function two() to do, not forgetting the last four functions. Each function's about... on //average// about a hundred lines long (or more), and I want to keep everything nice and collected. But, that idea of the #define stars() was the best one I've encountered in hours. Thanks a lot!

here's the code after I got to it for awhile:

Code:
```#include <iostream>
#include <cstdlib>
#include <stdlib.h>
#include <math.h>
#define stars() cout<<"**********************************************************"<<endl;

void one(void)
{
using namespace std;
int choice_1;

float add1, add2, sub1, sub2, mult1, mult2, div1, div2, base, pwr, sq_rt, prcnt1, prcnt2;
cout<<"Section one : Basic operations"<<endl<<endl;
cout<<"Please select an operation to perform from the list below:"<<endl<<endl;

cout<<"1 - Addition"<<endl<<"2 - Subtraction"<<endl<<"3 - Multiplication"<<endl<<"4 - Division"<<endl<<"5 - Root Configuration"<<endl<<"6 - Square roots"<<endl<<"7 - Percentages"<<endl<<endl;
cin>>choice_1;

stars()

if (choice_1 == 1)
{
cout<<"What do you wish to add?"<<endl<<endl;
cout<<"+"<<endl;
}

else if(choice_1 == 2)
{
cout<<"What do you wish to subtract?"<<endl<<endl;
cin>>sub1;
cout<<"-"<<endl;
cin>>sub2;
cout<<sub1<<" - "<<sub2<<" = "<<sub1 - sub2<<endl<<endl;
}

else if(choice_1 == 3)
{
cout<<"What do you wish to multiply?"<<endl<<endl;
cin>>mult1;
cout<<"*"<<endl;
cin>>mult2;
cout<<mult1<<" * "<<mult2<<" = "<<mult1 * mult2<<endl<<endl;
}

else if(choice_1 == 4)
{
cout<<"What do you wish to divide?"<<endl<<endl;
cin>>div1;
cout<<"/";
cin>>div2;
cout<<div1<<" / "<<div2<<" = "<<div1 / div2<<endl<<endl;
}

else if(choice_1 == 5)
{
cout<<"What is the base (root coefficient) of this equation?"<<endl<<endl;
cin>>base;
cout<<"What is the power (exponential coefficient) of this equation?"<<endl<<endl;
cout<<"^"<<endl;
cin>>pwr;
cout<<base<<"^"<<pwr<<" = "<<pow(base, pwr)<<endl<<endl;
}

else if(choice_1 == 6)
{
cout<<"What is the number you wish to find the squared-root of?"<<endl<<endl;
cin>>sq_rt;
if (sq_rt >= 1)
{
cout<<"Output for "<<sq_rt<<" is "<<sqrt(sq_rt)<<endl<<endl;
}
else
{
}
}

else if(choice_1 == 7)
{
cout<<"What is the standard (upper) coeffieicnt, or the number you"<<endl;
cout<<"wish to find a certain percentage of?"<<endl<<endl;
cin>>prcnt1;
cout<<"What is the subordinate (lower) coefficient, or the"<<endl;
cout<<"percentile of the previous number you wish to find?"<<endl<<endl;
cin>>prcnt2;
cout<<prcnt2<<" percent of "<<prcnt1<<" is "<<(prcnt2/100)*prcnt1<<endl<<endl;
}

else
{
}

stars()

}

void two(void)
{
using namespace std;
int choice_2;
float side1;
float side2;
float base;
float height;
float pi;
pi = 3.1415926535897932384626433832795;

cout<<"Section two : Area configiration"<<endl<<endl;
cout<<"What operation do you wish to perform from the list below?"<<endl<<endl;
cout<<"1 - Area Calculator (2-D)"<<endl;
cout<<"2 - Surface-area Calculator (3-D)"<<endl<<endl;

cin>>choice_2;

stars()
if(choice_2 == 1)
{
cout<<"Area Calculator for two-dimentional objects"<<endl<<endl;
int choice_2a;

cout<<"What operation do you wish to perform from the list below?"<<endl<<endl;
cout<<"1 - Square/Rectangle"<<endl<<"2 - Triangle"<<endl<<"3 - Circle"<<endl<<"4 - Parallellogram"<<endl<<"5 - Trapezoid"<<endl<<endl;

cin>>choice_2a;

if(choice_2a == 1)
{
cout<<"Square or rectangle area calculation - here's the formula:"<<endl<<"area (a) = base (b) * height (h)"<<endl;
cout<<"Okay, what's the base of the square or rectangle?"<<endl<<endl;
cin>>base;
cout<<"Alright, I'll need the height of the square or rectangle.  What is the height?"<<endl<<endl;
cin>>height;
cout<<"area = "<<base<<" * "<<height<<" = "<<base * height<<endl<<endl;
}

else if(choice_2a == 2)
{
cout<<"Triangle area calculation - here's the formula:"<<endl<<"area (a) = base (b) * height (h) / 2"<<endl;
cout<<"Okay, what's the base of the triangle?"<<endl<<endl;
cin>>base;
cout<<"Alright, I'll need the height of the triangle.  What's the height?"<<endl<<endl;
cin>>height;
cout<<"area = "<<base<<" * "<<height<<" / 2 = "<<(base*height)/2<<endl<<endl;
}

else if(choice_2a == 3)
{
cout<<"Circle area calculation - here's the formula:"<<endl<<"area (a) = (pi (3.14) * r)^2"<<endl;
cout<<"Okay, what's the radius of the circle?"<<endl<<endl;
cout<<"area = (pi * r)^2 = ("<<pi<<" * "<<radius<<")^2 = "<<pow((pi * radius), 2)<<endl<<endl;
}

else if(choice_2a == 4)
{
cout<<"Parallelogram area calculation - here's the formula:"<<endl<<"area (a) = base (b) * height (h)"<<endl;
cout<<"Okay, what's the base of the paralellogram?"<<endl<<endl;
cin>>base;
cout<<"Alright, what's the height of the parallelogram?"<<endl<<endl;
cin>>height;
cout<<"area = "<<base<<" * "<<height<<" = "<<base * height<<endl<<endl;
}

else if(choice_2a == 5)
{
cout<<"Trapezoid area calculation - here's the formula:"<<endl<<"area (a) = .5 * (height (h) * (base1 (b1) + base2 (b2))"<<endl;
cout<<"Okay, what's the first base (b1) of the paralellogram?"<<endl<<endl;
cin>>side1;
cout<<"Alright, what's the second base (b2) of the parallelogram?"<<endl<<endl;
cin>>side2;
cout<<"Okay, now, I'll need the height of the parallelogram.  What's the height?"<<endl<<endl;
cin>>height;
cout<<"area = .5 * ("<<height<<" * ("<<side1<<" * "<<side2<<") = "<<.5 * (height * (side1 + side2))<<endl<<endl;
}
else
{
}

}

else if(choice_2 == 2)
{
cout<<"Surface-area Calculator for three-dimentional objects"<<endl<<endl;

cout<<"What operation do you wish to perform from the list below?"<<endl<<endl;
cout<<"1 - "<<endl<<"2 - "<<endl<<"3 - "<<endl<<"4 - "<<endl<<"5 - "<<endl<<"6 - "<<endl<<endl;
//triangular prism, cube, sphere, rectangular prism, pyramid, cone,
}

else
{
}

stars()

}

void three(void)
{
using namespace std;
int choice_3;

cout<<"Section three : Volumetrics"<<endl<<endl;
stars()
}

void four(void)
{
using namespace std;
int choice_4;

cout<<"Section four : Trigonometry"<<endl<<endl;
stars()
}

void five(void)
{
using namespace std;
int choice_5;

cout<<"Section five : Advanced algorithms and Calculus"<<endl<<endl;
stars()
}

void six(void)
{
using namespace std;
int choice_6;

cout<<"Section six : Unit conversion"<<endl<<endl;
stars()
}

int main()
{
int choice_main;
choice_main = 1,2,3,4,5,6;

stars()
cout<<"Please select a function from the list below:"<<endl<<endl;
cout<<"1 - Basic mathematical operations"<<endl<<"2 - Area configuration"<<endl<<"3 - Volumetrics"<<endl<<"4 - Trigonometrics"<<endl<<"5 - Advanced algorithms and calculus"<<endl<<"6 - Unit conversion"<<endl<<"7 - Exit Program"<<endl<<endl;

cin>>choice_main;
stars()
switch(choice_main)
{
case 1 : one();
break;
case 2 : two();
break;
case 3 : three();
break;
case 4 : four();
break;
case 5 : five();
break;
case 6 : six();
break;
case 7 :
cout<<"Quitting program..."<<endl<<endl;
break;
default: cout<<"Error, bad input, halting program..."<<endl<<endl;
}

system("PAUSE");
return 0;
}```
Shouts out to you, man, thanks a million!

8. #include <iostream>
#include <cstdlib>
#include <stdlib.h>
#include <math.h>

cstdlib and stdlib.h are the same header.
cstdlib is basically:

Code:
```namespace std {

#include <stdlib.h>

}```
So, you can drop the stdlib.h. Also, #include <cmath> instead of <math.h> (it's the exact same thing as stdlib).