3 Replies - 2699 Views - Last Post: 14 November 2013 - 04:49 AM Rate Topic: -----

#1 tud16919  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 23
  • Joined: 12-November 13

incompatible types when assigning to type ‘int[4]’ from type ‘int’

Posted 12 November 2013 - 05:22 PM

Hi,

So I am writing a code for my C Programming class. It reads through a text file that contains an album title and then a list of songs, artists and song lengths. Everything was working until I tried to read in the song length in the function read_time. I get the error:
In function ‘read_time’:
line 84: error: incompatible types when assigning to type ‘int[4]’ from type ‘int’

I am not sure how to go about fixing this. I am using a character called scratch to hold the numbers that are read in from the file. Then the function chomp removes the end line from the string. I then want to define time[i] as the value of scratch. Any advice would be much appreciated! Thanks

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define DEBUG 1
#define MAX_TITLE_SIZE  100
#define MAX_SONG_SIZE   80
#define TIME_SIZE	4
#define EOLINE		'\n'
#define EOS 		'\0'
#define DEBUG		1


void read_title		( char *input_file, 	char title[]);
void skip_line		( FILE *IN );
char *chomp		( char s[] );
void read_artist	( char *input_file, 	char artist[33][MAX_SONG_SIZE] );
void read_time		( char *input_file, 	int time[33][TIME_SIZE] );
void read_song  	( char *input_file, 	char song[33][MAX_SONG_SIZE] );
FILE *get_filehandle	( const char *filename, const char *mode );

int main( void )
{

	char title[ MAX_TITLE_SIZE ];
        char song[33][MAX_SONG_SIZE];
	char artist [33][MAX_SONG_SIZE];
	int  time[33][TIME_SIZE];
	char letter;
	char sort_type;
	char a;
		
	read_title("album_info", title);
        if (DEBUG)
	printf( "The title returned to the main function is: %s.\n", title );	

	read_artist  ( "album_info", artist );
	if (DEBUG)
	puts("made it past read_artist");

	read_song("album_info", song);

        if (DEBUG)
        puts("made it past read_song");

	read_time  ( "album_info", time );
        if (DEBUG)
        puts("made it past read_time");



	return EXIT_SUCCESS;	

}

void read_title( char *input_file, char title[] )
{

        FILE *IN = get_filehandle( input_file, "r" );


	fgets( title, MAX_TITLE_SIZE, IN);
	chomp( title );
        if (DEBUG)
	printf("This is the title: %s.\n", title);	
	return;
}

void read_time( char *input_file, int time[33][TIME_SIZE] )
{

        FILE *IN = get_filehandle( input_file, "r" );

	char scratch[20];
	int i;
		skip_line( IN );
		skip_line( IN );
		skip_line( IN );

        for (i=0; i < 33; i++){
		fgets( scratch, TIME_SIZE, IN );
	        chomp(scratch);
		printf("scratch is %s.\n", scratch);
		time[i] = atoi(scratch);
		printf( "Time is %d.\n", time[i] );
		skip_line( IN );
                skip_line( IN );
	}
	return;

}

void read_artist( char *input_file, char artist[33][MAX_SONG_SIZE] )
{

        FILE *IN = get_filehandle( input_file, "r" );

	skip_line( IN );
	if (DEBUG)
	puts("Reached read_artist");

 	int i = 0;
        while (i < 33){
                fgets( artist[i], MAX_SONG_SIZE, IN );
		
		if (DEBUG)
		printf("this is an artist %s", artist[i]);
                chomp( artist[i] );
                if (DEBUG)
		printf("this is an artist after scratch %s\n", artist[i]);	
		skip_line( IN );
                skip_line( IN );
                i++;
        }

        return;
}

void read_song( char *input_file, char song[33][MAX_SONG_SIZE] )
{

        if (DEBUG)
        puts("Reached read_song");

        FILE *IN = get_filehandle( input_file, "r" );

        skip_line( IN );


        int i = 0;
        while (i < 33){
                fgets( song[i], MAX_SONG_SIZE, IN );

                if (DEBUG)
                printf("this is a song %s", song[i]);

                chomp( song[i] );

                if (DEBUG)
                printf("this is a song after chomp %s\n", song[i]);

                skip_line( IN );
                skip_line( IN );
                i++;
        } 

        return;
}

char *chomp( char s[] )
{
	short len = strlen(s);
	
	if ( s[ len - 1 ] == EOLINE )
		s[ len - 1 ] = EOS;

	return s;
}

void skip_line( FILE *IN )
{

	char c;

	while( fgetc( IN ) != EOLINE )
		;//As long as not EOLINE do nothing.
	return;
}

FILE *get_filehandle( const char *filename, const char *mode )
{
        FILE *FH = fopen( filename, mode );
        if ( ! FH )
        {
                printf( "Can't open %s file \"%s\".\n", ( mode[0] == 'r' ) ?
                "input" : "output", filename );
                puts( "** ABORT **" );

                exit( EXIT_FAILURE );
        }

        return FH;
}



Is This A Good Question/Topic? 0
  • +

Replies To: incompatible types when assigning to type ‘int[4]’ from type ‘int’

#2 mojo666  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 353
  • View blog
  • Posts: 774
  • Joined: 27-June 09

Re: incompatible types when assigning to type ‘int[4]’ from type ‘int’

Posted 13 November 2013 - 09:56 AM

int time[33][TIME_SIZE] is an array of 33 arrays of 4 integers each. time[i] = atoi(scratch); You are telling the compiler to store an integer into an array of 4 integers. The compiler does not know how to do this. You would have to tell it where in the array the integer must be stored. Something like time[i][0] = atoi(scratch);. Are you sure you need a multidimensional array?

Also, this should probably be in the c forum.
Was This Post Helpful? 0
  • +
  • -

#3 tud16919  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 23
  • Joined: 12-November 13

Re: incompatible types when assigning to type ‘int[4]’ from type ‘int’

Posted 13 November 2013 - 10:10 AM

Thank you I just needed to make it time[33]. I realized after I poseted the question that I accidentally marked it as a database question but I cannot edit my posts since I am a new member. Sorry about that.


View Postmojo666, on 13 November 2013 - 09:56 AM, said:

int time[33][TIME_SIZE] is an array of 33 arrays of 4 integers each. time[i] = atoi(scratch); You are telling the compiler to store an integer into an array of 4 integers. The compiler does not know how to do this. You would have to tell it where in the array the integer must be stored. Something like time[i][0] = atoi(scratch);. Are you sure you need a multidimensional array?

Also, this should probably be in the c forum.

Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5874
  • View blog
  • Posts: 12,757
  • Joined: 16-October 07

Re: incompatible types when assigning to type ‘int[4]’ from type ‘int’

Posted 14 November 2013 - 04:49 AM

So... the magic number 33 is the max number of songs you can have? And you're using many parallel arrays? Stop that, right now, immediately.

I would prefer to see something like:
typedef struct {
        char song[MAX_SONG_SIZE];
	char artistMAX_SONG_SIZE];
	int  time; /* [TIME_SIZE] you don't need this, WTF? */
} Song;

#define MAX_SONGS 33

typedef struct {
	char title[ MAX_TITLE_SIZE ];
	Song songs[MAX_SONGS];
	int songCount;
} Album;

int read_album(FILE *, Album *);



For your time, sscanf the scratch string and get an integer.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1