Hey, I'm having a little trouble with my program. I asked my friend, and the teacher's assistant and they both can't help. Can anyone see the problem with my program?
I've written the whole thing, but I get this damn Linker error. My friend said all the code looks legit, but... It still doesn't compile. I have the feeling it's something very simple.
Thanks.
Error:
1 2 3
[Linker error] undefined reference to `AVG(char)'
[Linker error] undefined reference to `max_min(char, int)'
ld returned 1 exit status
/*****************************
* Made by me *
* Assignment 3 *
* ece270 *
*****************************/
#include <iostream>
#include <fstream>
usingnamespace std;
int AVG(char);
int max_min(char,int);
int main()
{
char filename[50] = "a3.txt";
int count = AVG(filename[50]);
max_min(filename[50], count);
return 0;
}
int AVG(char filename[50])
{
int current_number = 0;
int total_ = 0;
int count = 0;
ifstream the_file;
the_file.open (filename);
while(the_file)
{
the_file >> current_number;
total_ += current_number;
count++;
}
cout << total_ << " is the total of all numbers in the file." << "\n";
cout << count << " is the number of number in the file." << "\n";
cout << (total_ / count) << " is the average of all number in the file." << "\n";
the_file.close();
return count;
}
int max_min(char filename[50], int count)
{
ifstream the_file;
the_file.open (filename);
int current_number;
int max, min;
for(int i = 1; i<count; i++)
{
the_file >> current_number;
if(current_number > max) max = current_number;
if(current_number < min) min = current_number;
}
cout << max << " is the highest number in this file." << "\n";
cout << min << " is the lowest number in this file." << "\n";
the_file.close();
return 0;
}
#include <iostream>
#include <fstream>
#include <string> // don't be afraid of string
usingnamespace std;
int AVG(string); // use strings
int max_min(string,int);
int main()
{
string filename = "a3.txt";
int count = AVG(filename); // better
max_min(filename, count); // better
return 0;
}
//...
int max_min(string filename,int count)
{
ifstream the_file;
the_file.open (filename.c_str()); // call .c_str() when you need to get a char array
If you can't use strings for whatever reason, then you can make the following changes:
1 2 3 4 5 6 7 8 9 10 11
// make sure your parameter types match
int AVG(char filename[50]); // <- char [50] , not char
int max_min(char filename[50],int); // <- same
int main()
{
char filename[50] = "a3.txt";
int count = AVG(filename); // don't put [50] here, pass the pointer, not just one char
max_min(filename, count); // same
@Disch: I'm a beginner to but shouldn't line 9 on your second code block (In case he cant use strings) read as:
int count = AVG(*filename);?
Otherwise it doesn't compile\work for me. Even then it only seems to pass one char at a time outside the function. If he can't use strings would a global char array be a better option so that every function can see it?
No, my code is correct. *filename is bad because it only passes the first character of the array, not a pointer to the whole array (which would in a sense pass the whole string).
if it wasn't compiling for you, make sure your function prototype / definition matched mine. Notice I'm never having char as a parameters, always a char array: char filename[50]. That makes a big difference.
If he can't use strings would a global char array be a better option so that every function can see it?
Absolutely not. Globals are almost never the better option.