Function display issues

Hello, I having some trouble with my countCharacters() function. when i run the program everything works as intended but when I run the function twice as seen below, it adds both functions together when I want it to display each total separately.
The countCharacters() function is suppose to open the txt file and count the amount of characters within.
for ex, the groceries.txt has 344 characters, and names.txt has 75. when i run the program the first output is,
# of characters in the file : 344
when the program hits the second function its outputs,
# of characters in the file : 419
when it should be 75.

any knowledge would be greatly appreciated.
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
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <fstream>
#include <string>

using namespace std;
void countCharacters(string, int&);


int main()
{
  int char_count { };

  countCharacters("groceries.txt", char_count);
cout  << "# of characters in the file : " << char_count << endl;
  countCharacters("names.txt", char_count);
 cout  << "# of characters in the file : " << char_count << endl;

  }

void countCharacters(string file, int& ch_cnt)
{
ifstream  fin(file);
  if (fin)  
  {
    while (fin)
    {
      char ch { };
      fin.get(ch);

      if ((ch > '@' && ch < '[') || (ch > '`' && ch < '{')){
        ch_cnt++;
      } else if (ch == ' '){
        ch_cnt++;
         }
    }

  }
  fin.close();
}
Last edited on
Hello liam401,

When the program starts what is the value of "char_count" at line 13 then line 14?

When you call the function the second time what is the value of "char_count" at line 15 and line 16?

Then when the function is called each time what is the value of "ch_cnt" at the beginning and end of the function?

Andy
Hello liam401,

Started to test the program, but realized I do not have the same input files you do.

It would really help if you would include them in your post.

Andy
Hello Handy Andy,
thanks for the response.
Below are the two txt files.

groceries.txt
Bread, Cereal, Rice and Pasta: Bread
Vegetables: Carrots
Fruits: Orange juice
Milk, Yogurt and Cheese: Milk
Meat, Poultry, Fish, Dry Beans, Eggs and Nuts: Eggs
Fats, Oils and Sweets: Olive oil
Staples, Condiments and Miscellaneous Foods (spices, baking powder, etc.): Mustard
Health and Beauty Products: Toothpaste
Household Items (laundry soap, light bulbs, etc.): Coffee filters

names.txt
Charlie
Finley
Skyler
Justice
Royal
Lennon
Oakley
Armani
Azariah
Landry
Frankie
Sidney
Hello liam401,

In main you have these lines of code.
1
2
3
4
5
6
7
8
9
10
int main()
{
    int char_count{};

    countCharacters("groceries.txt", char_count);
    cout << "# of characters in the file : " << char_count << endl;
    countCharacters("names.txt", char_count);
    cout << "# of characters in the file : " << char_count << endl;

}

Lines 5 - 8 are not really helping the compiler that much to make any difference. And you should stop writing the code for the compiler and start writing code that you and others can read.

1
2
3
4
5
6
7
8
9
10
11
int main()
{
    int char_count{};

    countCharacters("groceries.txt", char_count);
    cout << "# of characters in the file : " << char_count << endl;

    countCharacters("names.txt", char_count);
    cout << "# of characters in the file : " << char_count << endl;

}

Just 1 blank line makes the problem easy to see.

Your code works, but it could use some other changes.

Andy
it adds both functions together


ch_cnt needs to be set to 0 at the beginning of the function as it's passed by ref - so has the initial value in the function the same as the previous value.

Also, the count function can be much simplified. isalpha() returns true(1) if the specified char is a letter (upper or lower case) or false (0) otherwise. So ch_cnt is only incremented by 1 if ch is either a letter or a space. Noe that is also isspace(). But this doesn't do quite what might be expected. It returns true if the specified cha is a white-space char (' ', '\t', '\n) and not just space char.

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
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>

using namespace std;
void countCharacters(const string&, int&);

int main()
{
	int char_count { };

	countCharacters("groceries.txt", char_count);
	cout << "# of characters in the file : " << char_count << endl;

	countCharacters("names.txt", char_count);
	cout << "# of characters in the file : " << char_count << endl;

}

void countCharacters(const string& file, int& ch_cnt)
{
	ch_cnt = 0;

	ifstream fin(file);

	for (char ch {}; fin.get(ch); )
		ch_cnt += (std::isalpha(ch) || ch == ' ');
}


But why has the result passed by ref parameter and not a return value?

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
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
using namespace std;

int countCharacters(const string&);

int main()
{
	cout << "# of characters in the file : " << countCharacters("groceries.txt") << endl;
	cout << "# of characters in the file : " << countCharacters("names.txt") << endl;
}

int countCharacters(const string& file)
{
	int ch_cnt {};

	ifstream fin(file);

	for (char ch {}; fin.get(ch); )
		ch_cnt += (std::isalpha(ch) || ch == ' ');

	return ch_cnt;
}

Topic archived. No new replies allowed.