Thread: simple calculator

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    63

    Question simple calculator

    So I'm trying to make a simple calculator, I have a general idea but need tips. So far I have this but it's not working properly, once you enter something into scanf if just exits the program without showing me a result. I know later I'll have to throw in a loop, but for now i just want it to work once through.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        double result, x;
        int op;
        result=0.0;
        printf("%f \n", result);
        scanf("%d %lf", op, x);
        
            if (op=='+')
            {
            result=result+x;
            printf("result is: %f:", result);
            }
            else if (op=='-')
            {
            result=result-x;
            printf("result is: %f:", result);
            }
            else if (op=='*')
            {
            result=result*x;
            printf("result is: %f:", result);
            }
            else if (op=='/')
            {
            result=result/x;
            printf("result is: %f:", result);
            }
        
            
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    First I don't think you want to use %d to input your op.
    stdout is usually line buffered. You can either print newline char or use fflush(stdout) or use setvbuf.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Location
    Rochester, NY
    Posts
    196
    Quote Originally Posted by yacek View Post
    So I'm trying to make a simple calculator, I have a general idea but need tips. So far I have this but it's not working properly, once you enter something into scanf if just exits the program without showing me a result. I know later I'll have to throw in a loop, but for now i just want it to work once through.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        double result, x;
        int op;
        result=0.0;
        printf("%f \n", result);
        scanf("%c %lf", &op, &x);
        
            if (op=='+')
            {
            result=result+x;
            printf("result is: %f:", result);
            }
            else if (op=='-')
            {
            result=result-x;
            printf("result is: %f:", result);
            }
            else if (op=='*')
            {
            result=result*x;
            printf("result is: %f:", result);
            }
            else if (op=='/')
            {
            result=result/x;
            printf("result is: %f:", result);
            }
        
            
    }
    You need to give scanf pointers to the variables to store things.

    As for changing %d to %c, I'm not sure much about how scanf works, but it's something to do with the format reading. %d produces a 47 for some reason when it reads ascii 43 ('+'), %c will produce the ascii 43 you need to compare that way. Not sure why, typically you read characters in as characters, ints as ints, so on and so forth.

    Though it's true that characters are often analogous with ints, when you're telling a function "how" to read something (ie. how to interpret the bits), tell it what it really is. Anything via stdin (terminal) is a character. Reading it in with scanf would only be if you were expecting a numerical value there.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    63
    hmm not sure what fflush(stdout) does exactly. I updated the code, things seem to be working now except when I want to do a subtraction it won't give me a result. Also when I try to add a return=0; at the end of the program I get a compilation error.

    Code:
    /*
    
    My name is Jack Trocinski
    
    */
    
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    	double result, x;
    	char op;
    	result=0.0;
    	printf("%f \n", result);
    		
    	while ((op!='R') || (op!='r'))
    		{
    		
    		scanf("%c %lf", &op, &x);
    		
    		if (op=='+')
    		{
    		result=result+x;
    		printf("result is: %f \n", result);
    		}
    		else if (op=='-')
    		{
    		result=result-x;
    		printf("result is: %f \n", result);
    		}
    		else if (op=='*')
    		{
    		result=result*x;
    		printf("result is: %f \n", result);
    		}
    		else if (op=='/')
    		{
    		result=result/x;
    		printf("result is: %f \n", result);
    		}
    	}
    	
    	
    }
    Last edited by yacek; 10-06-2010 at 10:24 PM.

  5. #5
    Registered User
    Join Date
    Aug 2010
    Location
    Rochester, NY
    Posts
    196
    Quote Originally Posted by yacek View Post
    hmm not sure what fflush(stdout) does exactly. I updated the code, things seem to be working now except when I want to do a subtraction it won't give me a result. Also when I try to add a return=0; at the end of the program I get a compilation error.

    Code:
    /*
    
    My name is Jack Trocinski
    
    */
    
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        double result, x;
        char op;
        result=0.0;
        printf("%f \n", result);
            
        while ((op!='R') || (op!='r'))
            {
            
            scanf("%c %lf", &op, &x);
            
            if (op=='+')
            {
            result=result+x;
            printf("result is: %f \n", result);
            }
            else if (op=='-')
            {
            result=result-x;
            printf("result is: %f \n", result);
            }
            else if (op=='*')
            {
            result=result*x;
            printf("result is: %f \n", result);
            }
            else if (op=='/')
            {
            result=result/x;
            printf("result is: %f \n", result);
            }
        }
        
        
    }
    It's "return 0;" - not "return=0;" - you're not setting the return, it's not a variable, you're telling it what to return, it can be an entire expression, ie.
    Code:
    return (a > b) ? a : b;
    Which should work fine

    fflush() flushes a stream, if you do fflush(stdout) - it will flush the stdout stream. The reason for flushes is because the stdio package caches I/O for efficiency reasons, as does the OS. Telling it to flush the given stream, is telling it to dump the given cache in the stdio package. That's not really an issue here, though sometimes it is, especially if you're printing something out right before the program crashes, and it's not showing up because it was cached when the program dies.

    Because you specified in the format that there's a space between the operator and the operand, there must be one or more spaces. entering "-5" will not work, but "- 5" will.

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    63
    Awesome, got it to work. Thanks Syndacate!

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    63
    Actually I'm still having a problem, here is the new code. The + and - only work the first time during the loop. When I use them again later it won't give me a result. Hmm.

    Code:
    /*
    
    My name is Jack Trocinski
    
    */
    
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        double result, x;
        char op;
        result=0.0;
        printf("Calculator is on.  Use R to exit the calculator.\n");
        printf("INSTRUCTIONS: Operators you may use:\n * for multiplication\n / for division\n + for addition\n - for subtraction\n");
        printf("ENTER THE OPERATOR FOLLOWED BY A NUMBER THEN PRESS ENTER.  DO NOT USE THE SPACE BAR.\n\n");
        printf("%f\n", result);
            
        while ((op!='R') || (op!='r'))
            {
            
            scanf("%c%lf", &op, &x);
            
            if (op=='+')
            {
            result=result+x;
            printf("result is: %f \n", result);
            }
            else if (op=='-')
            {
            result=result-x;
            printf("result is: %f \n", result);
            }
            else if (op=='*')
            {
            result=result*x;
            printf("result is: %f \n", result);
            }
            else if (op=='/')
            {
            result=result/x;
            printf("result is: %f \n", result);
            }
        }
        
        return 0;
    }

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    63
    ah figured it out, have to use fflush(stdin)

  9. #9
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    ah figured it out, have to use fflush(stdin)
    Which is totally wrong. RTFM
    fflush is for output files.
    http://c-faq.com/stdio/gets_flush2.html

  10. #10
    Registered User
    Join Date
    Oct 2010
    Posts
    63
    hmm. how would I use stdout in the problem then, never had any experience with it. I assume after the printf but just putting fflush(stdin) after isn't fixing the problem.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Cprogramming.com FAQ > Validate user input
    If you were to add "input cleanup" as well as "input validation", then you would soon see that raw scanf() calls are pretty inadequate to solving the problem.

    A scanf() call that terminates early, because of bad input, is in an even worse state than merely having a trailing newline to deal with.

    Reading a whole line using fgets(), THEN convert/validate with sscanf() generally solves both problems at the same time.
    The input stream is left in a usefully predictable state.

    FWIW, routines like strtod(), strtol() should be used in preference to sscanf(), since these also detect numeric overflow and the like.
    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 Socialising Chat Bots
    By bengreenwood in forum C++ Programming
    Replies: 10
    Last Post: 11-28-2007, 08:42 AM
  2. simple calculator
    By HunterCS in forum C++ Programming
    Replies: 10
    Last Post: 07-18-2007, 06:51 AM
  3. A Simple Calculator Program Not Working
    By oobootsy1 in forum C++ Programming
    Replies: 9
    Last Post: 01-09-2004, 09:34 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Simple simple graphics
    By triplem in forum C Programming
    Replies: 2
    Last Post: 05-19-2003, 02:52 AM