# A basic math programming question

This is a discussion on A basic math programming question within the C Programming forums, part of the General Programming Boards category; >Everything has its ups and downs, but it's easier to check for errors >and no need with annoying syntax to ...

1. >Everything has its ups and downs, but it's easier to check for errors
>and no need with annoying syntax to clear input buffers.
Don't read it as me knocking fgets. I think it's a good solution to the gets/scanf problem, but "use fgets!" seems to have become something religious among some C programmers, and we don't want to mislead rookies into thinking that all they need to do for great code is use fgets.

2. Cheers to that!

3. >and we don't want to mislead rookies into thinking that all they need to do for great code is use fgets.

I, for one, don't think that's all I need.

4. actually, even with atof I have problems if the input values for Xreal, Yreal, Zreal, Zforce have different numbers of decimals from one another.

?

5. A thought without looking over your shoulder: since your buffers are, for whatever reasons, only eight characters long, you can only get seven characters (plus the \0) from the input. So: if you type "12345.678" the first fgets will peel off "12345.6", while the second fgets will then peel off "78\n" -- hence it won't ask for input here, since there's already perfectly good input sitting in the input stream. You can fix that by changing 8 to, say, 25 (or some other number that people are probably not going to type that many digits).

6. yes indeed!
thanks.
ok one last question for me today:
if instead of asking for Xreal, then Yreal, then...
I wanted to ask for the input in the form of either:
(23, 3489, 348.387) or if that is too difficult because of the other characters,
23 3489 348.387

7. I know in PASCAL it would be something like
Readln(Xreal, Yreal, Zreal)

for 23 3489 348.347 type input

8. I would probably do something like this:
Code:
```char input_buffer[80];
int check_input;

fgets(input_buffer, sizeof input_buffer, stdin);
check_input = sscanf(input_buffer, "(&#37;f,%f,%f)", &Xreal, &Yreal, &Zreal);
while (check_input != 3) {
//bad input do it again
}```
Assuming your prompt gets people to type the numbers in with () around them and commas in between. The "formatted input" functions ?scanf work reasonably well, if you know you have formatted input.

9. Great. Thanks.

Page 3 of 3 First 123
Popular pages Recent additions