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 95 96 97
|
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
// Declare functions
void openFile();
void printFile(fstream &);
int countChars(fstream &, int& upper, int& lower, int& digit);
void printCount(fstream &, 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, upper, lower, digit);
// print spaces for clean look
cout << "\n";
cout << "\n";
// system pause and screen clear
system("pause");
system("CLS");
printCount(file, upper, lower, digit);
return 0;
}
void openFile()
{
file.open("test.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, int& upper, int& lower, int& digit)
{
///your file eof is already set you need to reset your file stream before use
file.clear(); ///clear
file.seekg(0); ///reset read pos
if (file)
while (file >> ch)
{
///file.seekg(0); remove from here
///file.get(ch); consumes an extra character
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;
}
}
else
std::cout<<"\nbad stream\n"; ///check if your stream failed
return upper, lower, digit; ///this return is wrong, make this a void function your are passing you vars by ref.
}
void printCount(fstream &file, int upper, int lower, int digit) ///file is unused am sure you don't need it
{
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;
}
|