Z
Zach Heath
I'm just starting C and haven't done programming for a few
years...could you guys please take a look at this? Thanks for your
time!
I have an input file that looks like:
1.5 2.5 Bob, Joe
4.5 5.5 Bob, Jolene
The first time through the do while loop it works fine but the second
time it looks like this:
Bob, Joe 38.50 12.75 490.88
Jolene 0.00 0.00 0.00
, Janet 0.00 0.00 0.00
- It cuts off last name and all of the floats show up zeros...
command line: ./a.out < inputFile.c
int main()
{
float inHrs, inRt, myTot;
char inName[16];
do {
/* while ( fgets(inName, sizeof(inName), stdin) ) {
*/ scanf ( "%f%f", &inHrs, &inRt );
myTot = inHrs * inRt;
fgets( inName, sizeof( inName ), stdin);
char * p = strchr( inName,'\n' );
if ( p != NULL ){
*p = '\0';
};
printf ( "%s\t%4.2f %4.2f %4.2f \n", inName, inHrs,
inRt, myTot );
inHrs = 0;
inRt = 0;
} while ( fgets(inName, sizeof(inName), stdin) != NULL );
/*
};
*/
return 0;
}
years...could you guys please take a look at this? Thanks for your
time!
I have an input file that looks like:
1.5 2.5 Bob, Joe
4.5 5.5 Bob, Jolene
The first time through the do while loop it works fine but the second
time it looks like this:
Bob, Joe 38.50 12.75 490.88
Jolene 0.00 0.00 0.00
, Janet 0.00 0.00 0.00
- It cuts off last name and all of the floats show up zeros...
command line: ./a.out < inputFile.c
int main()
{
float inHrs, inRt, myTot;
char inName[16];
do {
/* while ( fgets(inName, sizeof(inName), stdin) ) {
*/ scanf ( "%f%f", &inHrs, &inRt );
myTot = inHrs * inRt;
fgets( inName, sizeof( inName ), stdin);
char * p = strchr( inName,'\n' );
if ( p != NULL ){
*p = '\0';
};
printf ( "%s\t%4.2f %4.2f %4.2f \n", inName, inHrs,
inRt, myTot );
inHrs = 0;
inRt = 0;
} while ( fgets(inName, sizeof(inName), stdin) != NULL );
/*
};
*/
return 0;
}