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