Click to See Complete Forum and Search --> : simple scanf question. please help


buttercrunch
10-25-2002, 11:46 AM
hello i'm in a very desperate need for help here.. please help...


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NAMESIZE 30

typedef int time;

typedef struct workerdata Data;

struct workerdata {
char *name;
time start;
time finish;
};


int main ( void )
{
int i = 1,c,d;
int starttime, finishtime;
char name[NAMESIZE];
Data array[100];

scanf ( "%d", &c );
for ( i = 1 ; i < c ; i++ )
{
while ( ( scanf( "%s %d %d", name, &starttime, &finishtime ) == 3 ) )
{
int x = strlen (name);
if ( ( array[i].name = (char *) malloc(sizeof(char) * (x+1))) == NULL )
{
fprintf( stderr, "malloc failed\n" );
return 0;
}
array[i].name = (char *) malloc(sizeof(char) * x+1);
strcpy( array[i].name, name );
array[i].start = starttime;
array[i].finish = finishtime;
i++;
}
}

printf( "worker: %d\n", c );
printf( "result :\n" );

for ( d = 1 ; d <= c ; d++ )
{
printf ( "%s %d %d\n", array[i].name, array[i].start, array[i].finish );
}

return 0;
}


i gcc-ed the code, then issued ./a.out < test
and get this output:

worker: 5
result :
(null) 0 1768448768
(null) 0 1768448768
(null) 0 1768448768
(null) 0 1768448768
(null) 0 1768448768

the file 'test' contains :
5
john 11 10
alice 11 10
mary 12 13
jenny 14 15
alex 15 20



please help, i failed my last project just because of this simple problem.... :(

buttercrunch
10-25-2002, 12:21 PM
bump

mingshun
10-25-2002, 02:55 PM
Originally posted by buttercrunch
bump


Poor programming practises ...
You should initialize your Data array first, preferably with calloc rather than malloc. Try not to abuse char pointers. Use char arrays in your struct workdata to avoid unneccessary careless mistakes.

bigrigdriver
10-29-2002, 07:03 AM
scanf ( "%d", &c );
for ( i = 1 ; i < c ; i++ )
{
while ( ( scanf( "%s %d %d", name, &starttime, &finishtime ) == 3 ) )

I had the same problem recently. It's easily repaired. In the first scanf, you enter an enteger and press <enter>. Correct? Pressing <enter> puts a newline '\n' into the input stream. The integer is written to file, but the <enter> stays in the buffer. The next scanf comes along, sees <enter> and takes of running, bypassing whatever you wanted to do.
The cure is easily done. At the top of the program, add another #define. It should read #define FLUSH while (getchar() != '\n').
Then, just above the while ((scanf statement, inside the curly brace, and this: FLUSH;.
That will clear out the input buffer (especially that pesky <enter> that's hanging around) and the buffer is ready to receive data from the second scanf.

mingshun
10-30-2002, 12:06 PM
Originally posted by bigrigdriver
scanf ( "%d", &c );
for ( i = 1 ; i < c ; i++ )
{
while ( ( scanf( "%s %d %d", name, &starttime, &finishtime ) == 3 ) )


Yours is a different problem I think. Yours arises because your second scanf grabs the the Enter key. You should have use getchar instead.

Whereas for buttercrunch, his arises probably because he didn't initialize his Data array[100] where each array is actually a struct and memory allocation should be done for them.