Passing char arrays

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.

Here's my code:
Class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#define NUMNODES 10

using namespace std;

#pragma once
class Prog3Graph
{
public:
	Prog3Graph();
	~Prog3Graph();
	bool buildGraph(char *fileName);
	void printGraph();
	void depthFirstTraversal();
	char filename[25];
private:
	bool getNextLine(char line, int lineLen);

	char line[80];
	int lineLen;
	ifstream  inFile;         // File stream to read from
	int AdjMatrix[NUMNODES][NUMNODES];
	GraphNode Nodes[NUMNODES];
};


buildGraph
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
bool Prog3Graph::buildGraph(char *fileName)
{
	ifstream inFile;
	//infile.open(filename, ifstream::in);
	inFile.open("C:\\Users\\Jonathan\\Desktop\\graph.txt", ifstream::in);

	if (!inFile.is_open())
	{
		cout << "Unable to open file " << filename << ". \nProgram terminating...\n";
		return false;
	}

	//Matrix
	getNextLine(line, 80);
	//inFile.getline(line,80);
	cout<<line;
	
	return true;
}


getNextLine
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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;
            else if(line[0] == '#')  // Skip any comment lines
                continue;
            else done = true;    // Got a valid data line so return with this line
        }
        else
        {
            strcpy(line, "");
            return false;    // Flag end of file with null string and return false
        }
    } 
    return true; // Flag successful read
}
Last edited on
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?


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)

Last edited on
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!
Last edited on
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)
Topic archived. No new replies allowed.