I'm having trouble with passing a character array between functions of the same class. I have a function, buildGraph, that calls function getNextLine. The getNextLine essentially just retrieves the next line of an input file and stores it into a "char line[80]". However when I try to use "line" in my buildGraph function, it has nothing in it.
bool Prog3Graph::getNextLine(char line, int lineLen)
{
bool done = false;
while(!done)
{
inFile.getline(line, lineLen);
if(inFile.good())
{
if(strlen(line) == 0) // Skip any blank lines
continue;
elseif(line[0] == '#') // Skip any comment lines
continue;
else done = true; // Got a valid data line so return with this line
}
else
{
strcpy(line, "");
returnfalse; // Flag end of file with null string and return false
}
}
returntrue; // Flag successful read
}
Why are you trying to use C-strings instead of std::string?
Why is getNextLine() part of your class? It doesn't seem to be using anything in the class.
Also this function is either misnamed or the parameters are incorrect. Why are you sending a single character into this function that seems to be expecting a C-string?
Also this function is either misnamed or the parameters are incorrect. Why are you sending a single character into this function that seems to be expecting a C-string?
From what I understood, arrays are a memory address such as pointers. I have getNextLine() in the class because it's using the line and lineLen variables that are in the class.
Since line is a member variable, you do not need to pass anything between the functions.
Try changing it to:
1
2
private:
bool getNextLine(int lineLen);
and
bool Prog3Graph::getNextLine(int lineLen)
Thanks for the input.
I was going to try it out, but for some reason the file is now reading that it's bad in the getNextLine(). Since I haven't closed the file yet, I shouldn't have to open the file again for getNextLine() again should I?
EDIT:
I think because I ran the program before without closing it, the buffer went to crap. Your suggestion works, abhishekm71. Thanks a ton!
bool Prog3Graph::getNextLine(char line, int lineLen)
I have getNextLine() in the class because it's using the line and lineLen variables that are in the class.
No, when you pass parameters into the function that have the same names as your member variables the member variables are hidden and you use the parameters. And since this parameter is a single character, that is being passed by value, that's what you're working with, a char not a C-string. If you want to use the class member variables then you don't need to pass anything into the function.
From what I understood, arrays are a memory address such as pointers.
So where is this mysterious pointer you've mentioned. If you wanted to pass an array into the function you would need to use a pointer (or array notation).
1 2
bool Prog3Graph::getNextLine(char *line, int lineLen)
bool Prog3Graph::getNextLine(char line[], int lineLen)