Thread: Simple calculator program not letting me use the same operator (+.-.*./) twice

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    24

    Simple calculator program not letting me use the same operator (+.-.*./) twice

    I am trying to make a simply calculator that allows the user to continually modify a result using +,-,*, or / , however for some reason my code is only letting me use each operator (+,-,*, or /) one time, before giving me the error message that is supposed to only appear when the user enters a character that is not one of the four basic operators (+,-,*, or /). Actually no matter what I enter the second time it does this. Here is the code. Please help:


    Code:
    //
    //  main.c
    //  calc
    //
    //
    //
    
    
    #include <stdio.h>
    #include <stdlib.h>
    
    
    
    
    #define QUIT 0
    #define CONTINUE 1
    
    
    //---------------math prototype--------------------------
    int math (double first, double second, char operator);
    
    
    int main (void)
    {
        
        int equation=CONTINUE;
        char operation='+';
        double result=10.0, modifier;
    
    
        
        printf("calculator is on.\n");
        
        //printf("%d\n",equation);
        
        while (equation==CONTINUE) {
            
            
    //reading user input of operation and a number:
        scanf("%c%lf", &operation, &modifier);    
    
    
            
    //sending the two values of "result" and "modifier" and the operator character to subfunction called "math"
    // and putting the result of "result"(+ or - or * or /)"modifier" in to the variable "result":
        result = math(result, modifier, operation);
    
    
        
            printf("result=%f\n", result);
        
        }
            
        return 0;
        
    
    
    }
    
    
    
    
    //----------math-----------------------------------------------------
    
    
    int math (double first, double second, char operator)
              {
                  
                  if (operator=='+')
                      return (first+second);
                  
                  else if (operator=='-')
                      return (first-second);
                  
                  else if (operator=='*')
                      return (first*second);
                  
                  else if (operator=='/')
                      return (first/second);
                  
                  else 
                    printf("%c is an unknown operation. Reenter your last line:\n", operator);
                      return (first);
                  
              }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    After you enter your operator and another operand, you hit the enter key. It gets stored in the input buffer (as a '\n'), so the next time through the loop, your scanf call reads the '\n' and thinks it's an invalid operator. Read this for some possible solutions: Cprogramming.com FAQ > Flush the input buffer.
    Last edited by anduril462; 10-03-2011 at 10:02 AM.

  3. #3
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    is a '\n' key always stored in the buffer or is it only stored after you have entered a character first. example you enter in a integer, then a char, will the char see the \n or is that only if you enter in a char then another char?

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by camel-man View Post
    is a '\n' key always stored in the buffer or is it only stored after you have entered a character first. example you enter in a integer, then a char, will the char see the \n or is that only if you enter in a char then another char?
    The newline char is always stored in the buffer, however since scanf is formatted input you will only see it when you try to input a char. To tell scanf to ignore whitespace characters, you can put a leading space in the format string.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by camel-man View Post
    is a '\n' key always stored in the buffer or is it only stored after you have entered a character first. example you enter in a integer, then a char, will the char see the \n or is that only if you enter in a char then another char?
    The best thing to do with scanf() and questions like this is to write a short program and experiment to see what happens.

    '\n' is always in the buffer in sequence with everything else, eg, if you type "3 abc" and hit enter, the buffer will contain "3 abc\n". However, some scanf formats (such as %d) will "intelligently" skip it. Again, thoughtful experimentation should resolve any ambiguities you find in your documentation.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by camel-man View Post
    is a '\n' key always stored in the buffer or is it only stored after you have entered a character first. example you enter in a integer, then a char, will the char see the \n or is that only if you enter in a char then another char?
    Question: Did you press the <enter> key?
    Answer: Yes.
    Result: It's in the buffer.

    Even if you enter nothing and hit <enter> ....

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    24
    Thanks Andrew! Putting a space between opening quotes and the %c did the trick! That was really annoying. I thought C ignored white space characters...I guess this rule does not apply 100% of the time or it only doesn't apply inside quotations? This is a strange issue.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Charlie Lesoine View Post
    Thanks Andrew! Putting a space between opening quotes and the %c did the trick! That was really annoying. I thought C ignored white space characters...I guess this rule does not apply 100% of the time or it only doesn't apply inside quotations? This is a strange issue.
    C does what you tell it to do... and you weren't telling it to ignore whitespace.

  9. #9
    Registered User
    Join Date
    Oct 2011
    Location
    india
    Posts
    18
    what do you meant by this program?is it like this?
    1+1
    2 then you want to use 2 as your 1st operand for the 2nd time?
    if that is the case,then this program will not generate the result

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by vikasvijayan View Post
    what do you meant by this program?is it like this?
    1+1
    2 then you want to use 2 as your 1st operand for the 2nd time?
    if that is the case,then this program will not generate the result
    Please don't post nonsense.

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by CommonTater View Post
    Please don't post nonsense.
    I actually kinda followed what that poster wrote, but at the same time I'm not impressed with most of the posts from that poster.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to write a program of a simple calculator?
    By yeohwl91 in forum C Programming
    Replies: 5
    Last Post: 09-24-2011, 04:04 PM
  2. Write a program to model a simple calculator
    By Annihilate in forum C Programming
    Replies: 5
    Last Post: 11-22-2010, 01:14 PM
  3. Replies: 15
    Last Post: 12-31-2007, 06:26 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. Replies: 7
    Last Post: 07-19-2002, 04:33 AM

Tags for this Thread