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; Hi, I'm trying to learn some basic ideas about programming mathematical functions in C. I would like the user to ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    84

    A basic math programming question

    Hi,
    I'm trying to learn some basic ideas about programming mathematical functions in C.
    I would like the user to be able to input a number with decimals, for instance "2.123"
    and then use this input to carry out an equation.
    Here is how I've been doing it:
    int main (int argc, char * const argv[]) {


    /* Variables */
    float Xreal;
    float Yreal;
    float Zreal;
    float Xforce;
    float Yforce;
    float Zforce;
    float Tval;
    char inXreal[8];
    char inYreal[8];
    char inZreal[8];
    char inZforce[8];


    /* Prompts */

    /* Get coordinates of real-perspective point */
    printf("Xreal value: \n");
    gets(inXreal);
    Xreal=atoi(inXreal);

    printf("Yreal value: \n");
    gets(inYreal);
    Yreal=atoi(inYreal);

    printf("Zreal value: \n");
    gets(inZreal);
    Zreal=atoi(inZreal);

    /* Get Z coordinate of forced-perspective point */
    printf("Zforce value: \n");
    gets(inZforce);
    Zforce=atoi(inZforce);


    /* Computation */
    Tval=Zforce/Zreal;
    Xforce=Tval*Xreal;
    Yforce=Tval*Yreal;

    /* Output Display */
    printf("The point: (%.3f, %.3f, %.3f) was generated with a Tvalue of: %.3f.\n",Xforce,Yforce,Zforce,Tval);
    }

    This works so long as the input values are whole numbers.
    But I would also like to be able to perform these functions using decimals.
    I understand that the problem must have something to do with using CHAR and ATOI, but I'm not certain of another way to get user input for decimals, etc.

    Can anyone offer some advice?
    Thanks!

  2. #2
    Registered User
    Join Date
    Feb 2008
    Posts
    84

    by the way

    by the way:
    I am working in Xcode 3 on a mac running os x leopard

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Since atoi stands for "alphanumeric to integer", it seems unlikely it will work for getting floating point values.

    Whatever path you're following in your quest for C knowledge, if it took you past "atoi" it should have taken you past "atof" as well. (And "atof" stands for "alphanumeric to floating point" -- it returns a value of type double.)

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >int main (int argc, char * const argv[]) {
    If you're not going to use the parameters, don't include them. Also, be sure to return a value from main, even if it's always 0:
    Code:
    int main ( void )
    {
      return 0;
    }
    >gets(inXreal);
    Never use gets. It's an unsafe function, and it's impossible to make gets safe.

    >Xreal=atoi(inXreal);
    Even if you use it with integers, atoi is generally a bad idea unless you've validated the string first. I'd recommend strtol, or in the case of floating-point values, strtod.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    84
    Hi, thanks for your very quick response.
    I suspected that was exactly the problem, and so I checked the index of the book I am using for other ato... functions, but believe it or not, they do not mention atof.

    thanks for your help!

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    84
    prelude,
    can you clarify:
    I am getting a message in terminal that gets is unsafe...
    How could I perform that same function without it?

    Printf("Input number: \n");
    WHAT GOES HERE?

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    84
    and, in the context of the above code... where do i put the "return", because my "{" then contains the rest of the program, im not sure where the return zero goes


    thanks for your help.
    im just getting started

  8. #8
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    have you read FAQ?
    you can use fgets(buffer, sizeof buffer, stdin)
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >How could I perform that same function without it?
    Sorry about that. I was in so much of a hurry that I forgot to point you toward the fgets function.

    >where do i put the "return"
    Put the return in main when you're done. A good start is right at the end, just before the closing brace.
    Code:
    int main ( void )
    {
      /* Your code here */
      return 0;
    }
    My best code is written with the delete key.

  10. #10
    Registered User
    Join Date
    Feb 2008
    Posts
    84
    ok, i get the return. thanks

    but, im not certain i understand the fgets.
    if it is "fgets(buffer, sizeof buffer, stdin)"
    what do I put for each: buffer, size of, stdin?

    all i want is to have it understand an input such as "2.123"

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    buffer = where you want the answer to go.

    sizeof buffer = size of the buffer. IOW, how many characters are in your char array? (Answer: 8.)

    stdin = stdin. You know, the thing where people type, and it shows up on the screen. Short for "standard in". This should literally be the word "stdin".

  12. #12
    Registered User
    Join Date
    Feb 2008
    Posts
    84
    yes, but how do i implement that?

  13. #13
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    Once again - have you read the FAQ?
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  14. #14
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    I would use scanf

    Code:
    int main()
    {
        float a;
        float b;
        
        printf("enter a float:\n");
        scanf("%f", &a);
        
        printf("Your float is %.3f", a);
        
        return (EXIT_SUCCESS);    
        
    }

  15. #15
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I would use scanf
    scanf has its own problems, and conveniently enough, your example exhibits one of them. What happens if I type "I'm a little teapot" instead of "123.456"?
    My best code is written with the delete key.

Page 1 of 3 123 LastLast
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, 12:12 PM
  2. basic question to Arrays
    By doneirik in forum C++ Programming
    Replies: 1
    Last Post: 01-25-2005, 01:57 AM
  3. Basic Math Problem. Undefined Math Functions
    By gsoft in forum C Programming
    Replies: 1
    Last Post: 12-28-2004, 02:14 AM
  4. a simple math question
    By chunlee in forum C Programming
    Replies: 3
    Last Post: 11-10-2004, 06:04 AM

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