floating point numbers display something wrong i think

This is a discussion on floating point numbers display something wrong i think within the C Programming forums, part of the General Programming Boards category; when i enter floading point numbers then go to function to check there is no character then go to showdata ...

  1. #1
    Unregistered
    Guest

    floating point numbers display something wrong i think

    when i enter floading point numbers then go to function to check there is no character then go to showdata function to show the the number it doesn't show 0.0000 if i put showdata function in the main it shows the right value
    could u help me what is wrong with it
    cheers!
    #include <stdio.h>

    typedef struct tagStock
    {
    char wholesale[100];
    float wholesaleprice;

    }Stock;

    int i;
    char ex;
    //*************prototype***************
    void ShowData();
    float WholesaleP(char wholeP[],Stock *stk);
    //******Main**********
    void main()
    {
    struct Stock sta;

    bool k;
    int a;
    WholesaleP(sta.wholesale,&sta);

    ShowData();
    }
    //************function****************
    float WholesaleP(char wholeP[],Stock *stk)
    {

    do
    {
    printf("Enter wholesale price : ");
    scanf("%s",wholeP);
    for(i = 0; wholeP[i] && isdigit(wholeP[i]) || wholeP[i]=='.'; i++) ;

    } while(wholeP[i]);
    stk->wholesaleprice= atof(wholeP);
    return 1;
    }

    //=======Show Data function=================
    void ShowData()
    {
    struct tagStock sta;
    printf("Wholesale price : ");
    printf("%f\n",sta.wholesaleprice);
    }

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    1. It should be int main and main should return a value.
    2. What's the purpose of the while loop in WholesaleP?
    3. You haven't initialised the struct in ShowData.

    You should pass the struct which is created in main to the function ShowData. And ShowData should then print the contents of the pased struct.

  3. #3
    Unregistered
    Guest
    the purpose of the loop is
    to check the floating number is all number not character
    i am quite new with C
    thanks

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    In that case I would use a while loop instead of a for loop. Perhaps something like this works (I haven't tested it):

    Code:
    StrLength = strlen (wholeP);
    
    NonDigitFound = FALSE;
    Counter = 0;
    
    while (NotDigitFound == FALSE && Counter < StrLength)
    {
        if (!isdigit (wholeP [Counter]))
            NotDigitFound = TRUE;
    }
    
    if (NotDigitFound == FALSE)
        stk->wholesaleprice= atof(wholeP);
    In C a variable declared in a function is local. That means that the variable is known in the function in which it is declared and it is not known by the other functions.

    So the variable sta you declared in main() is not known by ShowData(). If you want ShowData to print the contents of sta, you need to pass that variable to the function.

    Function main() should be of type int and return a return-value, this value tells the environment how the program finished. Usually 0 is OK and 1 is ERROR. The environment can be a programm which calls your program or the OS.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing Floating Point Return Values
    By jason_m in forum C Programming
    Replies: 5
    Last Post: 08-15-2008, 02:37 PM
  2. Problem with floating point numbers
    By thetinman in forum C++ Programming
    Replies: 3
    Last Post: 11-13-2005, 06:55 PM
  3. checking for floating point number
    By ssharish2005 in forum C Programming
    Replies: 6
    Last Post: 10-18-2005, 09:14 PM
  4. Floating point #'s, why so much talk about it?
    By scuzzo84 in forum C Programming
    Replies: 5
    Last Post: 09-20-2005, 05:29 PM
  5. fixed point / floating point
    By confuted in forum Game Programming
    Replies: 4
    Last Post: 08-13-2002, 02:25 PM

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