# how do you avoid using one function

• 11-21-2009
bijan311
how do you avoid using one function
this is my code

Code:

```#include <iostream> #include <cmath> using namespace std; float number1()//the function that gets the first number {         float x;         cout<<"please enter a number:";         cin>> x;//this is where the number is stored                 return x;//the value of x is now "float number1()" } char operation() {         char a;         cout<<"please enter a operation (x, / , - , +, ^, s(square root for first number))";                 cin>> a;         return a; } float number2() {         float y;         cout<<"please enter another number:";                 cin>> y;                 return y; } float math() {         float x = number1();//saying the variable x is the function "float number1()"         char a = operation();     float y = number2();     float answer;         float answer2; //this next part decides if you add, multiply, divide, or subtract depending on the input         switch(a)         {         case 'x':                         answer = x*y;                         break;         case '/':                                 answer = x/y;                                 break;         case '+':                                         answer = x+y;                                         break;         case '-':                                                 answer = x-y;                                                 break;         case '^'://pow is power (like 1 to the tenth power)                 answer = pow (x,y);                 break;                 case 's'://sqrt is square root                 answer = sqrt (x);                 break;                         }         return answer; } int main() {         float answer = math();         char enter;         cout<< answer <<"\n";         system("pause");         cin.get(); }```
so i would like to know how to not do float number 2 when the operation is square root.
• 11-21-2009
BdON003
Just make the switch statement in math look like so:
Code:

```switch(a)         {         case 'x':                         float y = number2();                         answer = x*y;                         break;         case '/':                         float y = number2();                         answer = x/y;                         break;         case '+':                         float y = number2();                         answer = x+y;                         break;         case '-':                         float y = number2();                             answer = x-y;                         break;         case '^'://pow is power (like 1 to the tenth power)                         float y = number2();                         answer = pow (x,y);                         break;                 case 's'://sqrt is square root                 answer = sqrt (x);                 break;                         }```
• 11-21-2009
bijan311
i did that in a way but it didn't compile so i made it like this
Code:

```if (a == 's'){                 answer = sqrt (x);         } float y;     switch (a){         case 'x':                 y = number2();                 answer = x*y;                 break;         case '/':                 y = number2();                 answer = x/y;                 break;         case '+':                 y = number2();                 answer = x+y;                 break;         case '-':                 y = number2();                 answer = x-y;                 break;         case '^':                 y = number2();                 answer = pow (x,y);                 break;         }```
• 11-22-2009
Daved
You're close with that if, but I would put something else in an if. Notice how you're repeating the code to call number2()? Instead of putting that call in switch, put that in some sort of if control.
• 11-22-2009
anon
Code:

```if (is_binary_operation(a)) {     float y = get_number("Enter another number: ");     switch over binary operations } else {     switch over unary operations }```