I F YOU CAN FIX THIS I WILL GIVE YOU ALL MY CHEGG POINTS. I just need this to co
ID: 657371 • Letter: I
Question
IF YOU CAN FIX THIS I WILL GIVE YOU ALL MY CHEGG POINTS.
I just need this to compile and run
http://www.cse.msu.edu/~cse220/proj/Project3%20%282%29.pdf background info
you need to test it with this input: http://www.cse.msu.edu/~cse220/proj/Titanic.txt
/************************************************************************************************************
C program that reads a Titanic text file and prompts user to enter the class type, age and gender
and prints the matched count of query and survived in the matched and prints the percentage
of survival of matched .
*************************************************************************************************************/
#include <stdio.h>
#include <conio.h> // this is where the problem is!
#include <ncurses.h>
#include <string.h>
#include <stdlib.h>
/* Macro definitions */
#define NUM_OF_LINES 2202
#define MAX_LINE_LEN 99
//structure declaration
typedef struct{
char Class[7];
char age[6];
char gender[7];
char survived[4];
}Passenger;
/* Function declarations */
int findchar(char str[], int size, int start, char ch) ;
void substrcpy(char str1[], int start, int end, char str2[]) ;
char* readline(FILE *fp, char line[]) ;
int cmpStruct(Passenger p, Passenger query);
int main(void)
{
FILE *fp; // A file pointer
char fileName[] = "Titanic.txt";
fp = fopen(fileName, "r"); // Open the file for reading
char Class[7];
char age[6];
char gender[7];
char survived[4];
int index=0;
Passenger p[NUM_OF_LINES];
if (fp == NULL)
{
// Fail to open the file, quit the program
printf("Error opening file %s. ", fileName);
getch();
return -1;
}
int i1, i2;
char line[MAX_LINE_LEN];
// each line is stored in one row of the array
char lines[NUM_OF_LINES][MAX_LINE_LEN];
//read structure file data
while(!feof(fp) && index {
if(readline(fp,line)!=NULL)
strcpy(lines[index],line);
else
break;
int len = strlen(lines[index]);
i1 = findchar(lines[index], len, 0, '=');
i2 = findchar(lines[index], len, i1, ',');
substrcpy(lines[index], i1+1, i2-1, p[index].Class);
i1 = findchar(lines[index], len, i2, '=');
i2 = findchar(lines[index], len, i1, ',');
substrcpy(lines[index], i1+1, i2-1, p[index].age);
i1 = findchar(lines[index], len, i2, '=');
i2 = findchar(lines[index], len, i1, ',');
substrcpy(lines[index], i1+1, i2-1,p[index].gender);
i1 = findchar(lines[index], len, i2, '=');
substrcpy(lines[index], i1+1, len-1, p[index].survived);
index++;
}
int userChoice;
do
{
printf("Please choose an option(1 for query, 0 for quit) : ");
scanf("%d",&userChoice);
//checking if user choice is 1
if(userChoice==1)
{
printf("You choose to query the Titanic data. ");
printf("Which cabin class(first, second, third,crew) :");
//read class type
scanf("%6s",Class);
printf("What age(adult, child) :");
//read age
scanf("%5s",age);
printf("What gender(male,female) :");
scanf("%6s",gender);
//read gender
Passenger query;
strcpy(query.Class,Class);
strcpy(query.age,age);
strcpy(query.gender,gender);
int i=0;
int matchedCount=0;
int survivedCount=0;
while(i {
//calling cmpStruct that checks matching of given query with structure at index i
if(cmpStruct(p[i], query)==0)
{
matchedCount++;
if(strcmp(p[i].survived,"yes")==0)
survivedCount++;
}
i++;
}
printf("Query result : ");
printf("%d passengers match your query, %d survived. ",matchedCount,survivedCount);
if(matchedCount>0)
printf("Chance of survival is %.1f %% ",((double)survivedCount/matchedCount)*100.0);
else
printf("No match found ");
}
//exit the program if the user choice is 0
else if(userChoice==0)
{
printf("You chose to quit. Bye!");
getch();
//call exit(0) method to close the program
exit(0);
}
if(userChoice<0 || userChoice>1)
printf("Invalid choice");
}while(userChoice<0 || userChoice>1);
fclose(fp); // Close the file after reading
getch();
return 0;
}
//The method cmpStruct that accpets teh passenger and query and returns 0 if the matched ortherwiser returens 1
int cmpStruct(Passenger p, Passenger query)
{
int match=1;
//check if Class type is any and age type any
if(strcmp(query.Class,"any")==0 && strcmp(query.age,"any")==0)
{
//then compare only gender
if( strcmp(p.gender,query.gender)==0)
match=0;
}
//check if Class type is any
else if(strcmp(query.Class,"any")==0)
{
//then compare age and gender type of query to Structure variable ,p
if( strcmp(p.age,query.age)==0 && strcmp(p.gender,query.gender)==0)
match=0;
}
//Check if the age type is any
else if(strcmp(query.age,"any")==0)
{
//then compare class type and gender type of query to Structure variable ,p
if(strcmp(p.Class,query.Class)==0 && strcmp(p.gender,query.gender)==0)
match=0;
}
//Check if the gender type is any
else if(strcmp(query.gender,"any")==0)
{
//then compare class type and age type of query to Structure variable ,p
if(strcmp(p.Class,query.Class)==0 && strcmp(p.age,query.age)==0)
match=0;
}
else
{
//Otherwiser match class type and age and gender of query with Passenger structure variable,p
if(strcmp(p.Class,query.Class)==0 && strcmp(p.age,query.age)==0 && strcmp(p.gender,query.gender)==0)
match=0;
}
return match;
}
//Retunrs the position of the character ch in str of size from start postion
int findchar(char str[], int size, int start, char ch)
{
int i;
for(i = start; i < size; i++) {
if(str[i] == ch) {
return i;
}
}
return -1;
}
void substrcpy(char str1[], int start, int end, char str2[])
{
int i, j;
for(i = start, j = 0; i <= end; i++, j++) {
str2[j] = str1[i];
}
str2[j] = '';
}
/* This function reads one line from an opened file stream */
/* Return the same char pointer pointing to line if reading succeeds */
/* Return NULL pointer if reading fails */
char* readline(FILE *fp, char line[])
{
// Read one line using fgets
if (fgets(line, MAX_LINE_LEN, fp) != NULL) {
int len = strlen(line);
// Remove all the ' ' and ' ' characters at the end of line
while (line[len - 1] == ' ' || line[len - 1] == ' ') {
line[len - 1] = '';
len = strlen(line);
}
// Reading succeeded
return line;
}
else {
// Reading failed
return NULL;
}
}
Explanation / Answer
Program:
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
/* Macro definitions */
#define NUM_OF_LINES 2202
#define MAX_LINE_LEN 99
//structure declaration
typedef struct{
char Class[7];
char age[6];
char gender[7];
char survived[4];
}Passenger;
/* Function declarations */
int findchar(char str[], int size, int start, char ch) ;
void substrcpy(char str1[], int start, int end, char str2[]) ;
char* readline(FILE *fp, char line[]) ;
int cmpStruct(Passenger p, Passenger query);
int main(void)
{
FILE *fp; // A file pointer
char fileName[] = "tit.txt";
fp = fopen(fileName, "r"); // Open the file for reading
char Class[7];
char age[6];
char gender[7];
char survived[4];
int index=0;
Passenger p[NUM_OF_LINES];
if (fp == NULL)
{
// Fail to open the file, quit the program
printf("Error opening file %s. ", fileName);
getch();
return -1;
}
int i1, i2;
char line[MAX_LINE_LEN];
// each line is stored in one row of the array
char lines[NUM_OF_LINES][MAX_LINE_LEN];
//read structure file data
while(!feof(fp)) {
if(readline(fp,line)!=NULL)
strcpy(lines[index],line);
else
break;
int len = strlen(lines[index]);
i1 = findchar(lines[index], len, 0, '=');
i2 = findchar(lines[index], len, i1, ',');
substrcpy(lines[index], i1+1, i2-1, p[index].Class);
i1 = findchar(lines[index], len, i2, '=');
i2 = findchar(lines[index], len, i1, ',');
substrcpy(lines[index], i1+1, i2-1, p[index].age);
i1 = findchar(lines[index], len, i2, '=');
i2 = findchar(lines[index], len, i1, ',');
substrcpy(lines[index], i1+1, i2-1,p[index].gender);
i1 = findchar(lines[index], len, i2, '=');
substrcpy(lines[index], i1+1, len-1, p[index].survived);
index++;
}
int userChoice;
do
{
printf("Please choose an option(1 for query, 0 for quit) : ");
scanf("%d",&userChoice);
//checking if user choice is 1
if(userChoice==1)
{
printf("You choose to query the Titanic data. ");
printf("Which cabin class(first, second, third,crew) :");
//read class type
scanf("%6s",Class);
printf("What age(adult, child) :");
//read age
scanf("%5s",age);
printf("What gender(male,female) :");
scanf("%6s",gender);
//read gender
Passenger query;
strcpy(query.Class,Class);
strcpy(query.age,age);
strcpy(query.gender,gender);
int i=0;
int matchedCount=0;
int survivedCount=0;
while(i<=NUM_OF_LINES) {
//calling cmpStruct that checks matching of given query with structure at index i
if(cmpStruct(p[i], query)==0)
{
matchedCount++;
if(strcmp(p[i].survived,"yes")==0)
survivedCount++;
}
i++;
}
printf("Query result : ");
printf("%d passengers match your query, %d survived. ",matchedCount,survivedCount);
if(matchedCount>0)
printf("Chance of survival is %.1f %% ",((double)survivedCount/matchedCount)*100.0);
else
printf("No match found ");
}
//exit the program if the user choice is 0
else if(userChoice==0)
{
printf("You chose to quit. Bye!");
getch();
//call exit(0) method to close the program
exit(0);
}
if(userChoice<0 || userChoice>1)
printf("Invalid choice");
}while(userChoice<0 || userChoice>1);
fclose(fp); // Close the file after reading
getch();
return 0;
}
//The method cmpStruct that accpets teh passenger and query and returns 0 if the matched ortherwiser returens 1
int cmpStruct(Passenger p, Passenger query)
{
int match=1;
//check if Class type is any and age type any
if(strcmp(query.Class,"any")==0 && strcmp(query.age,"any")==0)
{
//then compare only gender
if( strcmp(p.gender,query.gender)==0)
match=0;
}
//check if Class type is any
else if(strcmp(query.Class,"any")==0)
{
//then compare age and gender type of query to Structure variable ,p
if( strcmp(p.age,query.age)==0 && strcmp(p.gender,query.gender)==0)
match=0;
}
//Check if the age type is any
else if(strcmp(query.age,"any")==0)
{
//then compare class type and gender type of query to Structure variable ,p
if(strcmp(p.Class,query.Class)==0 && strcmp(p.gender,query.gender)==0)
match=0;
}
//Check if the gender type is any
else if(strcmp(query.gender,"any")==0)
{
//then compare class type and age type of query to Structure variable ,p
if(strcmp(p.Class,query.Class)==0 && strcmp(p.age,query.age)==0)
match=0;
}
else
{
//Otherwiser match class type and age and gender of query with Passenger structure variable,p
if(strcmp(p.Class,query.Class)==0 && strcmp(p.age,query.age)==0 && strcmp(p.gender,query.gender)==0)
match=0;
}
return match;
}
//Retunrs the position of the character ch in str of size from start postion
int findchar(char str[], int size, int start, char ch)
{
int i;
for(i = start; i < size; i++) {
if(str[i] == ch) {
return i;
}
}
return -1;
}
void substrcpy(char str1[], int start, int end, char str2[])
{
int i, j;
for(i = start, j = 0; i <= end; i++, j++) {
str2[j] = str1[i];
}
str2[j] = '';
}
/* This function reads one line from an opened file stream */
/* Return the same char pointer pointing to line if reading succeeds */
/* Return NULL pointer if reading fails */
char* readline(FILE *fp, char line[])
{
// Read one line using fgets
if (fgets(line, MAX_LINE_LEN, fp) != NULL) {
int len = strlen(line);
// Remove all the ' ' and ' ' characters at the end of line
while (line[len - 1] == ' ' || line[len - 1] == ' ') {
line[len - 1] = '';
len = strlen(line);
}
// Reading succeeded
return line;
}
else {
// Reading failed
return NULL;
}
}
Output:
Please choose an option(1 for query, 0 for quit) : 1
You choose to query the Titanic data.
Which cabin class(first, second, third,crew) :first
What age(adult, child) :adult
What gender(male,female) :male
Query result :
175 passengers match your query, 57 survived.
Chance of survival is 32.6 %
Please choose an option(1 for query, 0 for quit) : 1
You choose to query the Titanic data.
Which cabin class(first, second, third,crew) :second
What age(adult, child) :child
What gender(male,female) :female
Query result :
13 passengers match your query, 13 survived.
Chance of survival is 100.0 %
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.