# Thread: how do you avoid using one function

1. ## 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();
//this next part decides if you add, multiply, divide, or subtract depending on the input
switch(a)
{
case 'x':
break;

case '/':
break;

case '+':
break;

case '-':
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;

}

}
int main()
{
float answer = math();
char enter;

system("pause");

cin.get();
}```
so i would like to know how to not do float number 2 when the operation is square root.

2. Just make the switch statement in math look like so:
Code:
```switch(a)
{
case 'x':
float y = number2();
break;

case '/':
float y = number2();
break;

case '+':
float y = number2();
break;

case '-':
float y = number2();
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;

}```

3. 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();
break;
case '/':
y = number2();
break;
case '+':
y = number2();
break;
case '-':
y = number2();
break;
case '^':
y = number2();
answer = pow (x,y);
break;
}```

4. 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.

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

Popular pages Recent additions