thanks for the help!!! I have been working on this program nonstop for about 7 hours or so.
Here's what I have in my main.c file, .h file, and another .c that contains my functions, Here they are in that order.
Here's the problem now. I am trying to read in a file called "matchmaker.txt" and it will NOT read it in. Even if I put the file in directly I always get a return for it not being read "file can not be found" from my program.
matchmaker.c
Code:
#include <stdio.h>
#include <stdlib.h>
#include "matchmaker.h"
int main(int argc, char *argv[])
{
int total, x;
FILE* fp = NULL;
person* array = NULL;
fp = openfile();
total = readtotal(fp);
array = readfile(fp, total);
fclose(fp);
findMatch(array, total);
for(x = 0; x < total; x++)
{
free(array[x].first);
array[x].first=NULL;
free(array[x].last);
array[x].last=NULL;
free(array[x].color);
array[x].color=NULL;
free(array[x].hobby);
array[x].hobby=NULL;
}
free(array);
array = NULL;
system("PAUSE");
return 0;
}
header file - "matchmaker.h"
Code:
#ifndef matchmaker_H
#define matchmaker_H
#include <stdio.h>
#include <stdlib.h>
struct Person
{
char* first;
char* last;
char sex;
int age;
char* color;
char* hobby;
};
typedef struct Person person;
FILE* openfile();
int readtotal(FILE* fp);
person * readfile(FILE * fp, int total);
void findMatch(person *array, int total);
#endif
functions file "matchmakerFunc.c"
Code:
#include "matchmaker.h"
FILE* openfile(void)
{
FILE* fp = NULL;
char filename[100];
int len = 0;
//printf("Enter the name of the file: ");
fgets(filename, 100, stdin);
//scanf("%s", filename); //i tried this thinking maybe it could be the fgets causing the problem, but it's not
len = strlen(filename);
if(filename[len-1] = '\n')
filename[len-1] = '\0';
fp = fopen(filename, "r");
if(fp == NULL)
{
printf("That file does not exist!");
//exit(-1);
}
return fp;
}
int readtotal(FILE* fp)
{
int total;
fscanf(fp, "%i", &total);
getc(fp);
return total;
}
person * readfile(FILE* fp, int total)
{
int x;
char temp[100];
person * array = (person*)malloc(total*sizeof(person));
if(array=NULL)
exit (-1);
for(x = 0; x < total; x++)
{
fgets(temp, 100, fp);
array[x].first = (char *)malloc(strlen(temp)*sizeof(char));
if(array[x].first == NULL)
exit(-1);
strncpy(array[x].first, temp, strlen(temp)-1);
fgets(temp, 100, fp);
array[x].last = (char *)malloc(strlen(temp)*sizeof(char));
if(array[x].last == NULL)
exit(-1);
strncpy(array[x].last, temp, strlen(temp)-1);
fscanf(fp, "%c", &array[x].sex);
getc(fp);
fscanf(fp, "%i", &array[x].age);
getc(fp);
fgets(temp, 100, fp);
array[x].color = (char *)malloc(strlen(temp)*sizeof(char));
if(array[x].color == NULL)
exit(-1);
strncpy(array[x].color, temp, strlen(temp)-1);
fgets(temp, 100, fp);
array[x].hobby = (char*)malloc(strlen(temp)*sizeof(char));
if(array[x].hobby == NULL)
exit(-1);
strncpy(array[x].hobby, temp, strlen(temp)-1);
}
return array;
}
void findMatch(person * array, int total)
{
int outer;
int inner;
int found, count1 = 0, count2 = 0;
for(outer=0; outer < total; outer++)
{
count1 = 0;
count2 = 0;
for(inner = 0; inner < total; inner++)
{
found = 0;
if(outer != inner)
{
if(array[outer].sex != array[inner].sex)
{
if(array[outer].age - array[inner].age < 6 || array[outer].age - array[inner].age > -6 ) found++;
if(strcmp(array[outer].color, array[inner].color) == 0) found++;
if(strcmp(array[outer].hobby, array[inner].hobby) == 0) found++;
}
}
if(count1 = 0)
{
printf("First Name: %s", array[outer].first);
printf("Last Name: %s", array[outer].last);
printf("Gender: %i", array[outer].sex);
printf("Age: %c", array[outer].age);
printf("Color: %s", array[outer].color);
printf("Hobby: %s", array[outer].hobby);
printf("\n\nPROSPECTIVE MATCHES\n----------------------------\n\n");
count1++;
}
if(found > 1)
{
printf("First Name: %s", array[inner].first);
printf("\nLast Name: %s", array[inner].last);
printf("\nGender: %i", array[inner].sex);
printf("\nAge: %c", array[inner].age);
printf("\nColor: %s", array[inner].color);
printf("\nHobby: %s", array[inner].hobby);
}
}
}
}