program won't compile - problem with void function

I'm getting the following errors:

10 E:\Lab 7 - Test Averages\Lab7.cpp variable or field `calcavg' declared void
10 E:\Lab 7 - Test Averages\Lab7.cpp `ifstream' was not declared in this scope
10 E:\Lab 7 - Test Averages\Lab7.cpp `ifstream' was not declared in this scope
10 E:\Lab 7 - Test Averages\Lab7.cpp `string' was not declared in this scope
10 E:\Lab 7 - Test Averages\Lab7.cpp `name' was not declared in this scope
10 E:\Lab 7 - Test Averages\Lab7.cpp expected primary-expression before "float"

^I get this error 6 times, total. All line 10.

10 E:\Lab 7 - Test Averages\Lab7.cpp initializer expression list treated as compound expression

E:\Lab 7 - Test Averages\Lab7.cpp In function `int main(int, char**)':
26 E:\Lab 7 - Test Averages\Lab7.cpp `calcavg' cannot be used as a function

So, there's something wrong with what I coded in line 10, but I don't see the problem...

My program code as of right now:


#include <cstdlib>
#include <iostream>
#include <fstream>
//User-defined functions prototypes.
void calcavg(ifstream&indat,string&name,float&t1,float&t2,float&t3,float&t4,float&t5,float&avg);
char calcgrade(float avg);

using namespace std;
int main(int argc, char *argv[])
//Main body.
{
ifstream indat;
ofstream outdat;
char lettergrade;
float average,t1,t2,t3,t4,t5,avg,total=0,avgclass;
string name;
int counter=0;
indat.open("E:classtestscores.txt");
outdat.open("E:classaverageandgrade.txt");
while(!indat.eof())
{calcavg(indat,name,t1,t2,t3,t4,t5,avg);
lettergrade=calcgrade(avg);
outdat<<name<<"\t"<<t1<<"\t"<<t2<<"\t"<<t3<<"\t"<<t4<<"\t"<<t5<<"\t"<<avg<<"\t"<<lettergrade;
counter++;
total+=avg;
}
avgclass=total/counter;
outdat<<"Class average: "<<avgclass<<endl;
indat.close();
outdat.close();
system("PAUSE");
return EXIT_SUCCESS;
}

//Definition of calcgrade:
char calcgrade(float avg)
{
char lettergrade;
if(avg>=90 && avg<=100)
{lettergrade='A';}
if(avg>=80 && avg<=89)
{lettergrade='B';}
if(avg>=70 && avg<=79)
{lettergrade='C';}
if(avg>=60 && avg<=69)
{lettergrade='D';}
if(avg>=0 && avg<=59)
{lettergrade='F';}

return lettergrade;
}
FIRST, put code in [ code] .... [ /code] blocks (without the spaces after the "["). This makes it readable for us!

The first issue is the namespace std. This is a case of a compiler not giving a very helpful error. I see the same thing on gcc, which is what I'm assuming you're using too. It's not because the function is declared void type, it's because it doesn't know what ifstream and string are.

When you use something like ifstream or string, you either have to include:
using namespace std;

... Or you have to use std::ifstream, or std::string everytime.

That gets rid of most of the errors.

That leaves one error. I've got to run out really quick. If no one else responds, I'll come back to this, but I bet someone will beat me to it.
Last edited on
I moved "using namespace std;" from it's place in the above code to right underneath the preprocessor directives. Now, I'm getting the following:

[Linker error] undefined reference to `calcavg(std::basic_ifstream<char, std::char_traits<char> >&, std::string&, float&, float&, float&, float&, float&, float&)'

and

ld returned 1 exit status

------------------
This is where "using namespace std" is located now:

//////////////////////////////////////////////////
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;

void calcavg(ifstream&indat,string&name,float&t1,float&t2,float&t3,float&t4,float&t5,float&avg);
char calcgrade(float avg);
///////////////////////////////////////////////////
Also, the compiler I'm using is Dev C++.
Ahh, I see that your original example did have the line "using namespace std;", just below where I was expecting it. Being in a hurry earlier, missed that it was there since it wasn't after the defines. The namespace won't be used without being specified before that line exists, so moving it up as you have is the correct way to go.

As for the new error... When you see a linker error of an undefined reference, that's when it knows there needs to be code linked in that it hasn't found. In this case, the program forward declares a prototype of calcavg(), but there's no actual calcavg function ever written.

I assume that calcavg was a function you were going to write. Either it needs to be added to this file, or if it's in a different file, it needs to be compiled and linked as well.
So, basically, I need to define calcavg..?
I defined calcavg:

//Definition of calcavg:
void calcavg(ifstream&indat,string&name,float&t1,float&t2,float&t3,float&t4,float&t5,float&avg)
{}


Dev C++ finally compiled the program, and only the cursor was display on the console window (which is fine, because I want my output in a file; not in the console window); however, when i opened my output file, I wound up with pages and pages of garbage text that made no sense. What am I doing wrong?
I can't tell exactly what you're trying to do, but I'll throw out a few comments that I have. These definitely won't fix all the problems in the code.

Perhaps you abbreviated in your last post. Putting braces "{ }" after what was the function prototype gets it to compile, but there's no code there to have it do anything.

Another issue is that where you define the floats all in one line, you're only initializing total to 0. The reason why you're seeing pages of garbage text is because t1-t5 and average are never initialized to 0, and aren't set by calcavg assuming your program does have the empty braces "{ }" without text in the body. This means that these floats will take whatever value is randomly in their memory spot when the program runs, which is garbage. Since they're never changed, that's what it's writing out.

I'm not sure what drive E is on your system, but you should have "E:\class..." instead of just "E:class..." (Note the missing "\")

Another thing to consider is that avg is a float. In calcgrade(), think through what happens when it's called when avg is 89.5.

These comments probably don't fix everything in there, but hopefully will get you pointed in the right direction.

I talked to my instructor and the problem was that there was not any code in the body of my calcavg function. Thank you for helping me. I appreciate it!
Topic archived. No new replies allowed.