Thread: how do you avoid using one function

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    151

    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.

  2. #2
    Registered User
    Join Date
    Oct 2009
    Posts
    48
    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;
    		
    	}

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    151
    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;
    	}
    Last edited by bijan311; 11-21-2009 at 10:09 PM.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    if (is_binary_operation(a)) {
         float y = get_number("Enter another number: ");
         switch over binary operations
    }
    else {
        switch over unary operations
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. dynamic function execution
    By Sargnagel in forum C Programming
    Replies: 7
    Last Post: 05-07-2003, 05:28 AM

Tags for this Thread