Hello friends,
I am creating a program and am having trouble with one part. The input file looks like:
Code:
2
3
Bill Jeff Steve
Linda Brittany Jessica
3 1 4
2 1 9
8 3 7
6 4 9
4 8 9
6 9 4
Where I am stuck is putting the numbers into the string array. For example, Bill's scores should be set up as Bill's scores should be set as
Code:
bill_score = {3, 1, 4}
and when this code is ran
Code:
for (i=0; i<3; i++)
{
printf("%s - ", men[i]);
printf("%d ", bill_score);
}
it should output
Here is what I have so far, thanks you guys.
Code:
# include <stdio.h>
# include <stdlib.h>
# define MAX_CHARS 20 // MAX_CHARS is the precondition of the number of letters each name can have.
int main()
{
// Opens the file and tests to see if the file is being read properly.
FILE *ifp = fopen("matching.txt ", "r");
if (!ifp)
{
printf("The file is unable to open!\n");
return -1;
}
// Variables: i is the initializer; num_events is the number of events; num_pairs is the number of pairs.
int i, num_events, num_pairs;
// Reads in the first line-- the number of dating sessions.
fscanf(ifp, "%d", &num_events);
// Reads in the number of pairs
fscanf(ifp, "%d", &num_pairs);
//printf("The number of events is: %d\n", num_events);
//printf("The number of pairs is: %d\n\n", num_pairs);
// Mallocing space for a char-pointer at the size of number of pairs.
char **men = malloc(sizeof(char*) *num_pairs);
char **women = malloc(sizeof(char*) *num_pairs);
// Loops through the whole group of daters and their scores.
for (i=0; i< num_events; i++)
{
// This loop allocates space for the names of each of the men into an array
for (i=0; i< num_pairs; i++)
{
// For each man, dynamically allocate memory for the size of 20 chars(MAX_CHARS)
men[i] = malloc(sizeof(char) *MAX_CHARS);
fscanf(ifp, "%s", men[i]);
printf("%s ", men[i]);
// Debugger to test to see if the names are being read in properly... printf("%s\n", men[i]);
}
printf("\n");
for (i=0; i< num_pairs; i++)
{
// For each woman, dynamically allocate memory for the size of 20 chars(MAX_CHARS)
women[i] = malloc(sizeof(char) *MAX_CHARS);
// Scans in the name of the women and alloocates them to women[i]
fscanf(ifp, "%s", women[i]);
printf("%s ", women[i]);
}
printf("\n");
printf("\n");
}
// Loops and prints the output for the number of events.
for (i=0; i < num_events; i++)
{
printf("Matching #%d: Maximum Score = \n", i+1);
}
return 0;
}