2 Replies - 493 Views - Last Post: 12 November 2013 - 07:07 PM Rate Topic: -----

#1 tud16919  Icon User is offline

  • New D.I.C Head

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

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

Posted 12 November 2013 - 05:35 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

001 #include <stdio.h>
002 #include <stdlib.h>
003 #include <string.h>
004
005 #define DEBUG 1
006 #define MAX_TITLE_SIZE 100
007 #define MAX_SONG_SIZE 80
008 #define TIME_SIZE 4
009 #define EOLINE '\n'
010 #define EOS '\0'
011 #define DEBUG 1
012
013
014 void read_title ( char *input_file, char title[]);
015 void skip_line ( FILE *IN );
016 char *chomp ( char s[] );
017 void read_artist ( char *input_file, char artist[33][MAX_SONG_SIZE] );
018 void read_time ( char *input_file, int time[33][TIME_SIZE] );
019 void read_song ( char *input_file, char song[33][MAX_SONG_SIZE] );
020 FILE *get_filehandle ( const char *filename, const char *mode );
021
022 int main( void )
023 {
024
025 char title[ MAX_TITLE_SIZE ];
026 char song[33][MAX_SONG_SIZE];
027 char artist [33][MAX_SONG_SIZE];
028 int time[33][TIME_SIZE];
029 char letter;
030 char sort_type;
031 char a;
032
033 read_title("album_info", title);
034 if (DEBUG)
035 printf( "The title returned to the main function is: %s.\n", title );
036
037 read_artist ( "album_info", artist );
038 if (DEBUG)
039 puts("made it past read_artist");
040
041 read_song("album_info", song);
042
043 if (DEBUG)
044 puts("made it past read_song");
045
046 read_time ( "album_info", time );
047 if (DEBUG)
048 puts("made it past read_time");
049
050
051
052 return EXIT_SUCCESS;
053
054 }
055
056 void read_title( char *input_file, char title[] )
057 {
058
059 FILE *IN = get_filehandle( input_file, "r" );
060
061
062 fgets( title, MAX_TITLE_SIZE, IN);
063 chomp( title );
064 if (DEBUG)
065 printf("This is the title: %s.\n", title);
066 return;
067 }
068
069 void read_time( char *input_file, int time[33][TIME_SIZE] )
070 {
071
072 FILE *IN = get_filehandle( input_file, "r" );
073
074 char scratch[20];
075 int i;
076 skip_line( IN );
077 skip_line( IN );
078 skip_line( IN );
079
080 for (i=0; i < 33; i++){
081 fgets( scratch, TIME_SIZE, IN );
082 chomp(scratch);
083 printf("scratch is %s.\n", scratch);
084 time[i] = atoi(scratch);
085 printf( "Time is %d.\n", time[i] );
086 skip_line( IN );
087 skip_line( IN );
088 }
089 return;
090
091 }
092
093 void read_artist( char *input_file, char artist[33][MAX_SONG_SIZE] )
094 {
095
096 FILE *IN = get_filehandle( input_file, "r" );
097
098 skip_line( IN );
099 if (DEBUG)
100 puts("Reached read_artist");
101
102 int i = 0;
103 while (i < 33){
104 fgets( artist[i], MAX_SONG_SIZE, IN );
105
106 if (DEBUG)
107 printf("this is an artist %s", artist[i]);
108 chomp( artist[i] );
109 if (DEBUG)
110 printf("this is an artist after scratch %s\n", artist[i]);
111 skip_line( IN );
112 skip_line( IN );
113 i++;
114 }
115
116 return;
117 }
118
119 void read_song( char *input_file, char song[33][MAX_SONG_SIZE] )
120 {
121
122 if (DEBUG)
123 puts("Reached read_song");
124
125 FILE *IN = get_filehandle( input_file, "r" );
126
127 skip_line( IN );
128
129
130 int i = 0;
131 while (i < 33){
132 fgets( song[i], MAX_SONG_SIZE, IN );
133
134 if (DEBUG)
135 printf("this is a song %s", song[i]);
136
137 chomp( song[i] );
138
139 if (DEBUG)
140 printf("this is a song after chomp %s\n", song[i]);
141
142 skip_line( IN );
143 skip_line( IN );
144 i++;
145 }
146
147 return;
148 }
149
150 char *chomp( char s[] )
151 {
152 short len = strlen(s);
153
154 if ( s[ len - 1 ] == EOLINE )
155 s[ len - 1 ] = EOS;
156
157 return s;
158 }
159
160 void skip_line( FILE *IN )
161 {
162
163 char c;
164
165 while( fgetc( IN ) != EOLINE )
166 ;//As long as not EOLINE do nothing.
167 return;
168 }
169
170 FILE *get_filehandle( const char *filename, const char *mode )
171 {
172 FILE *FH = fopen( filename, mode );
173 if ( ! FH )
174 {
175 printf( "Can't open %s file \"%s\".\n", ( mode[0] == 'r' ) ?
176 "input" : "output", filename );
177 puts( "** ABORT **" );
178
179 exit( EXIT_FAILURE );
180 }
181
182 return FH;
183 }

Is This A Good Question/Topic? 0
  • +

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

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1329
  • View blog
  • Posts: 4,555
  • Joined: 19-February 09

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

Posted 12 November 2013 - 07:00 PM

Hi, you are reading 4 bytes/characters.

081 fgets( scratch, TIME_SIZE, IN );



then converting to an integer.

The integers don't need a size.

028 int time[33][TIME_SIZE];



You just need integers.

028 int time[33];


Was This Post Helpful? 1
  • +
  • -

#3 tud16919  Icon User is offline

  • New D.I.C Head

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

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

Posted 12 November 2013 - 07:07 PM

Thank you so much!!!!!!!!!
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1