OK..so here's the problem...
I'm supposed to create a program that reads a text file containing 5 lines, each composed of a name, a colon, and three integers. For example:
Bill Gates: 10 20 30
Shalla Booger: 80 70 84
Hagar Joe Plinty: 70 90 80
Darth Vader: 60 50 80
Mace Windu: 50 60 80
Now, what i'm supposed to do is take the average value of the three numbers for each name and then sort them so that the names are in ascending order based on the average. So far, I've only come up with this:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#define ROW 5
#define COLUMN 100
#define FILENAME "file1.txt"
int main(void)
{
FILE *fp1;
char array[ROW][COLUMN], test_array[COLUMN], *ptr;
int cnt = 0;
fp1 = fopen(FILENAME, "r"); // open a file for reading
while (fgets(array[cnt], COLUMN, fp1) != NULL)
{
strcpy(test_array, array[cnt]); // copy each string into an array
printf("%s", test_array);
ptr = test_array;
while (*ptr != '\0')
{
if (isdigit(*ptr))
// something goes here
ptr++;
}
cnt++;
}
return 0;
}
Basically, I want to use fgets in a while loop to read in every line from the file and store each line in a 2-D array (dunno if strcpy should be there to place each line in a single dimension array for searching). Then, using some kind of search loop, extract each set of numbers, average them, and then put them into another array so that I can sort them later.
The difficult part that I've run into is extracting the numbers. I can't think of a way to get a 2 digit number from a string out.
Any help you guys can offer would be helpful.