NEED SOME HOMEWORK HELP ASAP

Write a program to read in a series of characters representing student's grades from a file called "grades.txt" until the first blank space is reached. Echo and print the input data, one character per line. If the grade is A, B, C, or D, it is a passing grade. If it is F, it is a failing grade. Any other character will produce an error message.

The program must count how many passing grades, how many failing grades and how many errors were found. At the end of the program, print all the counts. YOU MUST USE function(s) to solve the program, such as a function to get and print the input data, and a function to check whether the grade is pass of fail or error.

For example: if the file contained
AFBDFXC XABCYZ
the program should report:

there were 4 passes, 2 failure and 1 error.

Remember, it must stop at the blank! You need to use the get function to read the blank.



Here's my code:

#include<iostream>
#include<string>
#include<fstream>
using namespace std;


string gradech = "-";

void print(int pass, int fail , int error);



int main()
{
ifstream infile;
infile.open("grades.txt");


while(!infile.eof())
{
infile >> gradech;
int failure=0;
int error=0;
int pass=0;
for (unsigned i = 0; i < gradech.length(); i++)
{

if(gradech.at(i) =='A')
pass++;
else if(gradech.at(i) =='B')
pass++;
else if(gradech.at(i) =='C')
pass++;
else if(gradech.at(i)=='D')
pass++;
else if(gradech.at(i) =='F')
failure++;
else
error++;

}


print(pass,failure,error);

}
return 0;

system("pause");

}

void print(int pass, int fail , int error)
{

cout<<"Pass : "<< pass <<" Fail : "<< fail <<" Error : "<< error <<endl;
}

It doesn't seem to read past the "else" statement in the program. any help would be nice
whats the words in grades.txt and when is it due?
Last edited on
My bad. the words are in the example and this stuff is due by 7.
Last edited on
I only need the program to read the first couple of characters. I believe something is wrong with the while loop function. Any help would be greatly appreciated.
2345; Just want to jump in and ask a question or 2 and make a long comment.

Have you designed the problem in Pseudo code first? I ask because it seems like you are starting to code without a plan. I use RATFOR myself, but nearly pseudo language will work. I have taken your requirements and broken them into logical groups for you. From here you can begin writing code to "fill-in-the-blanks"
Define your functions from your requirements
Define your Vars as needed
Document and comment as you progress.
Here I've added your requirements - slightly re-arranged for reading
and my very quick rendering of the program in, well, in nothing, but a logical structure.
Print it and read it over.
Remember, always lay-out your design in Pseudo Code first.

- - -
Write a program to read in
a series of characters representing student's grades
from a file called "grades.txt"
until the first blank space is reached.

Echo and print the input data,
one character per line.

If the grade is A, B, C, or D,
[THEN] it is a passing grade.

If it is F,
[THEN] it is a failing grade.

Any other character
[THEN] will produce an error message.


The program must
count how many passing grades,
how many failing grades and
how many errors were found.

At the end of the program,
print all the counts.

YOU MUST USE function(s) to solve the program,
such as a function to
get [input data]
print input data,

and a function to check whether the grade is
pass [GRADE]
or fail [GRADE]
or error [GRADE].

- - - - - - - - - - - - - - - - - - - - - - - -- - - - - -

start

Define a char
name it myGrade
define other vars as required

MAIN ()
{

// get the grades
// move the grade into var myGrade
// check that myGrade != BLANK or SPACE or NULL
DO Read_File("grades.txt") ;

DO WHILE file !EOF() && char(myGrade) != " " ; {

-----DO GET_Grade ();
-----DO COUNT ();
-----DO PRINT_COUNTS ();


-----}

RETURN(); // END OF MAIN()




// FUNC to get a GRADE

func GET_Grade ()

INPUT myGrade;
echo MyGrade;

IF myGrade == "A" OR myGrade == "B" OR myGrade == "C"OR myGrade == "D"
THEN DO
// passing GRADE

DO func ( COUNT )


ENDDO



IF myGrade == "F"
THEN DO

// FAILING GRADE

DO func ( COUNT ) // rem to add these failures into the final count as well

ENDDO

ELSE
PRINT some ERROR MSG // Bad Key Press, clear input buffer & try again

DO func ( COUNT ) // rem to add these ERRORS into the final count as well


END_ELSE

RETURN();




// Function to total the number of each grade level A-D & F

FNC COUNT ()

IF myGrade == "A" // 4.0 gpa
then my_A_grade++;

ELSE IF myGrade == "B" // 3.0 gpa
then my_B_grade++;

ELSE IF myGrade == "C" // 2.0 gpa
then my_C_grade++;

ELSE IF myGrade == "D" //
then my_D_grade++;


ELSE IF myGrade == "F" // FAIL
then my_F_grade++;


ELSE IF myGrade == "E" // "E" for ERROR
then my_E_grade++;


Overall_TOTAL_Pass = my_A_grade + my_B_grade +my_C_grade + my_D_grade ;
Overall_TOTAL_FAIL = my_F_grade;
Overall_TOTAL_ERROR = my_E_grade;
Grand_TOTAL = Overall_TOTAL_Pass + Overall_TOTAL_FAIL + Overall_TOTAL_ERROR;

RETURN();




// Function PRINT ALL COUNTS

FNC PRINT_COUNTS ()

PRINT Overall_TOTAL_Pass ;
PRINT Overall_TOTAL_FAIL ;
PRINT Overall_TOTAL_ERROR ;
PRINT Grand_TOTAL ;

RETURN();






Last edited on
use [code][/code] tags please
thank you Incis for your advice. I will be sure to apply this knowledge to all future projects. I rewrote the entire program. Turns out I wasn't even able to open the file to read it. I made sure the program was reading the input well and then proceeded on to fixing everything else. I have to submit my homework seeing as to how it is past seven but I truly, truly appreciate all of the words that you have given me. I hope this forum ends up helping someone else as well.
Last edited on
// Here's my code:

#include<iostream>
#include<string>
#include<fstream>
using namespace std;


string gradech = "-";

void print(int pass, int fail , int error);



int main()
{
ifstream infile;
infile.open("grades.txt"); //
//AFBDFXC XABCYZ

while(!infile.eof())
{

infile >> gradech;
int failure=0;
int error=0;
int pass=0;
int i=0;

for ( i = 0; i < gradech.length(); i++) {

if(gradech.at(i) =='A')
pass++;

else if(gradech.at(i) =='B')
pass++;

else if(gradech.at(i) =='C')
pass++;

else if(gradech.at(i)=='D')
pass++;

else if(gradech.at(i) =='F')
failure++;
else
error++;

// Testing to see what my intermit values are at this point;
printf(" \n\nval of -i- = %d ", i );
printf(" \nLetter of myGrade = %c ", gradech.at(i) );
printf(" \nLength of myGrade = %d ", gradech.length() );
printf(" \nValue of -pass- = %d ", pass );
printf(" \nValue of -failure- = %d ", failure );
printf(" \nValue of -error- = %d ", error );


}

print(pass,failure,error);

}
return 0;

//system("pause");

}

void print(int pass, int fail , int error)
{
//cout<<"Pass : "<< pass <<" Fail : "<< fail <<" Error : "<< error <<endl;

printf(" \n\nValue of -pass- = %d ", pass );
printf(" \nValue of -failure- = %d ", fail );
printf(" \nValue of -error- = %d ", error );
scanf( "%d", &pass); // something to hold the screen while you read.

}

// It doesn't seem to read past the "else" statement in the program. any help would be nice


2345,
I removed the sys pause
The error is/was in the print fnc()
It's always good to use PRINTF statements as FLAGS to not only see where you are, where the problem lies, but also to see if the values you expect are the ones the machine is providing.
Good Luck.
ps, while this runs fine, it doesn't match your teachers requirements very much. - just saying.


Last edited on
This should solve what you want...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
#define PROMPT cout << "<Press ENTER KEY to continue>";
#define ENDPROMPT cout << "<Press ENTER KEY to Exit>";
#define PAUSE cin.ignore(numeric_limits<streamsize>::max(), '\n' );
void getstuff(ifstream &fin);
void print(int pass, int fail , int error);
int main()
{
ifstream infile;
infile.open("grades.txt");



getstuff(infile);
return 0;
}

void print(int pass, int fail , int error)
{

	cout<<"Pass : "<<pass <<" Fail : "<<fail <<" Error : "<<error<<endl;
}

void getstuff(ifstream &infile)
{
	string stemp;
	int x =0 ;
	int failure=0;
    int error=0;
    int pass=0;
	while(!infile.eof())
{
	infile>>stemp;
	for (int i = 0 ; i < stemp.length();i++)
	{
		stemp.at(i);
		if(stemp.at(i) =='A')
			pass++;
		else if(stemp.at(i) =='B')
			pass++;
		else if(stemp.at(i) =='C')
			pass++;
		else if(stemp.at(i)=='D')
			pass++;
		else if(stemp.at(i) =='F')
			failure++;
		else
			error++;
	}
	print(pass,failure,error);
	cout<<"WE ARE AT A BLANK"
		<<" do you want to continue reading file?"<<endl
		<<"type y for yes n for no :";
	getline(cin,stemp);
	if (stemp=="y"||stemp=="Y")
	{
	 ;
	}
	else
		{ x= 1;
	break;}
	PROMPT;
	PAUSE;

}

	if (x==1)
	{;}
	else
	{
	cout<<"END OF FILE ."<<endl;

	}
	cout<<"PROGRAM CLOSING."<<endl;
        infile.close();
	ENDPROMPT;
	PAUSE;

}




Last edited on
Topic archived. No new replies allowed.