Thread: NEED HELP! :( Simple Calculator using Pointers.

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    12

    NEED HELP! :( Simple Calculator using Pointers.

    Need help making a Calculator program.
    I need help on the function I have set, idk if I'm doing it correct or not. and how to have a function call for it. I tried using Calculate();
    - Create a function called "Calculate" that takes in two integer pointers and - char variable as parameters. This function will return an integer value.
    - Save the returned value into a new int variable called "result"
    - Point "integerPointer" to the address of "result" and display the dereferenced value for integerPointer.
    is what is needed.
    Code:
    #include<iostream>
    using namespace std;
    int integerVariable1;
    int integerVariable2;
    char operation;
    int result;
    int Dothemath;
    int Calculate(int *integerPointer,char operation);
    int main()
    {
    	char again = 'y';
    while (again == 'y')
    {
    	cout<<"Please enter your first number:";
    	cin>>integerVariable1;
    	
    	cout<<"Please enter the desired operation:";
    	cin>>operation;
    
    	cout<<"Please enter your second number:";
    	cin>>integerVariable2;
    
    	int* integerPointer=0;
    	integerPointer=&integerVariable1;
    
    	cout<<*integerPointer<<operation;
    	integerPointer=&integerVariable2;
    	cout<<*integerPointer;
    	cout<<"=";
    	integerPointer=&result;
    	cout<<*integerPointer;
    	cout<<"\nCare to try again? (y/n)";
        cin>>again;
    }
        
       return 0;
    }
     int Calculate(int *integerPointer,char operation)
    {
    	int result;
      if(operation=='+')
      {
    	  result=*integerPointer+*integerPointer;
      }
      if(operation=='-')
      {
       result=*integerPointer-*integerPointer; 
      } 
      return result;
     }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Something like
    Code:
    	cout<<"Please enter your first number:";
    	cin>>integerVariable1;
    	
    	cout<<"Please enter the desired operation:";
    	cin>>operation;
    
    	int result = Calculate( &integerVariable1, operation );
    I think with that, you should be able to get pretty close to the answer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    12
    Code:
    cout<<"Please enter your first number:";
    	cin>>integerVariable1;
    	
    	cout<<"Please enter the desired operation:";
    	cin>>operation;
    
    	int result = Calculate( &integerVariable1, operation );
    k I see this and tried it it will include the variable 1 and operation, into the calculate but it wont let me take the variable two instead it just takes in variable 1 and uses it twice
    how am I supposed to make it similar to
    Code:
    int result = Calculate( &integerVariable1, &integerVariable2,operation );

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Where are your edits to the Calculate function, so that it expects and uses another parameter?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple calculator
    By yacek in forum C Programming
    Replies: 10
    Last Post: 10-07-2010, 05:24 AM
  2. Need help with simple calculator
    By j_wolfe in forum C Programming
    Replies: 2
    Last Post: 09-15-2008, 09:00 AM
  3. simple calculator
    By HunterCS in forum C++ Programming
    Replies: 10
    Last Post: 07-18-2007, 06:51 AM
  4. A simple calculator in C
    By AlphaMac in forum C Programming
    Replies: 11
    Last Post: 10-14-2006, 01:26 AM
  5. simple calculator
    By Micko in forum C++ Programming
    Replies: 4
    Last Post: 08-12-2005, 06:47 AM

Tags for this Thread