Please could someone assist me with double digits

This is a discussion on Please could someone assist me with double digits within the C Programming forums, part of the General Programming Boards category; Add more code to debug the problem then. Print what is happening at each stage - you will sooner or ...

  1. #31
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Add more code to debug the problem then. Print what is happening at each stage - you will sooner or later find where it's going wrong.

    Note: You should aim to change the code as little as possible when adding these prints. Just going round making random changes to the code isn't really that helpfull unless you have a fair idea what you're trying to fix.

    Is your code compiling without warnings now?

    --
    Mats

  2. #32
    Registered User
    Join Date
    Jul 2007
    Posts
    49
    It compiles without warnings, or any error messages. You said something about space
    I used if(c==' ') position++; before the isdigit(), is it correct? Again I ask is the loop I used ok. Just help me out so I can move forward, I have some other code to write, this just got me stucked for days now. Please so I can move forward.

  3. #33
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I haven't added any loop to your code, but you probably don't want to just update the position, as "c" is still a space after that. It's probably better to leave the "position++" in one place in the for-loop (at least that's what I did). And I didn't do much more than that (and remove some unused variables, fixed the stack underflow detection problem I told you about earlier).

    --
    Mats

  4. #34
    Registered User
    Join Date
    Jul 2007
    Posts
    49
    I've amended th pop as follows I guess that should check for the underflow, but still it's not working
    Code:
    float pop(ps)
    struct stack *ps;
        {
        
    if (ps->top==-1)
              {
    printf("%s", "stack underflow");
    exit(1);
              }
    else
           return(ps->items[ps->top--]);
         }

  5. #35
    Registered User
    Join Date
    Jul 2007
    Posts
    49
    Are you saying the position++ is not supposed to appear under the if(c==' ') ?

  6. #36
    Registered User
    Join Date
    Jul 2007
    Posts
    49
    Quote Originally Posted by matsp View Post
    I haven't added any loop to your code, but you probably don't want to just update the position, as "c" is still a space after that. It's probably better to leave the "position++" in one place in the for-loop (at least that's what I did). And I didn't do much more than that (and remove some unused variables, fixed the stack underflow detection problem I told you about earlier).

    --
    Mats
    But the position++ is in the for-loop, I don't get you hear, are u saying I should remove the one after the if(c==' ') ?

  7. #37
    Registered User
    Join Date
    Jul 2007
    Posts
    49
    Mats, I got it thanks for your time but try to be more lenient.
    Last edited by ben2000; 07-31-2007 at 03:53 PM.

  8. #38
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm trying to teach you how do solve the problems - because me solving them is a few minutes because I've solved MANY similar problems.

    I'm sure you've spent a lot of time on this, but in all honesty, if you had some experience in debugging your own code, you wouldn't have spent as much time - and that's what I'm trying to teach you.

    If I just say add a "if (c != ' ')" before the else, you won't learn how to debug, you will just have code that "happens to work because someone else solved that particular problem".

    So does "23 45 + +" work "correctly" now?

    --
    Mats

  9. #39
    Registered User
    Join Date
    Jul 2007
    Posts
    49
    You are right, it's working properly now, but I still have a problem and that's the next stage in my code. You know I'm using numbers as direct infix input. I want to use variable instead of numbers as infix. Say I have red*blue+green as infix, where these are red, blue, green are variables representing array columns for n*3 array. Get the row values and use it to substitute the variables in the infix before proceding to postfix. That's the next thing I'm trying to do. I hope you guide me through.

  10. #40
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Doesn't sound too difficult.

    Start with getting a piece of code that can recognise a variable, then add substitution for the actual value of the variable - the rest should be pretty easy.

    I would keep a very simple data structure for the variables, just a string and the value.

    --
    Mats

  11. #41
    Registered User
    Join Date
    Jul 2007
    Posts
    49
    ok, I'm on it now

  12. #42
    Registered User
    Join Date
    Jul 2007
    Posts
    49
    this is what I came up with so far but my but being that the array is double it's a bit difficult getting the elements into string expression and passing it to my infix.
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #define MAXCOLS 80
    
    int main()
    {
       const int num_operand =3;
        const int num_operatr = 2;
       const int strglen=num_operand+num_operatr;
        char operand;
        int i, j;
        char string[MAXCOLS];
        char getexp[strglen+1];
        double array[3][3]={23.4,13,12,3,6.5,21,5,4,6};
                     
    
        printf("Enter an expression:\n");
        fgets(getexp, sizeof getexp, stdin);
    
        for(j=0; getexp[j] != '\0'; j++)
        {
           operand=getexp[j];
           
           if(operand >= 'A' && operand <='Z')
               string[j]=array[operand-(int)('A')];
           else
               string[j]=(char)operand;
        }
        string[j] ='\0';
    
        printf("Expression\n");
        printf("&#37;s", string);
        
        getchar();
        return 0;
     }

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 03:44 PM
  2. C++ to C Conversion
    By dicon in forum C Programming
    Replies: 7
    Last Post: 06-11-2007, 08:38 PM
  3. need some help with last part of arrays
    By Lince in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 08:13 AM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  5. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 05:54 PM

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