Thread: Calculator using getchar, putchar

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    3

    Question Calculator using getchar, putchar

    Hi. I need help for a school project. I need to creat a calculator program using only getchar and putchar. the user has to enter the statement and the program should echo the statement and the answer. for example the user has to enter " 3 + 6 " and the program should answer " 3 + 6 = 9".
    I appreciate any help.

    here is what I have. and I have no idea what's wrong.

    Code:
    #include <stdio.h>
    #include <math.h>
    char Cont (void);
    int isdigit1 (char C);
    int Convert(char );
    int main(void)
    {
     int Error = 0 , Num1 , Num2, Oper;
     char Flag, c;
     double Result;
     do
     {
      while ( ( c=getchar() ) != EOF)
      {
       if ((c == ' ') || (c = '\t'))
       {
        putchar(c);
        continue;
       }
     
       if /*while*/ (isdigit1 (c))
       {
        Num1 = Convert(c);
       } 
       if ((c=='+') || (c=='-') || (c=='*') || (c=='/') || (c=='%'))
       {
        switch (c) 
        {
        case '+':
         Oper = 1; // current month days
         break;
     
        case '-': 
         Oper = 2;
         break;
     
        case '*': 
         Oper = 3;
         break;
     
        case '/': 
         Oper = 4;
         break;
     
                 case '%': 
         Oper = 5;
         break;
        }
        putchar (c);
       }
     
       if /*while*/ (isdigit1 (c))
       {
        Num2 = Convert(c);
       }
     
       switch (Oper)
       {
       case 1:
        Result = (double)(Num1 + Num2);
        break;
     
       case 2:
        Result = (double)(Num1 - Num2);
        break;
     
       case 3:
        Result = (double)(Num1 * Num2);
        break;
     
       case 4:
        Result = (double)(Num1 / Num2);
        break;
     
       case 5:
        Result = (double)(Num1 % Num2);
        break;
       }
       printf (" = %.2f", Result);
     
       Flag = Cont();
      }
     }while ((Flag == 'y') || (Flag == 'Y') ); 
     return 0;
    }
     
    int Convert (char  c)
    {
     int ch,
      sum = c - '0';
     while ((( ch = getchar() ) != ' ' ) && (ch != '\n' ))
     {
      if ( !isdigit1 (ch) )
       break;
      sum = sum *10 + ( ch - '0');
      putchar(ch);
     }
     return (sum);
    }
    int isdigit1 (char C)
    {
     if ((C>= '0') && (C<= '9'))
      return 1;
     return 0;
    }
    char Cont ()
    {
     char Flag;
     printf ("******************\nWould you like to continue?\nEnter y for yes, Anything else for no.\n");
     scanf ("%c%*c", &Flag);
     return Flag;
    }

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    I think you do have an idea what is wrong. It will either compile, or it won't. If it compiles, it will either work, or it won't. If it doesn't compile, chances are the compiler is producing messages, etc...

    Please provide more information. (Of course, all 200 people who frequent this forum and are active posters could create a new project with your code to answer these questions for themselves, but how much sense does that make?

    Please ask an intelligent question.

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    3

    Inteligent

    ok. let's make it intelligent!! It does compile with no error, but it doesn't work. somewhere my code has a problem.

  4. #4
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by arlen20002000 View Post
    ok. let's make it intelligent!! It does compile with no error, but it doesn't work. somewhere my code has a problem.
    Clearly. But if you gave us more specific information on what doesn't work. What error messages, if any, you get. Whether the output is just wrong or the program crashes. That'll help us save our telepathy powers for the really intractable bugs.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    getchar returns int - so c should be int

    Code:
    			if (isdigit1 (c))
    			{
    				Num1 = Convert(c);
    			} 
    
    			...
    
    			if (isdigit1 (c))
    			{
    				Num2 = Convert(c);
    			}
    So could you provide a scenario when this code will initilize Num1 and Num2 on different iterations of the loop?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    Code:
    while ( ( c=getchar() ) != EOF)
    The program gets hung up in the first inner while loop.

    Code:
    if ((c == ' ') || (c = '\t'))
    This conditional is always triggered, no matter what you input.
    Last edited by JDGATX; 03-26-2008 at 10:46 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 07:26 PM
  2. need help please! getchar, putchar, etc.
    By sue in forum C Programming
    Replies: 1
    Last Post: 03-21-2003, 08:40 PM
  3. Can anybody take a look at this?
    By TerryBogard in forum C Programming
    Replies: 10
    Last Post: 11-21-2002, 01:11 PM
  4. Replies: 2
    Last Post: 01-10-2002, 07:42 PM