#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    142
    Rep Power
    2

    Questions on scanning data


    edit:
    I forgot to insert the result image.

    this is the code of a program I wrote.
    I want to ask about the behavior of this loop.

    Code:
    #include <stdio.h>
    #include <math.h>
    
    void get_data (double *rp, char *opp);
    void next(double r, char op, double *x);
    
    int main()
    {
        double r;
        char op;
        double x = 0.0;
    
        get_data(&r, &op);
    
        while (op != 'q')
        {
            next(r, op, &x);
            printf("result so far is %.1f\n", x);
            get_data(&r, &op);
        }
    
        printf("final result is %.1f\n", x);
    }
    void get_data (double *rp, char *opp)
    {
        scanf("%c %lf", opp, rp);
    
    }
    void next(double r, char op, double *x)
    {
        *x = r;
    }
    in running this program, I got this result.


    I'm guessing the extra
    "result so far is 3.0"
    happened because of the newline character in the input buffer.
    what I don't understand is why I got
    "result so far is 3.0"
    instead of
    "result so far is 2.0"
    on the 2nd line.

    my analysis of the loop is

    enter + 3\n
    op = +, r = 3, input buffer: \n

    op != q is true
    "result so far is 3.0"
    enter - 2\n
    op = \n, r = 2, input buffer: -, 2

    op != q is true
    "result so far is 2.0"
    op = -, r = 2

    op != q is true
    "result so far is 2.0"
    ...

    why is the 3 used the second time?
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,213
    Rep Power
    2222
    Don't have time to follow the logic close, but I have a stupid question.

    scanf("%c %lf", opp, rp);

    Is it your intention read in the previous scanf's Enter key's code (a newline, '\n') and assign it to op (via the pointer, opp)? Or did you instead want to assign a character that the user would type in?

    Would this give you the results you expect?:
    scanf(" %c %lf", opp, rp);
    (please note the space in front of the %c)
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    142
    Rep Power
    2
    Is it your intention read in the previous scanf's Enter key's code (a newline, '\n') and assign it to op (via the pointer, opp)? Or did you instead want to assign a character that the user would type in?
    I wanted to assign a character.
    what the problem does is scan an operator and number, and does arithmetic.

    Would this give you the results you expect?:
    it does, and actually I was able to come up with that solution thanks to what you taught me earlier in another thread.
    but what I don't understand is why the extra line displayed by the program was
    "result so far is 3.0"
    instead of "result so far is 2.0".
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,213
    Rep Power
    2222
    What was the return value of scanf each time? When a conversion fails, the variable that would have received the converted value is not changed. So without stepping through the program rigorously, I would think that the second scanf call, the one that you didn't intend to have, failed to do the conversion for rp and hence r did not get changed.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    142
    Rep Power
    2
    When a conversion fails, the variable that would have received the converted value is not changed.
    thanks, I think this answers my question.

    what probably happened was

    1st data entry: + 3.0\n
    end of first loop: \n is stored in opp, but there are no more data in the input buffer, so conversion for rp fails.
    therefore, op gets \n and rp retains the value 3.0


IMN logo majestic logo threadwatch logo seochat tools logo