Thread: Trying to find out which character was entered

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    5

    Trying to find out which character was entered

    I'm trying to make a reverse polish notation calculator.
    One of my first steps is to figure out whether an operand (number) was entered or an operator/letter (l, p, q, +, -, *, or /).
    Here's what I have for this part:
    Code:
         
     char buff[MAX_BUFFER_SIZE], o;
    /*  This reads a string from the stack  */        if( scanf("%s", buff) < 1 )
          {
             printf("I did not understand\n");
             return 0;
          }
    
    
          /* checks to see if the user entered a number */
          if(isdigit(buff[0]) || isdigit(buff[1]))
          {
             /*turns string into number and puts value in x*/
             sscanf( buff, "%lf", &x);
             /* place statements here to handle getting a number */
    
    
           
          } else {
             /* place statements to do operations */
    		  sscanf( buff, "%lf", &o);
              if (o == 'l')  {
         printf("its l");
        }
      else if (o == 'q')  {
         printf("its q");
        }
      else  (o == 'p');  {
         printf("its p");
        }
    Obviously the intention of this part is not to print "its _" but I have it set for troubleshooting.
    When I compile, any operator or letter I enter will print "its p".
    I had it as a switch, but gave up on that and am now trying if/else.

    Any tips will be great.

    NOTE: We haven't learned isdigit() or sscanf() in class (that part of the program was given to us by our instructor), so therefore we are not allowed to use it anywhere else in the program.

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    sscanf( buff, "%lf", &o); gives you a floating point number. using a character as the variable to be assigned will give you undefined behavior. change the "%lf" to "%c"

    p.s. one way to debug this kind of input is to insert printfs after each scanf operation to see what you got.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    5
    Thanks for the reply.
    I did what you said and put
    Code:
    sscanf( buff, "%c", &o);
    printf("%c\n", o);
    and then my if/else statements.
    I'm still getting the same problem. My output looks like:
    v /*I entered this*/
    v
    its p
    _

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Corey Bullard View Post
    [/CODE]
    and then my if/else statements.
    I'm still getting the same problem. My output looks like:
    v /*I entered this*/
    v
    its p
    _
    Of course it is.
    Code:
    else  (o == 'p');  {
         printf("its p");
        }
    That's not an "else if", that's "else, o == 'p' ... prompting a warning from your compiler ... ; <--- END OF STATEMENT" "new statement, 'its p'"

    Now I don't know exactly what you expect it to do, but that's not it:
    Code:
    if( a == b )
        ... do something for when a is b
    else
    if( a == c )
        ... do something for when a is c
    ...
    else
        ... do something for when all else fails
    ...
    this executes regardless of whatever happens above, assuming your code reaches this far (you didn't return or exit)
    That's how an if-else chain works.


    Quzah.
    Last edited by quzah; 04-02-2012 at 07:48 PM.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    5
    Ah, OK, I get it now.
    I put an else at the end to handle all other cases (and print invalid character), and now it seems to work well.
    Thanks for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 23
    Last Post: 04-04-2011, 11:20 AM
  2. Write a program to find the factors of any number entered.
    By rajesh10071986 in forum C Programming
    Replies: 5
    Last Post: 01-20-2010, 03:15 PM
  3. Replies: 4
    Last Post: 11-01-2009, 06:19 AM
  4. Replies: 18
    Last Post: 07-06-2009, 07:12 AM
  5. Find '&' character in char array?
    By tidemann in forum C Programming
    Replies: 7
    Last Post: 10-19-2006, 05:04 AM