Issue when modularizing simple program

I had to create a program that reads from a text file, and then counts the number of uppercase, lowercase, and digits. I made it perfectly working without modules, but I have to use modules. When I run the modularized program, the totals are all zero. I don't think I forgot any code, but I can't seem to find the problem.

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Unit 10 modularized.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include<string>
#include<fstream>
using namespace std;

// Declare functions
void openFile();
void printFile(fstream &);
int countChars(fstream &, char ch, int upper, int lower, int digit);
void printCount(fstream &, char ch, int upper, int lower, int digit);

// Declare file stream object
fstream file;

// Declare global variables
char ch;
int upper;
int lower;
int digit;


int main()
{
	openFile();
	printFile(file);
	countChars(file, ch, upper, lower, digit);

	// print spaces for clean look
	cout << "\n";
	cout << "\n";

	// system pause and screen clear
	system("pause");
	system("CLS");

	printCount(file, ch, upper, lower, digit);

    return 0;
}

void openFile()
{
	file.open("text.txt", ios::in); // opens file
}

void printFile(fstream &file)
{
	if (file) // makes sure file was opened successfully
		while (file)
		{
			file.get(ch);
			cout << ch;
		}
	else
		cout << "Error handling file!\n"; // file error check
}

int countChars(fstream &file, char ch, int upper, int lower, int digit)
{
	if (file)
		while (file)
		{
			if (ch >= 'A' and ch <= 'Z')
			{
				upper += 1;
			}
			else if (ch >= 'a' and ch <= 'z')
			{
				lower += 1;
			}
			else if (ch >= '0' and ch <= '9')
			{
				digit += 1;
			}
		}
	return upper, lower, digit;
}

void printCount(fstream &file, char ch, int upper, int lower, int digit)
{
	cout << "The total number of uppercase letters: " << upper << endl;
	cout << "The total number of lowercase letters: " << lower << endl;
	cout << "The total number of digits: " << digit << endl;
}





look in the while (file) look at line 65... I don't think it's assigning anything to ch.

Not like it did with file.get(ch); before when you read the file to print it. You either gotta read it again, or count it while you're printing it (probably the latter).

Change the character count module to not interact with the file at all - just to look at ch and decide which global to increase. call it before printing each character while you're in the loop that prints those characters.

You don't need if (file) in front of while (file). The while will do the job of both.

You're reading until the end of the file in printFile (and printing one extra character, actually, since you're not checking for eof properly). Once you've spun the file to the end, countChars has nothing to count.

And as has been pointed out, you don't read anything in countChars anyway.

To check for eof properly you should test the input function directly, or if you can't do that then check for the eof condition right after the input function:
1
2
3
4
void printFile(fstream& file) {
    while (file.get(ch))
        cout << ch;
}

And in countChars, reset the file to the beginning first:
 
file.seekg(0); // rewind file 

Last edited on
do you have any resources for learning about eof? I am unfamiliar with it and I doubt you want to explain it!
closed account (E0p9LyTq)
do you have any resources for learning about eof?

There is a good reference section here at cplusplus:

http://www.cplusplus.com/reference/ios/basic_ios/eof/

or at cppreference:

http://en.cppreference.com/w/cpp/io/basic_ios/eof

If you see something you don't understand those are two excellent places to start.
You also need to pass upper, lower and digit by reference instead of by value.

Printcount() doesn't use parameter ch. So don't pass it.
To pass by reference you just lead it with a & right?
closed account (E0p9LyTq)
Yes.

void aFunction (int& a, int& b, int& c)

If you are not going to modify a parameter make it a const reference.
void aFunction (const int& a, const int& b, int& c)
a or b can't be modified in the function, c can.
Last edited on
Topic archived. No new replies allowed.