trying to fix this code...

This is a discussion on trying to fix this code... within the C Programming forums, part of the General Programming Boards category; I'm working on postfix notation calculator. Using stacks (array implementation. I used code from my textbook, apart from functions, I'm ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    99

    trying to fix this code...

    I'm working on postfix notation calculator. Using stacks (array implementation. I used code from my textbook, apart from functions, I'm allowed to do this. However, It does not work. After fixing lots of mistakes (from the code from the textbook), I get the following result:
    Enter the postfix string: 4 5 +
    5
    Value of the expression is: 0.
    I have 3 hours left to finish this, please help me.
    The code is in the file.
    Thanks for taking a look at this.
    Attached Files Attached Files

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    You're very close and your code style is excellent. I didn't immediately spot the error but my compiler did:
    Code:
    stack.c(43) : warning C4700: local variable 'c' used without having been initialized
    Also, remember that atoi expects a nul terminated string.

    So in summary, you need to change the highlighted section of the code:
    Code:
       for(i = 0; i < howlong; i++)
       {  
          s[0] = c;
          
          PostfixString[i] = c;
          
          if(isdigit(c))
          {
    You should also read this faq entry.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    99
    I did read, unfortunately it is a little above the level of my C knowledge...
    I also made some changes, and now the stack is empty and I cannot retrieve the result.
    The changes were made in the following part of code:
    Code:
     int LeftOperand, RightOperand, Result;
       int i;
       char c;
       char *s = "";  
      /* int howlong; */
       Stack EvalStack; 
       char *PostfixString;
    
    
       initialize(&EvalStack);
    
       printf("Enter the postfix string: ");
       gets(PostfixString); 
       
       /* howlong = strlen(PostfixString);
       printf("%d \n", howlong);  */
    
       for(i = 0; PostfixString[i] != '\0'; i++)
       {

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    That section of code was fine except for the use of gets instead of fgets. The changes required have been highlighted:
    Code:
       printf("Enter the postfix string: ");
       fgets(PostfixString, sizeof(PostfixString), stdin); /* gets is bad. */
       
       howlong = strlen(PostfixString);
       printf("%d \n", howlong);
    
       for(i = 0; i < howlong; i++)
       {  
          c = PostfixString[i]; /* Get a character, this was the wrong way around. */
    
          s[0] = c;
          s[1] = '\0'; /* Make sure the number string is nul terminated. */
          
          if(isdigit(c))
          {

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    99
    Thank you, thank you! We did not go through the fgets, fscanf etc functions yet, but I do not think it will be a problem. Can't wait for summer to have more time to study...
    Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help to fix code
    By anik18 in forum C Programming
    Replies: 19
    Last Post: 05-29-2009, 07:06 AM
  2. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  3. Replies: 1
    Last Post: 05-26-2006, 09:13 AM
  4. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21