calling a function outside of main

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.

#include <iostream>
#include <fstream>
#include <math.h>

//File: grade.cpp

using namespace std;

void grade()
{
'''stuff that takes an integer as input and outputs a letter grade'''
}

int main()
{
int x;
string y;
int line;
ifstream infile ("scores.txt");
ofstream outfile ("result.txt");
if (outfile.is_open())
{
while (! infile.eof()) {
getline(infile,line);
y = grade(line);
cout << outfile.write(y) << endl;
}
}
What is this line supposed to be?

'''stuff that takes an integer as input and outputs a letter grade'''

In any event, grade() isn't a valid function as currently coded. Its definition is also missing a parameter.

You're also missing a return statement for main().

Finally, when you post code, please use the formatting blocks (like I did above). Thanks.
Last edited on
Also, the function takes no input parameters, void grade() , but you're trying to pass it one - grade(line);
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;
}
In the function grade, you create an int value called x, and then start examining it. What value does it have when you make it?
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)..

#include <iostream>
#include <fstream>
#include <math.h>
#include <string>
#include <map>
#include <vector>

//File: grade.cpp

using namespace std;

void grade(x)
{
int x;
if ((x >= 97) && (x <= 100))
cout << "A+" << endl;
else if (x >= 92)
cout <<"A" << endl;
else if (x >= 87)
cout <<"A-" << endl;
else if (x >= 82)
cout <<"B+" << endl;
else if (x >= 77)
cout <<"B" << endl;
else if (x >= 72)
cout <<"B-" << endl;
else if((x >= 67)
cout <<"C+" << endl;
else if (x >= 62)
cout <<"C" << endl;
else if (x >= 57
cout <<"C-" << endl;
else if (x >= 52
cout <<"D" << endl;
else if (x >= 47
cout <<"D-" << endl;
else if (x<47)
cout <<"E" <<endl;
else
cout <<"Error" << endl;
}

int main()
{
int x = 0;
string y;
ifstream infile ("scores.txt");
ofstream outfile ("result.txt");
char grades[200];
if (outfile.is_open())
{
while (! infile.eof()) {
getline(infile,x);
y = grade(x);
cout << outfile.write(y) << endl;
}
return 0;
}
Oops - have added the missing parentheses towards the end of the grade(x) function, too.
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:

http://www.cplusplus.com/reference/iostream/istream/getline/
Last edited on
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.

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
#include <iostream>
#include <fstream>
#include <math.h>
#include <string>

//File: grade.cpp

using namespace std;
 
int grades(int x)
{
  if ((x >= 97) && (x <= 100))
    cout << "A+" << endl;
  else if (x >= 92)
    cout <<"A" << endl;
  else if (x >= 87)
    cout <<"A-" << endl;
  else if (x >= 82)
    cout <<"B+" << endl;
  else if (x >= 77)
    cout <<"B" << endl;
  else if (x >= 72)
    cout <<"B-" << endl;
  else if (x >= 67)
    cout <<"C+" << endl;
  else if (x >= 62)
    cout <<"C" << endl;
  else if (x >= 57)
    cout <<"C-" << endl;
  else if (x >= 52)
    cout <<"D" << endl;
  else if (x >= 47)
    cout <<"D-" << endl;
  else if (x<47)
   cout <<"E" <<endl;
  else
   cout <<"Error" << endl;
}

int main()
{
  int x;
  int grades;
  ifstream infile ("scores.txt");
  ofstream outfile ("result.txt");
  string str[1][100];
  int a = 0;
  int b = 0;
  if(!infile) //testing to make sure the file is open
    {
      cout<<"Error opening file"<<endl;
      return -1;
	}
  while (! infile.eof()) 
    {
     x= getline(infile,str[a][b],'\n'));
     outfile << grades(x) << endl;
  }
  return 0;
}
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:

infile.getline(param, param, ...)

Fix these, and we can go from there.
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.
First off, regarding:

x = int(infile.getline());

is an attempt to cast a string as an int. That doesn't work. You need to use something like atoi() to do that.

And, you need parameters in your getline() because getline() *requires* them.

http://www.cplusplus.com/reference/iostream/istream/getline/

It needs a stream, and size.

I still think you're better off using the formatted input that I mentioned above.
Topic archived. No new replies allowed.