print functions messing with my totals?

I have this program properly counting the uppercase, lowercase, and digits in a text file. However, when I add a function to print the file, the function to calculate the totals stops working. It says all the totals are zero. Any idea how I can keep the print function and still get my proper totals?

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
// Unit 10 final.cpp : Defines the entry point for the console application.
//

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

fstream file;

char ch;
int upper = 0;
int lower = 0;
int digit = 0;

void openFile();
void printFile(fstream &file, char ch);
int getTotals(fstream&);

int main()
{
	// Declare variables

	openFile();

	cout << "Loading file...\n";
	cout << "----------\n";
	system("pause");
	cout << "\n";

	printFile(file, ch);

	cout << "\n";

	upper, lower, digit = getTotals(file);

	cout << upper;

    return 0;
}

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

void printFile(fstream &file, char ch)
{
	while (file)
	{
		file.get(ch);
		cout << ch;
	}
}

int getTotals(fstream &file)
{

	while (file)
	{
		file.get(ch);

		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;
}



upper, lower, digit = getTotals(file);

You can't return several values from a function that way. Actually, the compiler warn you about that ("warning: left operand of comma operator has no effect"). Anyway, since your variables are global, you didn't need to return any value.

Is that an assignment or are you studying C++ by your own?

Hello stormbot,

In addition to what Enoizat said.

I would stay away from global variables if at all possible unless they start with "const". Any where in the program can change these variables and you may have a hard time figuring out where this is being done. Better to define these variables in main and pass them to the functions that need them.

The "openFile" function would work better if you check to make sure the file is open. If it is not there is no point in trying to use a stream that can not read the file. This may seem a little much, but I found it useful when first learning files. Notice what I did with the file name in main.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// in main
std::string iFileName{"text.txt"};

openFile(iFileName);

// in function "openFile"
void openFile(std::string iFileName)  // <--- Also change the prototype.
{
	file.open(iFileName, ios::in);

	if (file.is_open())
	{
		std::cout << "\n File " << iFileName << " is open" << std::endl;
		std::this_thread::sleep_for(std::chrono::seconds(2));  // <--- Needs header files chrono" and "thread".
	}
	else
	{
		std::cout << "\n File " << iFileName << " did not open" << std::endl;
		std::this_thread::sleep_for(std::chrono::seconds(3));  // <--- Needs header files chrono" and "thread".
		exit(1);
	}
}


In the "getTotals" function in the if/elst if statements the use of "and" is wrong. What you use here for a logical and is &&. As is nothing is being added to your totals.

And the return value of the function and return statement are not needed.

Hope that helps,

Andy
Hello stormbot,

I was interupted and hit "submit" before I was ready.

A sample if the input file or the file if small would be very helpful.

Andy
closed account (E0p9LyTq)
If you want a function's return value to be assigned to multiple variables of the same data type:

upper = lower = digit = getTotals(file);
Topic archived. No new replies allowed.