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

I have posted my code for a CSE project below. When I run the program it is only

ID: 3771123 • Letter: I

Question

I have posted my code for a CSE project below. When I run the program it is only reading the very first line of the data set that it pulls from (Titanic.txt) and I am not sure why. Can someone please help me out with this?

Also, I am not sure what the code that I marked with /*--->*/ is doing... can you explain that as well. Thank you!!! (code is in C)

#include
#include

#define NumOfLines 2201
#define MaxLineLen 65
typedef struct{
   //class = first, second, third, crew
   char class[6];
   //age = adult, child
   char age[5];
   //gender =male, female
   char gender[6];
   //survive = yes, no
   char survive[3];
}titanicP;


void printInfo(titanicP passenger);
titanicP lineToStruct(char line[]);

char* readline(FILE *titanicFile, char line[]);


int main(void) {
       FILE *titanicFile;
   titanicFile = fopen("Titanic.txt", "r");
       if (titanicFile == NULL){
           printf("Unable to open file.");
}

       char lines[NumOfLines][MaxLineLen];


  
    int n = 0;
    while(1) {
        char line[MaxLineLen];
        if (readline(titanicFile, line) != NULL) {
            strcpy(lines[n], line);
            n++;
        }
        else {
            break;
        }
    }

    fclose(titanicFile);

printf("%d lines read from file %s. ", n, titanicFile);
   int j=0;
   titanicP passenger2;
           while(1)
           {
           printf("Please choose an option(1 for query,0 for quit): ");

               float same=0;
               float survive=0;
               int query;
               scanf("%d",&query);

       if(query == 0){
           printf("You choose to quit. Bye! ");
           break;}
       else if(query == 1){
       printf("Now querying Titanic Survival Percentages ");
                        printf("What Class?: ");     
                     scanf("%s",&passenger2.class);

                       printf("What Age?: ");                        
                       scanf("%s",&passenger2.age);

                    printf("What Gender?: ");                     
                    scanf("%s",&passenger2.gender);
           }
              
       int i;
           for(i=0; i               titanicP passenger = lineToStruct(lines[i]);
                  if(compareStruct(passenger2,passenger)==0)
                        {
                            same++;{
                         if(strcmp(passenger.survive, "yes")==0)
                                       survive++;
                     }
               }
               if(same==0)
             {
                       printf("Invalid query ");
             }
               else
            {
                    printf("Query result: ");
                    printf("*%.0f passengers match your query, %.0f survived.",same,survive);   
                    printf("*Chance of survival is %.1f% ",(survive/same*100));               
         }
               survive=0;
         break;
}
}            
return 0;             
}
/*--->*/char* readline(FILE *titanicFile, char line[]) {


  
    if (fgets(line, MaxLineLen, titanicFile) != NULL) {
        int len = strlen(line);
       
        while (line[len - 1] == ' ' || line[len - 1] == ' ') {
            line[len - 1] = '';
            len = strlen(line);
        }
      
        return line;
    }
    else {
    
        return NULL;
           }
/*--->*/}


void printInfo(titanicP passenger) {
    printf("Information about the passenger: ");
    printf(" class: %s ", passenger.class);
    printf(" age: %s ", passenger.age);
    printf(" gender: %s ", passenger.gender);
    printf(" survived: %s ", passenger.survive);
}

int findchar(char str[], int size, char ch) {
    int i;
    for(i = 0; i < size; i++) {
        if(str[i] == ch) {
            return i;
        }
    }
    return -1;
}


titanicP lineToStruct(char line[]){
   titanicP passenger;

//extract class
int classInd = findchar(line, 100, '=');
char class = line[classInd+1];
switch (class){
   case 'f':
       strcpy(passenger.class, "first");
   case 's':
       strcpy(passenger.class, "second");
   case 't':
       strcpy(passenger.class, "third");
   case 'c':
       strcpy(passenger.class, "crew");
   }
   line[classInd] = '!';
int ageInd = findchar(line, 100, '=');
char age = line[ageInd+1];
   switch (age){
   case 'a':
       strcpy(passenger.age, "adult");
   case 'c':
       strcpy(passenger.age, "child");
   }
   line[ageInd] = '!';
int genderInd = findchar(line, 100, '=');
char gender = line[genderInd+1];
   switch (gender){
   case 'm':
       strcpy(passenger.gender, "male");
   case 'f':
       strcpy(passenger.gender, "female");
   }
   line[genderInd] = '!';
int surviveInd = findchar(line, 100, '=');
char survive = line[surviveInd+1];
   switch (survive){
   case 'y':
       strcpy(passenger.survive, "yes");
   case 'n':
       strcpy(passenger.survive, "no");
   }
   return passenger;

}

int compareStruct(titanicP passenger1, titanicP passenger2)
{


if(strcmp(passenger1.class, passenger2.class)==0||strcmp(passenger1.class,"any")==0)               
{
        if(strcmp(passenger1.age,passenger2.age)==0||strcmp(passenger1.age,"any")==0)
        {
               if(strcmp(passenger1.gender,passenger2.gender)==0||strcmp(passenger1.gender,"any")==0)
               {
                       return 0;
               }     
                else
            return 1;
       }     
        else
    return 1;


}
else
return 1;
}

Explanation / Answer

#include <stdio.h>

#include <string.h>

#define NUM_OF_LINES 2201

#define MAX_LINE_LEN 99

typedef struct {

char class[10]; // class => first, second , third , crew

char age[10]; // age => child , adult

char gender[10]; // gender => male , female

char survived[10];

} Passenger;

void printInfo(Passenger p) {

printf("class: %s ", p.class);

printf("age: %s ", p.age);

printf("gender: %s ", p.gender);

printf("survived: %s ", p.survived);

}

int findchar(char *str, int size, char ch) {

char *p;

int i;

for (i = 0; i<size; i++) {

if (str[i] == ch) {

return i;

}

}

return -1;

}

Passenger lineToStruct(char line[])

{

//class=first,age=adult,gender=male,survived=yes

Passenger passenger;

//Extracting the class

int classInd = findchar(line, 50, '=');

char class = line[classInd + 1];

switch (class)

{

case 'f':

strcpy(passenger.class, "first");

break;

case 's':

strcpy(passenger.class, "second");

break;

case 't':

strcpy(passenger.class, "third");

break;

case 'c':

strcpy(passenger.class, "crew");

break;

}

line[classInd] = '!';

//Extracting Age

int ageInd = findchar(line, 48, '=');

char age = line[ageInd + 1];

switch (age)

{

case 'a':

strcpy(passenger.age, "adult");

break;

case 'c':

strcpy(passenger.age, "child");

break;

}

line[ageInd] = '!';

//Extracting Gender

int genderInd = findchar(line, 48, '=');

char gender = line[genderInd + 1];

switch (gender)

{

case 'm':

strcpy(passenger.gender, "male");

break;

case 'f':

strcpy(passenger.gender,"female");

break;

}

line[genderInd] = '!';

return passenger;

}

//This function will convert the inputted query to a struct

Query lineToStruct(char line[])

{

Query query;

//Extracting the class of the query

int classInd = findchar(line, 50, '=');

char class = line[classInd + 1];

switch (class)

{

case 'f':

strcpy(query.class, "first");

break;

case 's':

strcpy(query.class, "second");

break;

case 't':

strcpy(query.class, "third");

break;

case 'c':

strcpy(query.class, "crew");

break;

}

line[classInd] = '!';

//Extracting Age of the query

int ageInd = findchar(line, 48, '=');

char age = line[ageInd + 1];

switch (age)

{

case 'a':

strcpy(query.age, "adult");

break;

case 'c':

strcpy(query.age, "child");

break;

}

line[ageInd] = '!';

//Extracting Gender of the query

int genderInd = findchar(line, 48, '=');

char gender = line[genderInd + 1];

switch (gender)

{

case 'm':

strcpy(query.gender, "male");

break;

case 'f':

strcpy(query.gender,"female");

break;

}

line[genderInd] = '!';

}

int cmpStruct(Passenger passenger, Query query)

{

if(strcmp(query.class, "any") !=0 && strcmp(query.class, passenger.class) !=0)

return 1;

if(strcmp(query.age, "any") !=0 && strcmp(query.age, passenger.age) !=0)

return 1;

if(strcmp(query.gender, "any") !=0 && strcmp(query.gender, passenger.gender) !=0)

return 1;

return 0;

}

int main(void) {

//char line[] = "class=first,age=adult,gender=male,survived=yes";

//Passenger p = lineToStruct(line);

FILE *fp; // A file pointer

char fileName[] = "Titanic.txt";

fp = fopen(fileName, "r"); // Open the file for reading

if (fp == NULL) {

// Fail to open the file, quit the program

printf("Error opening file %s. ", fileName);

return -1;

}

Passenger allPassengers[99];

int index;

int n = 0;

while(1) {

char line[MAX_LINE_LEN]; // reading a line

if (fgets(line, 99, fp ) == NULL) {

break;

}

Passenger tempPassenger = lineToStruct(line);

for(index=0; index<n;index++)

{

allPassengers[index] = tempPassenger;

}

n++;

// now I have all the passengers in my array

}

printf("%d lines read from file %s. ", n, fileName);

//

char lines[1000];

int q, same, survived;

int passsame = 0, passsurvived =0;

float per;

Passenger pa1, a[NUM_OF_LINES];

while (1) {

//Interactions with the user/

printf("Please choose an option (1 for query, 0 for quit): ");

scanf("%d", &q);

if (q == 0) {

printf("bye! ");

return -1;

}

if (q == 1) {

int tally1;

for (tally1 = 0; tally1<NUM_OF_LINES;tally1++) {

a[tally1] = lineToStruct(&lines[tally1]);

}

printf("You chose to query the Titanic data. ");

printf("Which cabin class (first, second, third, crew, any)?: ");

scanf("%s", pa1.class);

printf("What age (adult, child, any)?: ");

scanf("%s", pa1.age);

printf("What gender (male, female, any)?: ");

scanf("%s", pa1.gender);

//determine survivors

for (tally1 = 0; tally1 < NUM_OF_LINES; tally1++) {

same = cmpStruct(pa1, a[tally1]);

if (same == 1) {

passsame = passsame +1;

//survived = strcmp(a[tally1].survived, 'yes');

if (strcmp(a[tally1].survived, "yes")==0); {

passsurvived = passsurvived + 1;

}

}

}

if (passsame == 0) {

printf("Query not valid ");

}

else {

printf("Query result: ");

printf("%d passengers match your query, %d survived. ", passsame, passsurvived);

per = (float)((passsurvived / passsame) * 100);

printf("Chance of survival is : %.1f% ", per);

}

}

}

}