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.... :(
#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.... :(