Problem with my void adding a counter.

Hello guys, I'm currently attempting to create a program to translate a text file using input and output streams. I am trying to add a counter into my program to count the number of dashes/letters. I am working on the letter one now but cannot figure out how to add an int to my void. It keeps telling me that count is undefined when I call it. Here is my void function.
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
void transformation(ifstream& in_stream, ofstream& out_stream, int& count)
{
	count = 0;
	int digits = (int)count;
	char next;

	//Changes insanity to all uppercase.
    for (int ctr = 0; ctr < 8; ctr++){
		in_stream.get(next);
		next = toupper(next);
		out_stream.put(next);
	}

	//changes spaces to dashes.
	while(! in_stream.eof()){
		in_stream.get(next);
		if (next == ' ')
			out_stream << "-";
		else if (isdigit(next))
			digits++;
		else
			out_stream << next;
	}

	//outputs the counters.
	cout << "Number of digits: " << digits;
	cout << "Number of dashes: ";
}


This is how I called it in my main.
 
transformation(fin, fout, count);


This is how I have it outside my main in the header.
 
void transformation(ifstream& in_stream, ofstream& out_stream, int& count);


Any help would be greatly appreciated
Last edited on
Have you declared a variable called count in your main function?
I have not, but i thought since i declare a variable in the void, then i don't need to redeclare it.
Sorry, but you do. Within the scope of the transformation function (*), the input parameter is declared with the name count, but only within that scope. You still need to declare a variable in the main function that gets passed into transformation.

(*) it's called a "function" by the way. The word void simply indicates that this particular function doesn't return any value.
I still cannot get it to work as it is counting incorrectly for letters.
Also, when i add the dashes counter into my function, the else's get highlighted and say they are expecting a statement.

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
void transformation(ifstream& in_stream, ofstream& out_stream,int count)
{
	int digits = (int)count;
	int dashes = (int)count;
	char next;
	//Changes insanity to all uppercase.
    for (int ctr = 0; ctr < 8; ctr++){
		in_stream.get(next);
		next = toupper(next);
		out_stream.put(next);
	}

	//start the while loop.
	while(! in_stream.eof()){
		in_stream.get(next);
		
		//line to change all letters to lowercase.
		next = (tolower(next));

		//changes spaces to dashes.
		if (next == ' ')
			out_stream << "-";
			dashes++;
		else if (isdigit(next))
			digits++;
		else
			out_stream << next;
	}
If you want multiple statements in an if block, you need to enclose them in braces. If you don't, then only the first line is considered to be part of the block. Line 23 is considered to be after the end of the if block, which means on line 24, the else if statement doesn't follow on from an if statement.
Thank you. got it to work!
Glad to hear it :) You're welcome!

I'd actually recommend you put braces around all your if, else, and for blocks, even if they're only one line. This sort of mistake is easy to make, and putting the braces in will help you avoid making it. I also happen to think it makes the code more readable, but that's a matter of personal opinion.
I might change that around. I have found an error with my counters. They are not counting correctly for some reason?
Topic archived. No new replies allowed.