Thread: A little confused

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    19

    A little confused

    insert
    Code:
    
    
    Code:
    My Code
    #include<stdio.h>
    main()
    {
       int a,b,c=0;
       while(c!='#')
       {
       printf("please enter a new sequence\n"); 
       a=getchar() - '0';
       b=getchar() - '0';
       c=getchar();
       if(c=='*')
       {
        int product=a*b;
        printf("(%d) * (%d) = (%d) \n",a,b,product);
       }
       if(c=='/')
       {
        int divider=a/b;
        printf("(%d) / (%d) = ( %d)\n",a,b,divider);
       }
        if(c=='+')
       {
        int add = a+b;
        printf("(%d) + (%d) = (%d)\n",a,b,add);
       }
        if(c=='-')
       {
        int subtract=a-b;
        printf("(%d) - (%d) = (%d)\n",a,b,subtract);
       }
    
    
       }
       printf("good bye");
    }
    I have to write code that terminates the program when '#' is included which I have done and when I type in 34+
    it should display 3+4=7 and same with 34* it should display 3*4=12 where my error lies is that when I run the program through the while loop it gives me 
    please enter a new sequence 
    34+
    3 + 4=7
    please enter a new sequence 
    34-
    please enter a new sequence 
    34-
    please enter a new sequence 
    please enter a new sequence 
    34-
    please enter a new sequence 
    34-
    3-4=-1
    Can anyone tell me why it is like that and how to fix it im a confused C programmer

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    After you input "34+", you press enter. That puts a newline ('\n') in the input buffer. So the actual input the program sees is "34+\n". That means the second time through the loop, when you enter "34-\n", a is '\n' (since it was left over from the previous time), b is '3' and c is '\4'. You don't have a valid operator, so it does nothing, and you have "-\n" left in the input buffer. The next time you enter "34*\n". But given the leftovers, a is '-', b is '\n' and c is '3'.

    So, you need to "eat up the remaining characters after you read your two operands, and the operator. Use the getchar/loop trick here: FAQ > Flush the input buffer - Cprogramming.com.

  3. #3
    Registered User
    Join Date
    Mar 2014
    Posts
    19
    I have not learnt this method yet in class is there a counter solution in order to fix this problem.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I don't know what you mean by "counter solution". Counter like for counting 1, 2, 3, ...? Or counter meaning "against" like a fix? Did you read the link I posted (that red text is a link, click it)? Did you understand it? You just need to copy-paste from there the simple loop with the getchar in it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. this is fun but Im new and confused
    By c-hopper in forum C Programming
    Replies: 48
    Last Post: 10-21-2008, 10:05 PM
  2. confused
    By clipsit in forum C Programming
    Replies: 6
    Last Post: 03-20-2008, 01:00 PM
  3. confused
    By mopar123 in forum C Programming
    Replies: 18
    Last Post: 09-01-2005, 04:06 PM
  4. So Now Im getting confused?!?!?
    By zergdeath1 in forum C++ Programming
    Replies: 11
    Last Post: 03-06-2004, 05:41 PM
  5. im so confused???
    By Flim Flam in forum C Programming
    Replies: 7
    Last Post: 07-01-2003, 07:35 AM