### Thread: Questions on scanning data

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

Join Date
Jun 2013
Posts
142
Rep Power
6

#### Questions on scanning data

edit:
I forgot to insert the result image.

this is the code of a program I wrote.

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"
"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. 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)
3. No Profile Picture
046
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2013
Posts
142
Rep Power
6
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".
4. 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.
5. No Profile Picture
046
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2013
Posts
142
Rep Power
6
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