Hello,
I'm trying to write a program where a file is read, line by line, then a function is called that changes each line to a letter grade, then writes the grades out to a different file. I'm using a for loop to calculate the grade as each line is read, but when I go to call the file, I'm getting a whole slew of errors.. can anyone help me with making sure this program is reading each line correctly? And can call the void function accurately? Any help is appreciated. Thank you.
It was a long function that designates a letter grade based on a given integer. I've included it below, but originally didn't (in the interest of saving space!)
void grade()
{
int x;
if ((x >= 97) && (x <= 100))
cout <<"Your grade is an A+" << endl;
else if ((x >= 92) && (x < 97))
cout <<"Your grade is an A" << endl;
else if ((x >= 92) && (x < 97))
cout <<"Your grade is an A" << endl;
else if ((x >= 87) && (x < 92))
cout <<"Your grade is an A-" << endl;
else if ((x >= 82) && (x < 87))
cout <<"Your grade is a B+" << endl;
else if ((x >= 77) && (x < 82))
cout <<"Your grade is a B" << endl;
else if ((x >= 72) && (x < 77))
cout <<"Your grade is a B-" << endl;
else if ((x >= 67) && (x < 72))
cout <<"Your grade is a C+" << endl;
else if ((x >= 62) && (x < 67))
cout <<"Your grade is a C" << endl;
else if ((x >= 57) && (x < 62))
cout <<"Your grade is a C-" << endl;
else if ((x >= 52) && (x < 57))
cout <<"Your grade is a D" << endl;
else if ((x >= 47) && (x < 52))
cout <<"Your grade is a D-" << endl;
else if ((x<47) && (x>=0))
cout <<"Your grade is an E" <<endl;
else
cout <<"Error" << endl;
}
OK, well now we're getting somewhere. You're still missing some clean-up stuff that I mentioned above, but I understand the program now.
By the way, while it may not matter in this day and age of ultra-fast computers, your computation algorithm is inefficient. Note that your "else if" statements contain an unnecessary test, since (for example) if it fails the (x >= 97) test, you don't need the (x < 97) two lines below.
Moschops: I didn't know I had to give it a value when creating it - can I not leave it undefined until the loop, wherein it takes a meaning every time the loop is iterated through? (Depending on what the line in the input file reads?)
mzimmers: How do I include the formatting blocks? In the meantime, I've cleaned up the code a bit, am wondering if writing the output of each iteration of the loop to an array would be the easiest way to eventually write it to a different file, (but that part is still in the works. Here, I just initiated the array but am still trying to figure out how to write to it and eventually take value in the array to write to a new file)..
In your reply dialog, there are some formatting buttons to the right. Choose the angle brackets, and put your code in between.
I'm getting the impression you're rushing through this. Take your time, get your code cleaned up and repost it. One fix you'll need is in the parameter passing to grade(); it should look like this:
1 2
void grade(int x)
{
Also, grade() is a void function. That means it doesn't return anything as you're trying to do with y. You're already outputting the grade from grade(), so you don't need to do it again in main().
One other thing: your getline call isn't right. Look at this page for how to do it:
So, I've cleaned it up a bit and hopefully made steps forwards. I'm still getting a couple of errors regarding using "grades" as a function. running the function also returns the error; "invalid conversion from 'void*' to 'int'
Also, I've decided to try and write the list of inputted grades to a string. I hope that by defining the string as having one one column and many rows, that the loop will iterate through it and write to it correctly.
Yes, you're definitely making progress. A couple of things:
string str[1][100]; doesn't really make sense. A two-dimensional array where one dimension is only 1 element wide, is really a one-dimensional array. Get rid of the [1].
I notice you define an int variable grades in main(). This is going to produce undesirable results; avoid naming variables with the same name as functions you're using.
You're still not using getline properly. To call a function of an object, do it like this:
Huh I guess I find the documentation about getline confusing since mine is in a loop... will updating 'a' successfully iterate through each part of the string? (I think this is done by a++ in c++ - in Python, its a += 1).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
int main()
{
int x;
ifstream infile ("scores.txt");
ofstream outfile ("result.txt");
string str[100];
int a = 0;
if(!infile) //testing to make sure the file is open
{
cout<<"Error opening file"<<endl;
return -1;
}
while (! infile.eof()) {
x = infile.getline(str[a],'\n');
outfile << grades(x) << endl;
a++
}
return 0;
}
I'm not sure I fully understand what you're trying to do here. You want to read an int in from your input file, assign it a grade, and output the grade, right? You can simplify the input by eliminating getline (which returns text) and just do:
infile >> x;
This takes advantage of the istream formatted input operator, and puts a number directly into x.
Now: if you want grades() to return a string, you need to define it as a string function:
string grades (int x)
and you need to return a string from it. THEN your outfile will get the desired output. I'd recommend you store the letter grade in a string within grades() and return it at the end.
If I've misunderstood your assignment, let me know.
I wanted to write a program that would read in an input of integer scores (one per line) and output the corresponding grades (one per line)...
So, my "scores.txt" looks like:
98
92
94
47
30
So, the "result.txt" should eventually look like:
A+
A
A
D-
E
With that in mind, I still don't see quite why I need parameters in my getline() call, since it will only be returning a 2-character string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
int main()
{
int x;
ifstream infile ("scores.txt");
ofstream outfile ("result.txt");
if(!infile)
{
cout<<"Error opening file"<<endl;
return -1
}
while (! infile.eof()) {
x = int(infile.getline());
outfile << grades(x) <<endl;
}
return 0;
]
I expected that calling grades(x) in my fourth to last line would write it to the new file, but instead, it is just giving me errors. Thanks for your patience with this.