Hi all, I'm trying to get my program to read from a file that has both characters and integers (names and scores in a class). The file is set up like this with 30 names and scores:
Elizabeth 74
How do I make my program print this as shown? Every time I try to do it I get a whole bunch of weird characters (like a bunch of Os or Hs all in a line), and it's driving me mad. I even tried replacing all the names and scores in my file with just letter grades to test it, and it still didn't work properly. What am I doing wrong? I checked the FAQ, but it didn't mention anything like this. Also, is it possible to set up an array with the names and scores just like I've shown up there? I need to use the scores later in my program to calculate the class average, the highest score, and the lowest score. I'll provide the code I have now (which isn't much, admittedly) as well as the text file.
Here are the contents of my text file (these are all made up names and scores, by the way):Code:#include <stdio.h> #include <stdlib.h> #include <stdbool.h> #define ARRAY_SIZE 30 int main(void) { FILE* spStudents; float average = 0; char students; int scores; printf("Welcome! This program will read a text file containing names\n"); printf("and scores of 30 students enrolled in a class. The program will\n"); printf("arrange the list by score, find the class average, find the\n"); printf("student with the lowest score, and find the student with\n"); printf("the highest score.\n\n"); spStudents = fopen("L:\\CIS 161\\Chapter VIII pt 1\\students.txt", "r"); if (spStudents == NULL) { printf("Error opening students.txt for reading\n"); exit (100); } printf("Here are the students and scores unsorted:\n"); fclose(spStudents); system("PAUSE"); return 0; }
Brendon 82
Abigail 77
Ethan 72
Madeline 88
Caleb 90
Connor 69
Emma 71
Jacob 80
Jack 91
Elizabeth 74
Andrew 84
Gabriel 92
Chloe 93
Ian 73
Isaac 89
Claire 98
Evan 75
Rachel 85
David 86
Carly 78
Jake 79
Amber 66
Henry 81
Aubrey 70
Julianna 83
Hunter 68
Joseph 62
Lucy 99
Erin 59
Jason 94



LinkBack URL
About LinkBacks



