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?
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?
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.
// 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.