Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I got this solution from chegg a few hours ago. the only problem here what I am

ID: 3850525 • Letter: I

Question

I got this solution from chegg a few hours ago.
the only problem here what I am facing is with the line

char *buf = malloc(MAX_STR_LEN);

when I try to run it on my Dev++, it says 17 [Error] invalid conversion from 'void*' to 'char*' [-fpermissive].
So I am not sure what to do here.
Moreover, it the program should also show the average of three ratings. not all the ratings as output.
and can we also just use strtok to get rid off "," instead off atoi? I am not sure about it.
So can you please fix these issues?
Thank you.
I am copying the solution I got the last time.

Thank you

So here is the solution I got:

my csv file looks like this


#include<stdio.h>
#define MAX_STR_LEN 256
typedef struct
{
    char *name;
    char *coding;
    int minutes;
    int rating[3];
}movie;
int main()
{
        FILE * inp;
        movie m;
       inp = fopen("movies.csv", "r");
       char *buf = malloc(MAX_STR_LEN);
        char *tmp;

   if(!inp)
   {
       printf("Unable to open file ");
       return 1;
   }
   if (buf == NULL) {
        printf ("No memory ");
        return 1;
    }

    while (fgets(buf, 255, inp) != NULL)
   {
       if ((strlen(buf)>0) && (buf[strlen (buf) - 1] == ' '))
            buf[strlen (buf) - 1] = '';

          tmp = strtok(buf, ",");
          m.name = tmp;

           tmp = strtok(NULL, ",");
           m.coding = tmp;

            tmp = strtok(NULL, ",");
           m.minutes = atoi(tmp);

            tmp = strtok(NULL, ",");
           m.rating[0] = atoi(tmp);

            tmp = strtok(NULL, ",");
           m.rating[1] = atoi(tmp);

            tmp = strtok(NULL, ",");
           m.rating[2] = atoi(tmp);

           printf("%10s %4s %3d %d %d %d ",m.name,m.coding,m.minutes,m.rating[0],m.rating[1],m.rating[2]);

   }
   fclose(inp);
    return 0;
}

Date Name Movie Structure File Write a program that will read in a csv file with information regarding movies. The file will contain a movie name, MPAA coding PG, PG of and from the three top critics. Your program will read in the data into an array structures the send the array to a function that will professionally display all of the formation, including average rating of the critics. Show me this program working!

Explanation / Answer

The program given by you does not fill in an array of struct. It just uses a single struct and prints it . For your assignment, the requirement is to use an array and then print the array. So I have written a new code for the same. Also strtok() is used to break the input string into tokens. atoi() does the conversion from string to int. So both are needed.

Here is the C program and output for the question. Please don't forget to rate the answer if it helped. If any doubts, please post a comment and I shall respond. Thank you very much.

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define MAX_STR_LEN 256

typedef struct

{

char name[50];

char coding[6];

int minutes;

int rating[3];

  

}movie;

int read_file(FILE *infile, movie movies[]);

void display(movie movies[], int count);

int main()

{

FILE * infile;

movie movies[100]; /*max 100 movies in the array*/

int count ;

  

infile = fopen("movies.csv", "r");

  

  

if(!infile)

{

printf("Unable to open file ");

return 1;

}

count = read_file(infile, movies);

fclose(infile);

  

display(movies, count);

  

printf(" Done. ");

return 0;

}

int read_file(FILE *infile, movie movies[])

{

char buffer[MAX_STR_LEN];

char *token;

char separator[2] = ",";

int count = 0;

int i;

while(fgets(buffer, MAX_STR_LEN - 1, infile))

{

//remove the from the end

if ((strlen(buffer)>0) && (buffer[strlen (buffer) - 1] == ' '))

buffer[strlen (buffer) - 1] = '';

token = strtok(buffer, separator);

strcpy(movies[count].name, token);//copy the token to movie name

  

//extract next token i.e. coding

token = strtok(NULL, separator);

strcpy(movies[count].coding, token);

  

//extract next token i.e. movie length

token = strtok(NULL, separator);

movies[count].minutes = atoi (token);

  

//extract the 3 ratings

for(i = 0; i < 3; i++)

{

token = strtok(NULL, separator);

movies[count].rating[i] = atoi (token);

}

  

count++;

  

}

  

return count;

  

}

void display(movie movies[], int count)

{

int i;

double avg;

printf(" %-40s %-15s %15s %10s %10s %10s %20s", "Movie Name", "Coding", "Length(min)","Rating 1",

   "Rating 2","Rating 3", "Average Rating");

printf(" %-40s %-15s %15s %10s %10s %10s %20s", "==========", "======", "===========","========",

   "========","========", "============== ");

for( i = 0; i < count; i++)

{

avg = (movies[i].rating[0] + movies[i].rating[1] + movies[i].rating[2] ) / 3.0;

   printf(" %-40s %-15s %15d %10d %10d %10d %20.2f", movies[i].name , movies[i].coding, movies[i].minutes,

movies[i].rating[0], movies[i].rating[1], movies[i].rating[2], avg);

}

}

input file movies.csv

movie1,G,120,5,5,6
movie2,PG,120,9,9,8
movie3,PG-13,120,7,9,6
movie4,R,120,8,9,8

output

Movie Name Coding Length(min) Rating 1 Rating 2 Rating 3 Average Rating
========== ====== =========== ======== ======== ======== ==============

movie1 G 120 5 5 6 5.33
movie2 PG 120 9 9 8 8.67
movie3 PG-13 120 7 9 6 7.33
movie4 R 120 8 9 8 8.33