Thread: simple calculator

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    25

    simple calculator

    Code:
    //Q3- Write a program to model a simple calculator. Each data line should consist of the next operation 
    //to be preformed from the list below and the right operand. Assume the left operand is the accumulator value 
    //	(initial value of 0). You need a function scan_data with two output parameters that returns the operator 
    //	and right operand scanned from the data line. You need a function do_next_op that performs the required operation. 
    //	do_next_op has two input parameters  (the operator and operand) and one input/output parameter (the accumulator). The valid operators are:
    //
    //+ 	add
    //- 	subtract
    //* 	multiply
    /// 	divide
    //^ 	power (raise left operand to the power of the right operand)
    //q	quit
    //
    //Your calculator should display the accumulator value after each operation. A sample run follows.
    //+ 5.0
    //Result so far is 5.0
    //^ 2
    //Result so far is 25.0
    /// 2.0
    //Result so far is 12.5
    //q 0
    //final result is 12.5
    
    
    #include <stdio.h>
    #include <math.h>
    
    void scan_data ( float *x1, char *y1);
    void do_next_op(float x, char y, float *z  );
    
    int main()
    {
    	float value, total;
    	char operand ;
    	value = 0;
    	total = 0;
    
    
    	printf("+ 	add\n- 	subtract\n* 	multiply\n/ 	divide\n^ 	power \nq	quit\n");
    	printf("0\n");
    	do
    	{
    		scan_data(&value, &operand );
    		printf("%c%.2f \n", operand , value);
    		do_next_op(value, operand, &total );
    		printf("result is %.2f \n", total);
    	}
    	while ( operand != 'q');
    
    
    }
    
    void scan_data ( float *x1, char *y1)
    {
    	float x;
    	char y;
    	scanf("%c%f", &x, &y);
    	
    	*x1 = x;
    	*y1 = y;
    }
    void do_next_op(float x, char y, float *z  )
    {
    	switch (y)
    	{
    		case '+':
    	{
    			*z =  (*z + x);
    			break;
    	}
    		case '-':
    	{
    			*z =  (*z - x);
    			break;
    	}
    		case '*':
    	{
    			*z =  (*z * x);
    			break;
    	}
    		case '/':
    	{
    			*z =  (*z / x);
    			break;
    	}
    		case '^':
    	{
    			*z = pow( *z , x);
    			break;
    	}
    	}
    	return ;
    }
    This Is my code and the question is commented at the beginning. I'm supposed to use pointers not arrays. It keeps telling me within my scan_data function that y becomes corrupt which I'm assuming will happen to x as well.

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    Code:
    void scan_data ( float *x1, char *y1)
    {
    	float x;
    	char y;
    	scanf("%c%f", &x, &y);
    	
    	*x1 = x;
    	*y1 = y;
    }
    You are reading a character into your float variable, and a float into your char variable.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    25
    Thank you. Didn't see that. My program still wont work though. It wont change the value of total and when multiplying or dividing it goes into infinite loop.

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    scanf leave a newline in the input buffer. The second time it gets called, you get a newline put in your char variable, and after that it just gets worse. You need to flush the input buffer after using it.

    Try modifying your scan data function like so:

    Code:
    void scan_data ( float *x1, char *y1)
    {
            char garbage;
    	scanf("%c%f", y1, x1); //You don't actually need all those extra variables
            while((garbage=getchar())!='\n' && garbage!=EOF); //discards characters from the input buffer until it either eats a newline, or runs out of input
    }
    I haven't tested this, but it should do the trick.
    Last edited by KBriggs; 11-25-2010 at 07:59 PM.

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    25
    Sorry to make you go through the effort I edited the code and realized I was entering the values in the wrong order. It works with this new code now. And thank you for the help.
    Code:
    //Q3- Write a program to model a simple calculator. Each data line should consist of the next operation 
    //to be preformed from the list below and the right operand. Assume the left operand is the accumulator value 
    //	(initial value of 0). You need a function scan_data with two output parameters that returns the operator 
    //	and right operand scanned from the data line. You need a function do_next_op that performs the required operation. 
    //	do_next_op has two input parameters  (the operator and operand) and one input/output parameter (the accumulator). The valid operators are:
    //
    //+ 	add
    //- 	subtract
    //* 	multiply
    /// 	divide
    //^ 	power (raise left operand to the power of the right operand)
    //q	quit
    //
    //Your calculator should display the accumulator value after each operation. A sample run follows.
    //+ 5.0
    //Result so far is 5.0
    //^ 2
    //Result so far is 25.0
    /// 2.0
    //Result so far is 12.5
    //q 0
    //final result is 12.5
    
    
    #include <stdio.h>
    #include <math.h>
    
    void scan_data ( float *x1, char *y1);
    void do_next_op(float x, char y, float *z  );
    
    int main()
    {
    	float value, *val,*tot, total;
    	char operand, *oper ;
    	val = &value;
    	tot = &total;
    	oper = &operand;
    	value = 0;
    	total = 0;
    
    
    	printf("+ 	add\n- 	subtract\n* 	multiply\n/ 	divide\n^ 	power \nq	quit\n");
    	printf("enter in the form of your value then your operator. ie 5-.\n");
    	printf("0\n");
    	do
    	{
    		scan_data(val, oper );
    		printf("%c%.2f \n", *oper , *val);
    		do_next_op(value, operand, &total );
    		printf("result is %.2f \n", total);
    	}
    	while ( operand != 'q');
    
    
    }
    
    void scan_data ( float *x1, char *y1)
    {
    	float x;
    	char y;
    	scanf("%f%c", &x, &y);
    	
    	*x1 = x;
    	*y1 = y;
    }
    void do_next_op(float x, char y, float *z  )
    {
    	switch (y)
    	{
    		case '+':
    	{
    			*z =  (*z + x);
    			break;
    	}
    		case '-':
    	{
    			*z =  (*z - x);
    			break;
    	}
    		case '*':
    	{
    			*z =  ((*z) * x);
    			break;
    	}
    		case '/':
    	{
    			*z =  ((*z) / x);
    			break;
    	}
    		case '^':
    	{
    			*z = pow( *z , x);
    			break;
    	}
    	}
    	return ;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help modifying my calculator!!
    By Matus in forum C Programming
    Replies: 5
    Last Post: 03-25-2008, 12:03 PM
  2. Simple Socialising Chat Bots
    By bengreenwood in forum C++ Programming
    Replies: 10
    Last Post: 11-28-2007, 08:42 AM
  3. simple calculator
    By HunterCS in forum C++ Programming
    Replies: 10
    Last Post: 07-18-2007, 06:51 AM
  4. A Simple Calculator Program Not Working
    By oobootsy1 in forum C++ Programming
    Replies: 9
    Last Post: 01-09-2004, 09:34 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM