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++)
{
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].
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.
// 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 );
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.