scan float pointers

Discussion in 'C Programming' started by ehabaziz2001@gmail.com, Dec 10, 2005.

  1. Guest

    Hi,
    I runned that program with int variables and was OK but when I changed
    the type of
    pay_calc(),*emp_rate,pay
    from integer to float I failed ..Please Help

    main()
    {
    int *emp_no,*emp_works;
    int pay_calc(),*emp_rate,pay;
    char *emp_unit,ans,another();
    system("cls");
    do {
    input(&emp_no,&emp_works,&emp_rate,&emp_unit);
    pay=pay_calc(emp_works,emp_rate,emp_unit);
    pay_slip(emp_no,emp_works,emp_rate,emp_unit);
    cash(pay);
    } while (another()=='y');
    return 0;
    }


    char another()
    {
    char answer;
    printf("\nAnother Employee (y/n) : ");
    scanf("\n");
    scanf("%c",&answer);
    return (answer);
    }


    input (emp_n,emp_w,emp_r,emp_u)
    int *emp_n,*emp_w;
    int *emp_r;
    char *emp_u;
    {
    printf ("\n Enter the Employee number : ");
    scanf("%d",emp_n);
    printf ("\n Enter the Employee Pay Based unit (weeks or hours) : ");
    scanf("\n");
    scanf("%c",emp_u);
    if ((*emp_u)=='w')
    {
    printf ("\n Enter the number of weeks :");
    scanf("%d",emp_w);
    printf ("\n Enter the rate of paying for each week :");
    scanf("%d",emp_r);
    }
    if ((*emp_u)=='h')
    {
    printf ("\n Enter the number of hours :");
    scanf("%d",emp_w);
    printf ("\n Enter the rate of paying for each hour :");
    scanf("%d",emp_r);
    }
    return 0;
    }

    int pay_calc(emp_w,emp_r,emp_u)
    int emp_w;
    int emp_r;
    char emp_u;
    {
    int p=0.0;
    p=(emp_r)*(emp_w);
    if (emp_u=='h')
    printf ("\nThat pay for hours %d worked with rate %d is =
    %d",emp_w,emp_r,p);
    if (emp_u=='w')
    printf ("\nThat pay for Weeks %d worked with rate %d is =
    %d",emp_w,emp_r,p);
    return ((emp_r)*(emp_w));
    }
     
    , Dec 10, 2005
    #1
    1. Advertising

  2. On 10 Dec 2005 03:16:50 -0800, in comp.lang.c ,
    wrote:

    >Hi,
    >I runned that program with int variables and was OK but when I changed
    >the type of
    >pay_calc(),*emp_rate,pay
    > from integer to float I failed ..Please Help
    >
    >main()
    >{
    >int *emp_no,*emp_works;
    >int pay_calc(),*emp_rate,pay;


    Don't do this. Function declarations should be outside main. Otherwise
    their declarations are invisible to other routines.

    >printf ("\n Enter the Employee number : ");

    ?scanf("%d",emp_n);

    Do not use scanf to read user input. Please read the FAQ for reasons
    why, and what to use instead.

    >int pay_calc(emp_w,emp_r,emp_u)
    >int emp_w;
    >int emp_r;
    >char emp_u;


    This style of function definition has been deprecated since 1989, that
    means you should not use it. Please buy a book on C that dates after
    1990 ! This is how you should do it:

    int pay_calc(int emp_w, int emp_r, char emp_u)
    >{
    >int p=0.0;
    >p=(emp_r)*(emp_w);


    you do not need the parens here.

    >if (emp_u=='h')
    >printf ("\nThat pay for hours %d worked with rate %d is =
    >%d",emp_w,emp_r,p);
    >if (emp_u=='w')
    >printf ("\nThat pay for Weeks %d worked with rate %d is =
    >%d",emp_w,emp_r,p);
    >return ((emp_r)*(emp_w));


    or here.

    ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
     
    Mark McIntyre, Dec 10, 2005
    #2
    1. Advertising

  3. wrote:

    > I runned that program with int variables and was OK but when I changed
    > the type of
    > pay_calc(),*emp_rate,pay
    > from integer to float I failed ..Please Help


    what does "failed" mean? Does it compile? Does it give unexpected
    answers?

    1. double is preferable to int
    2. please use a sensible indentation style. I've reformatted one of
    your functions
    to be more sensible. My remarks are indicated with /***
    3. use "int main (void)" not "main ()"

    input (emp_n,emp_w,emp_r,emp_u)
    int *emp_n,*emp_w;
    int *emp_r;
    char *emp_u;

    /***
    this form of function definition has been obselete since 1989
    use:
    input (int *emp_n, int *emp_w, int emp_r, char *emp_u)
    ***/

    {
    printf ("\n Enter the Employee number : ");

    /***
    use '/n' at the end of use fflush()
    ***/

    scanf("%d",emp_n);

    /***
    scanf() is tricky to use correctly. Always check for errors and switch
    to
    fgets() and fscanf()
    ***/

    printf ("\n Enter the Employee Pay Based unit (weeks or hours) :
    ");
    scanf("\n");
    scanf("%c",emp_u);

    if ((*emp_u)=='w')
    {
    printf ("\n Enter the number of weeks :");
    scanf("%d",emp_w);
    printf ("\n Enter the rate of paying for each week :");
    scanf("%d",emp_r);
    }

    if ((*emp_u)=='h')
    {
    printf ("\n Enter the number of hours :");
    scanf("%d",emp_w);
    printf ("\n Enter the rate of paying for each hour :");
    scanf("%d",emp_r);
    }

    return 0;
    }

    --
    Nick Keighley

    We recommend, rather, that users take advantage of the extensions of
    GNU C and disregard the limitations of other compilers. Aside from
    certain supercomputers and obsolete small machines, there is less
    and less reason ever to use any other C compiler other than for
    bootstrapping GNU CC.
    (Using and Porting GNU CC)
     
    Nick Keighley, Dec 10, 2005
    #3
  4. Michael Mair Guest

    Nick Keighley wrote:
    > wrote:
    >
    >>I runned that program with int variables and was OK but when I changed
    >>the type of
    >>pay_calc(),*emp_rate,pay
    >> from integer to float I failed ..Please Help

    >
    > what does "failed" mean? Does it compile? Does it give unexpected
    > answers?
    >
    > 1. double is preferable to int

    ITYM double is preferable to float

    > 2. please use a sensible indentation style. I've reformatted one of
    > your functions
    > to be more sensible. My remarks are indicated with /***
    > 3. use "int main (void)" not "main ()"
    >
    > input (emp_n,emp_w,emp_r,emp_u)
    > int *emp_n,*emp_w;
    > int *emp_r;
    > char *emp_u;
    >
    > /***
    > this form of function definition has been obselete since 1989
    > use:
    > input (int *emp_n, int *emp_w, int emp_r, char *emp_u)
    > ***/
    >
    > {
    > printf ("\n Enter the Employee number : ");
    >
    > /***
    > use '/n' at the end of use fflush()


    '\n'

    > ***/
    >
    > scanf("%d",emp_n);
    >
    > /***
    > scanf() is tricky to use correctly. Always check for errors and switch
    > to
    > fgets() and fscanf()

    ITYM sscanf()

    Note that all functions of the scanf() family return an int
    value which denotes the number of successfully read items:
    if (1 != sscanf(mybuffer, "%d", emp_n)
    { /* error handling for failed conversion */ }

    > ***/
    >
    > printf ("\n Enter the Employee Pay Based unit (weeks or hours) :
    > ");
    > scanf("\n");
    > scanf("%c",emp_u);
    >
    > if ((*emp_u)=='w')
    > {
    > printf ("\n Enter the number of weeks :");
    > scanf("%d",emp_w);
    > printf ("\n Enter the rate of paying for each week :");
    > scanf("%d",emp_r);
    > }
    >
    > if ((*emp_u)=='h')
    > {
    > printf ("\n Enter the number of hours :");
    > scanf("%d",emp_w);
    > printf ("\n Enter the rate of paying for each hour :");
    > scanf("%d",emp_r);
    > }
    >
    > return 0;
    > }
    >
    > --
    > Nick Keighley
    >
    > We recommend, rather, that users take advantage of the extensions of
    > GNU C and disregard the limitations of other compilers. Aside from
    > certain supercomputers and obsolete small machines, there is less
    > and less reason ever to use any other C compiler other than for
    > bootstrapping GNU CC.
    > (Using and Porting GNU CC)


    Hmmm, is that advertising? In the context of clc, this looks
    like "embrace and extend" to me...

    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
     
    Michael Mair, Dec 10, 2005
    #4
  5. Michael Mair wrote:
    > Nick Keighley wrote:
    > > wrote:


    > >>I runned that program with int variables and was OK but when I changed
    > >>the type of
    > >>pay_calc(),*emp_rate,pay
    > >> from integer to float I failed ..Please Help


    <snip>

    > > 1. double is preferable to int

    >
    > ITYM double is preferable to float


    yup

    <snip>

    > > scanf("%d",emp_n);
    > >
    > > /***
    > > scanf() is tricky to use correctly. Always check for errors and switch
    > > to
    > > fgets() and fscanf()

    >
    > ITYM sscanf()


    yes

    <snip>

    my error rate seems a little high today...

    from my sig:-

    > > We recommend, rather, that users take advantage of the extensions of
    > > GNU C and disregard the limitations of other compilers. Aside from
    > > certain supercomputers and obsolete small machines, there is less
    > > and less reason ever to use any other C compiler other than for
    > > bootstrapping GNU CC.
    > > (Using and Porting GNU CC)

    >
    > Hmmm, is that advertising? In the context of clc, this looks
    > like "embrace and extend" to me...


    I didn't post it because I agreed with it. I've never been sure if it
    wasn't a bit
    toungue in cheek. In no way was it intended to be an advertisment.


    --
    Nick Keighley

    -pedantic
    This option is not intended to be useful; it exists only to satisfy
    pedants who would otherwise claim that GNU CC fails to support the
    ANSI standard.
    (Using and Porting GNU CC)

    :)
     
    Nick Keighley, Dec 10, 2005
    #5
  6. Old Wolf Guest

    wrote:

    > main()
    > {
    > int *emp_no,*emp_works;
    > input(&emp_no,&emp_works,&emp_rate,&emp_unit);
    >

    [snip]
    >
    > input (emp_n,emp_w,emp_r,emp_u)
    > int *emp_n,*emp_w;
    > int *emp_r;
    > char *emp_u;


    You are passing an (int **) to a function that expects an (int *).
    This is only the first error I spotted, there could be many more
    such errors.

    Please use function prototypes, then your compiler will
    detect these errors for you.

    Example:

    int input( int *emp_n, int *emp_w, int *emp_r, char *emp_u );

    int main() {
    input(&n, &w, &r, &u);
    }

    int input( int *emp_n, int *emp_w, int *emp_r, char *emp_u )
    {
    // stuff...
    }

    If you don't like the thought of writing out the function header twice,
    then put the whole function body before main() .
     
    Old Wolf, Dec 11, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy
    Replies:
    7
    Views:
    6,326
    Roedy Green
    May 10, 2004
  2. Jim West
    Replies:
    4
    Views:
    1,507
    Jeff Schwab
    Jan 16, 2004
  3. bd
    Replies:
    0
    Views:
    663
  4. Carsten Fuchs
    Replies:
    45
    Views:
    1,648
    James Kanze
    Oct 8, 2009
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    729
Loading...

Share This Page