LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-18-2003, 09:31 AM   #1
leroy27336
Member
 
Registered: Nov 2003
Distribution: LTS Ubuntu 6.06
Posts: 127

Rep: Reputation: 15
Help with code


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.
 
Old 11-18-2003, 09:45 AM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
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);
 
Old 11-18-2003, 09:51 AM   #3
leroy27336
Member
 
Registered: Nov 2003
Distribution: LTS Ubuntu 6.06
Posts: 127

Original Poster
Rep: Reputation: 15
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.
 
Old 11-18-2003, 09:55 AM   #4
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
I gave you part of what your teacher probably expected you to use.
Parsing out numbers from a series of fgetc() calls is a lot harder.
 
Old 11-18-2003, 09:58 AM   #5
leroy27336
Member
 
Registered: Nov 2003
Distribution: LTS Ubuntu 6.06
Posts: 127

Original Poster
Rep: Reputation: 15
we havn't learned fscanf yet though. that's why i'm assuming there has to be an easier way. thanks for all your help
 
Old 11-18-2003, 10:27 AM   #6
gearoid
Member
 
Registered: Nov 2003
Location: Ireland
Distribution: Red Hat 9
Posts: 49

Rep: Reputation: 15
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...
 
Old 11-18-2003, 10:31 AM   #7
gearoid
Member
 
Registered: Nov 2003
Location: Ireland
Distribution: Red Hat 9
Posts: 49

Rep: Reputation: 15
while(!inFile.eof())
{
inFile<<tempString; //read surname into temp var
inFile<<tempString; //read first name into temp var

totalGrade = 0;

sorry, forgot to read in the grades first!!
like I said - don't take as gospel...
for(int i = 0; i < 10; i++)
{
inFile<<grades[i];
}

for(int i = 0; i < 10; i++)
{
totalGrade += grades[i];
}
average = totalGrade / 10;
}

.....or something to that affect
 
Old 11-21-2003, 05:11 AM   #8
8nasmith
Member
 
Registered: Oct 2003
Location: Cornwall, UK
Distribution: Fedora Core 4
Posts: 268

Rep: Reputation: 30
does any one use turbo pascal or do you use quick basic?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
small syntax problem with C code (implemented in Code Composer Studio) illiniguy3043 Programming 6 01-07-2008 02:14 AM
User Preferences: Use HTML code instead of vB code? (vB code is overrated) stefanlasiewski LQ Suggestions & Feedback 5 07-26-2005 01:37 AM
Editing buttons (quote, code etc.) add the code to the end vharishankar LQ Suggestions & Feedback 2 09-13-2004 09:32 AM
Diffrerence between position independent code and Relocatable code? eshwar_ind Programming 7 05-11-2004 01:40 AM
Open Firmware code for booting OS from SATA : sample code available somewhere ? drsparikh Linux - Hardware 0 03-12-2004 11:16 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:26 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration