ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
i need to write a program that will compute numeric grades for a course. the course records are in a file that will serve as the input file. the input file is in exactly the following format: eath line contains a student's last name. then one space, then the students first name, then one space, then ten quiz scores all on one line. the quiz scores are whole numbers and are separated by one space. i need to output the data exactly how it is to an output file but i also need to add up the grades for each student and output the average.
i'm having troubles figuring out how to disregard the students last, and first name so i can start reading the scores to add up. so basically i need not to worry about first or last name only the scores. in help would be greatly appreciated.
use fscanf() - if I understood your file layout (code skeleton you need to add stuff):
Code:
FILE *in;
int average
char fnm[20],lnm[20],g1[5],g2[5],g3[5].... and so on;
in=fopen("myifile","r");
while(fscanf(in,"%s %s %s %s %s %s %s %s %s %s %s %s,
lnm,fnm,g1,g23,g3,g4,g5,g6,g7,g8,g9,g10 )!=EOF){
/* use atoi() to convert each string to a number
then calc the average or whatever */
}
fclose(in);
there has to be an easier way to do it than that. maybe someway with the isspace function. i don't know for sure.....someway to start adding the number after the second space is read in. i'm not making the whole line a string, but rather reading each letter in as a character. do you think i need to declare each line as a string or what.
is this C or C++ or what? Each line won't be read as a string, but each word will be delimited by a space if you use scanf / fscanf in C or cin / inFile (or whatever you call it) in C++. Using scanf or cin (or file input equivalents) will know a space when it sees it and will stop at it so you don't have to worry about it.
If is is C++ then have some temp string variable to hold the name variables that can be overwritten every time. Then you can have something like...
(sorry, I don't know how to format this code.
while(!inFile.eof())
{
inFile<<tempString; //read surname into temp var
inFile<<tempString; //read first name into temp var
totalGrade = 0;
for(int i = 0; i < 10; i++)
{
totalGrade += grades[i];
}
average = totalGrade / 10;
}
eh, I haven't tested this code so don't take it as gospel...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.