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. #31
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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.
    My best code is written with the delete key.

  2. #32
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    Cheers to that!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #33
    Registered User
    Join Date
    Feb 2008
    Posts
    84
    >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. #34
    Registered User
    Join Date
    Feb 2008
    Posts
    84
    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. #35
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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. #36
    Registered User
    Join Date
    Feb 2008
    Posts
    84
    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. #37
    Registered User
    Join Date
    Feb 2008
    Posts
    84
    I know in PASCAL it would be something like
    Readln(Xreal, Yreal, Zreal)

    for 23 3489 348.347 type input

  8. #38
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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, "(%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.
    Last edited by tabstop; 02-25-2008 at 03:47 PM. Reason: did it right this time

  9. #39
    Registered User
    Join Date
    Feb 2008
    Posts
    84
    Great. Thanks.

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

Similar Threads

  1. handling basic math functions
    By MyDestiny in forum C++ Programming
    Replies: 3
    Last Post: 03-02-2005, 01:12 PM
  2. basic question to Arrays
    By doneirik in forum C++ Programming
    Replies: 1
    Last Post: 01-25-2005, 02:57 AM
  3. Basic Math Problem. Undefined Math Functions
    By gsoft in forum C Programming
    Replies: 1
    Last Post: 12-28-2004, 03:14 AM
  4. a simple math question
    By chunlee in forum C Programming
    Replies: 3
    Last Post: 11-10-2004, 07:04 AM

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