scan float pointers

E

ehabaziz2001

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));
}
 
M

Mark McIntyre

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.
 
N

Nick Keighley

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)
 
M

Michael Mair

Nick said:
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
 
N

Nick Keighley

Michael said:
ITYM double is preferable to float
yup



ITYM sscanf()

yes

<snip>

my error rate seems a little high today...

from my sig:-
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)

:)
 
O

Old Wolf

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() .
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,481
Members
44,900
Latest member
Nell636132

Latest Threads

Top