the purpose of this program is to read a text file and count the total characters, the digits, puncuation, white spaces, etc, and then display the final count. right now i am trying to figure out what to put inside the while loop. it's not executing the current way i have it, and i've tried several other methods. i figure it has to be something along the lines of total number of characters (totalChar) is equal to all of the other characters added up. does anyone have any advice on what the condition inside the loop should be? (also i'm aware the displaying part at the end is not good, i'll fix that when i fix the loop)
Code:#include <iomanip> #include <iostream> #include <fstream> #include <ctype.h> using namespace std; /*************************************************************** Function: myIsAlpha Use: Tests if a character is alphabetic Arguments: It takes a single character as its argument Returns: Returns true if the character is alphabetic and false if the character is not. ***************************************************************/ bool myIsAlpha(char ch) { if((ch > 64 and ch < 91) or (ch > 96 and ch < 123)) return true; else return false; } /*************************************************************** Function: myIsDigit Use: Tests if a character is a digit Arguments: It takes a single character as its argument Returns: Returns true if the character is a digit and false if the character is not. ***************************************************************/ bool myIsDigit( char ch ) { if (ch > 47 and ch < 58) return true; else return false; } /*************************************************************** Function: myIsUpper Use: Tests if a character is uppercase alphabetic character Arguments: It takes a single character as its argument Returns: Returns true if the character is an uppercase alphabetic character and false if the character is not. ***************************************************************/ bool myIsUpper( char ch ) { if (ch < 64 and ch > 91) return true; else return false; } bool myIsAlpha( char ch ); bool myIsDigit( char ch ); bool myIsUpper( char ch ); int countPunct = 0, //declaring int variables to be used. countAlpha = 0, countUp = 0, countLow = 0, countChar = 0, countDig = 0, countSpace = 0, totalChar = countPunct+countAlpha+countUp+countLow+countChar+countDig+countSpace, x=0; char ch; //declaring ch as char int main() { ifstream inputFile; //opening the input file and if it fails to open, displaying and error message to the user and exiting the program inputFile.open( "input.txt" ); if( inputFile.fail() ) { cout << "input.txt failed to open"; exit( -1 ); } while (totalChar==countPunct+countAlpha+countUp+countLow+countChar+countDig+countSpace) { if(ispunct(ch)) //setting if statement so that if the ch character is a punctuation character, it will increment countPunct by 1. { countPunct++; } if(myIsAlpha(ch)) //setting if statement so that if the ch character is an alphabetic character, it will increment countAlpha by 1. { countAlpha++; } if (myIsDigit(ch)) //setting if statement so that if the ch character is a digit character, it will increment countDig by 1. { countDig++; } if (myIsUpper(ch)) //setting if statement so that if the ch character is an uppercase alphabetic character, it will increment countUp by 1. { countUp++; } if (islower(ch)) //setting if statement so that if the ch character is a lowercase alphabetic character, it will increment countLow by 1. { countLow++; } if (isspace(ch)) //setting if statement so that if the ch character is a white space character, it will increment countSpace by 1. { countSpace++; } totalChar = countPunct+countAlpha+countUp+countLow+countChar+countDig+countSpace; } cout<<"\n"<<countPunct<<"\n"<<countAlpha<<"\n"<<countDig<<"\n"<<countUp; //displaying results of the various character counts //Closing the input file inputFile.close(); return 0; }



LinkBack URL
About LinkBacks


