GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 22-Feb-2013, 09:30
chem chem is offline
New Member
 
Join Date: Feb 2013
Posts: 3
chem is on a distinguished road

Need Help Completing this Grade Calculator Assignment


I am in need of help my assignment is due this Sunday.

I basically need to write a program where it reads an input file (input_data.txt) containing 5 rows. Each row will contain first name, last name, results from quiz 1, 2, 3, 4, a midterm and a final.

Here is the input file:

John Doe 1, 80, 90, 70, 100, 90, 95
John Doe 2, 90, 80, 90, 90, 95, 100
John Doe 3, 100, 90, 80, 100, 100, 90
John Doe 4, 75, 100, 85, 80, 75, 95
John Doe 5, 60, 85, 90, 85, 100, 80

There are four quizzes 40% total, midterm is 25% and final is 35%. All scores in the input file are recorded out of 100. It then has to compute the final score by using the equation:

final_score = q1*.10 + q2*.10 + q3*.10 + q4*.10 + mid*.25 + final*.35

Then it must determine the final letter grade:

If final_score is >= 90 the student earned an A
If final_score is 80-89 the student earned an B
If final_score is 70-79 the student earned an C
If final_score is 60-69 the student earned an D
If final_score is <= 59 the student earned an F

It will then create an output_data.txt file (the output format is First Name, Last Name: Letter Grade)

Example:

John Doe 1: A
John Doe 2: C
etc...

Here is what I have written. And I'm really need to turn this assignment.

CPP / C++ / C Code:
//Grade Calculator
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main ()
{
	FILE *fptr1;
	FILE *fptr2;
	char fName[20];
	char lName[20];
	char buf[100];
	char *token;
	int q1, q2, q3, q4, mid, final;

	fptr1=fopen("input_data.txt","r");
	
	//reading using fscanf
	while(!feof(fptr1))
	{
		fscanf(fptr1,"%s %s %d, %d, %d, %d, %d, %d", fName,lName,&q1,&q2,&q3,&q4,&mid,&final);
		fclose(fptr1);

	//determine final_score calculation

		int i;
		int final_score;
	
		for (i=0; i<6; i++)
		{
		
		final_score = q1*.10 + q2*.10 + q3*.10 + q4*.10 + mid*.25 + final*.35;
		printf("%d", final_score);
		return final_score;
		}

	//determine letter grade based on final_score

		char letterGrade;
		{
		   	if (final_score >= 90)
				letterGrade = 'A';
			else if (final_score >= 80 && final_score < 90)
				letterGrade = 'B';
			else if (final_score >= 70 && final_score < 80)
				letterGrade = 'C';
			else if (final_score >= 60 && final_score < 70)
				letterGrade = 'D';
			else	
				letterGrade = 'F';
		}

	//display the results - create the output_data.txt file with results
	
	`	fptr2 = fopen("output_data.txt","w");
		If(fptr2)
		{
			fscanf(fptr2, "%s %s : %d\n", fName, lName, &letterGrade);
			fclose(fptr2);
		}
		printf("%s %s %d, fName, lName, letterGrade);


	return 0;
}




Thank you!
  #2  
Old 23-Feb-2013, 16:38
chem chem is offline
New Member
 
Join Date: Feb 2013
Posts: 3
chem is on a distinguished road

Re: Need Help Completing this Grade Calculator Assignment


I seemed to have removed all of the errors and I seem to be able to compile it just fine. But when I run it I get an error. I'm providing the updated code. So far, no one has responded to my posting but I'm trying here. I hope someone can shed some light on this.

CPP / C++ / C Code:
//Grade Calculator
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main ()
{
	FILE *fptr1;
	FILE *fptr2;
	char fName[20];
	char lName[20];
	char buf[100];
	char *token;
	char letterGrade;
	int final_score;
	int count;
	int q1, q2, q3, q4, mid, final;

	for (count=0; count <5; count++)
	{
		fptr1=fopen("input_data.txt","r");
	
	//reading using fscanf
		while(!feof(fptr1))
		{
			fscanf(fptr1,"%s %s %d, %d, %d, %d, %d, %d", fName,lName,&q1,&q2,&q3,&q4,&mid,&final);

	//determine final_score calculation

			{
				final_score = q1*.10 + q2*.10 + q3*.10 + q4*.10 + mid*.25 + final*.35;
				printf("%d", final_score);
			}

	//determine letter grade based on final_score

			{
			 	if (final_score >= 90)
					letterGrade = 'A';
				else if (final_score >= 80 && final_score < 89)
					letterGrade = 'B';
				else if (final_score >= 70 && final_score < 79)
					letterGrade = 'C';
				else if (final_score >= 60 && final_score < 69)
					letterGrade = 'D';
				else	
					letterGrade = 'F';
			}

	//display the results
	
				fptr2 = fopen("output_data.txt","w");
				if(fptr2)
				{
					fprintf(fptr2, "%s %s : %d\n", fName, lName, &letterGrade);
				}
			fclose(fptr1);
			fclose(fptr2);
		}
	}
}
Last edited by admin : 23-Feb-2013 at 23:15. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #3  
Old 23-Feb-2013, 16:58
chem chem is offline
New Member
 
Join Date: Feb 2013
Posts: 3
chem is on a distinguished road

Re: Need Help Completing this Grade Calculator Assignment


Here is the current version. I'm supposed to "append" not "write":

CPP / C++ / C Code:
//Grade Calculator
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main ()
{
	FILE *fptr1;
	FILE *fptr2;
	char fName[20];
	char lName[20];
	char buf[100];
	char *token;
	char letterGrade;
	int final_score;
	int count;
	int q1, q2, q3, q4, mid, final;

	for (count=0; count <5; count++)
	{
		fptr1=fopen("input_data.txt","r");
	
	//reading using fscanf
		while(!feof(fptr1))
		{
			fscanf(fptr1,"%s %s %d, %d, %d, %d, %d, %d", fName,lName,&q1,&q2,&q3,&q4,&mid,&final);

	//determine final_score calculation

			{
				final_score = q1*.10 + q2*.10 + q3*.10 + q4*.10 + mid*.25 + final*.35;
				printf("%d", final_score);
			}

	//determine letter grade based on final_score

			{
			 	if (final_score >= 90)
					letterGrade = 'A';
				else if (final_score >= 80 && final_score < 89)
					letterGrade = 'B';
				else if (final_score >= 70 && final_score < 79)
					letterGrade = 'C';
				else if (final_score >= 60 && final_score < 69)
					letterGrade = 'D';
				else	
					letterGrade = 'F';
			}

	//display the results
	
				fptr2 = fopen("output_data.txt","a");
				if(fptr2)
				{
					fprintf(fptr2, "%s %s : %d\n", fName, lName, &letterGrade);
				}
		}
	}
			fclose(fptr1);
			fclose(fptr2);
}
  #4  
Old 24-Feb-2013, 18:24
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,160
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Need Help Completing this Grade Calculator Assignment


Quote:
Originally Posted by chem
Here is the current version.

So: Is there some kind of problem with the program or a question for which you would like some help?

Here's the drill:

Tell us exactly what happened with the program:
  1. Got compiler messages (warnings or errors) that you don't understand? Were there any messages at all? Tell us what operating system and what compiler version you re using. Sometimes it makes a difference to people who would like to help.

  2. Program crashed? Was there any kind of output before it crashed? If there was any output, was it what you expected or not?

  3. Program ran to conclusion but gave incorrect output? What, exactly, did you expect it to give? What, exactly did it give?


Regards,

Dave
__________________
Sometimes I just can't help myself...
  #5  
Old 24-Feb-2013, 18:26
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,512
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Need Help Completing this Grade Calculator Assignment


How's the program going? Post the latest code and questions, if you may have any. [Basically what Dave suggested in post #4]

I can offer a few suggestions based on the code in post #3:
  1. I would say the loop using 'count < 5' could be removed. I know that there are 5 lines in the input file, but do not rely on a hard-coded limit, as the feof() will indicate the end-of-file to make the while loop stop. Otherwise, the program will need modifcation to alter the 5 and then recompile whenever the #lines may change. Plus, the first iteration actually processes the file, leaving the remaining four iterations with nothing to do!
  2. Open [and verify] the input and output files at the start before the [while] loop that processes the file. No point to continue if the "working files" are not ready.
  3. The fscanf appears to missing a format to capture the number that appears after the last name -- I see 9 values in the input file, but the fscanf is only capturing 8 of those?
  4. The conditions to check the grades for the 80, 70, and 60 ranges can be simplified.
There may be some hassle with the input as there are commas present. I don't know if this is the actual input for the assignment [or if you added the commas by hand in the first post], but there are some various ways to handle that, and it is not known what the limits for this assignment might be. I see that you have an unused 'token' variable [and others], so you may already be contemplating some alternative handling.

HTH
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #6  
Old 23-Jul-2013, 12:07
steamerandy steamerandy is offline
New Member
 
Join Date: Aug 2012
Posts: 27
steamerandy will become famous soon enough

Re: Need Help Completing this Grade Calculator Assignment


TurboPT covered most of what you need.

I would suggest that you do this in steps. Like TurboPT suggests. Open both files to at the beginning. You will wont to process each score and write the result to the output.

But before you do the scoring just read each line and write out the data you read. The example data you showhwas 3 fields in the name. I.E. "John Doe 1". Maybe should be "John Doe1"

If this were a real program you should be validating the input. Varify that each input score is valid. Range check it >0 and <= 100 or what ever. It would be a bit harder but the input format should also be checked. Right number of scores. What would happen if a score was left out of a line? Or as TurboPT pointed the extra (name?) field.

Good luck
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 22:13.


vBulletin, Copyright © 2000 - 2017, Jelsoft Enterprises Ltd.