Thread: need help with calculator

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    92

    need help with calculator

    I just started to learn programming in C. Was trying to make a programm to evaluate simple calculations. It's working but I can't end this program. My "End of the calculations"doesn't work. Can someone tell me where I am wrong.
    Thank you.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Code:
    ./temp
    S - to set number to Accumulator
    E - to end calculation
    
    Begin Calculation
    8S
    =8.00
    4+
    =12.00
    3-
    =9.00
    0E
    =9.00
    End of the calculations
    What's wrong with that?

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    92
    I must be doing something wrong see my output
    S - to set number to Accumulator
    E - to end calculation

    Begin Calculation
    10S
    =10.00
    5+
    =15.00
    2-
    =13.00
    0E
    Uknown operator

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Surely your runtime isn't treating 0E as a valid float? What happens if you do "0E0E"?

    If you want to see what your system thinks the operator is, print it in your unknown operator message.

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I don't see anything wrong with the code. I mean your output is formatted rather ponderously, but whatever.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    92
    You are right it worked with 0E0E. and it work when I replace uknown operator message on " End of calculations ".

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Fix:
    Code:
    // program to evaluate simple calculatins
    
    #include<stdio.h>
    #include<ctype.h>
    
    
    int main(void)
    {
    	float total=0; // accumulator
    	float a;
    	char op;
    
    	char buffer[256];
    
    	printf("S - to set number to Accumulator\n");
    	printf("E - to end calculation\n\n");
    	printf("Begin Calculation\n");
    	
    	
    	
    	do{
    		if(fgets(buffer, sizeof buffer, stdin) == NULL)
    			break;
    
    		if(toupper(buffer[1]) == 'E' && buffer[2] == '\n')
    		{
    			printf("=&#37;.2f\n",total);
    			printf("End of the calculations\n");
    			break;
    		}
    
    		sscanf(buffer, "%f%c",&a,&op);
    
    		switch(op)
    		{
    			case'S':				//Set accumulator
    				printf("=%.2f\n",a);		
    				total=a;
    				break;
    			case'+':				//Addition		
    				total=total+a;
    				printf("=%.2f\n",total);
    				break;
    			case'-':				//Subtraction
    				total=total-a;
    				printf("=%.2f\n", total);
    				break;
    			case'*':				//Multiplication			
    				total=total*a;
    				printf("=%.2f\n", total);
    				break;
    			case'/':				//Division
    				if (a==0)
    					printf("Division by zero\n");
    				else
    					total=total/a;
    				printf("=%.2f\n", total);
    				break;
    			default:				//default
    				printf("Uknown operator\n");
    				break;
    		}
    	} while(op!='E');
    
    	return 0;
    }

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    92
    Thank you tabstop and master5001. All additions to the code what master5001 advised are new to me, I mean we did no learn that yet, anyway that great to understand that there is someone who always will help you. Again thanks guys.

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    To give you a quick overview of what my code does differently is that it copies the whole line into a memory buffer before it runs scanf(). I use sscanf() because unlike scanf() it reads a chunk of memory instead of the keyboard input (which is highly over simplifying what scanf() really does, but I like to keep things simple for explanation's sake).

    After it reads in the line it checks to see if the second character in the line is 'e' or 'E' (it is not case sensitive in my code) and also checks to make sure that 'e' is the last character in the line (just to make sure to complain if the user goes crazy with their input). If the line is in the appropriate form, it ends the calculation before even trying to see what operators you may have tried to use.

    I also killed off all your global variables as they need not be globals to begin with. Here is a cool trick that doesn't change your code very much at all.

    Code:
    // program to evaluate simple calculatins
    
    #include<stdio.h>
    
    int main(void)
    {
    	float total=0; // accumulator
    	float a;
    	char op;
    
    	printf("S - to set number to Accumulator\n");
    	printf("E - to end calculation\n\n");
    	printf("Begin Calculation\n");
    	
    	do{
    		if (scanf("&#37;f%c",&a,&op) != 2)
    		{
    			printf("=%.2f\n",total);
    			printf("End of the calculations\n");
    			break;
    		}
    
    		switch(op)
    		{
    			case'S':				//Set accumulator
    				printf("=%.2f\n",a);		
    				total=a;
    				break;
    			case'+':				//Addition		
    				total=total+a;
    				printf("=%.2f\n",total);
    				break;
    			case'-':				//Subtraction
    				total=total-a;
    				printf("=%.2f\n", total);
    				break;
    			case'*':				//Multiplication			
    				total=total*a;
    				printf("=%.2f\n", total);
    				break;
    			case'/':				//Division
    				if (a==0)
    					printf("Division by zero\n");
    				else
    					total=total/a;
    				printf("=%.2f\n", total);
    				break;
    			default:				//default
    				printf("Uknown operator\n");
    				break;
    		}
    	} while(op!='E');
    
    	return 0;
    }

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. GUI Calculator - Critique
    By The Brain in forum Windows Programming
    Replies: 1
    Last Post: 02-25-2006, 04:39 AM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. Need help with calculator program
    By Kate in forum C# Programming
    Replies: 1
    Last Post: 01-16-2004, 10:48 AM
  5. Replies: 2
    Last Post: 05-10-2002, 04:16 PM

Tags for this Thread