I am working on an assignment in C++ meant to teach us more about objects and OOP. Below is my code. The gist of it is, the user enters some input, and the program counts the number of vowels or consonants (chosen by the user), the total number of characters entered, and the total number of end-of-lines.
I am having three issues:
- The portion of code I've commented out is causing an infinite loop when left in. It causes the output from the
countChars
function to be printed infinitely, as well as the output asking the user if they would like to put in more input. - The
countChars
function isn't counting EOL properly. I think it is most likely due to my unfamiliarity with the EOL. How do I signify it in my conditional statement? If I want to say, "increment if it has a value of '0'", I say,if (variable == 0)
. How do I tell C++ to increment if something is an EOL? countChars
outputs random, negative values for the counts. I do notice the values changing depending on what I type (except EOL), but I am not sure why I am getting negative values. I'm not sure how to fix it beyond using anunsigned int
and initializing the values.
Also, I foresee people telling me to use the getline function, but we had very specific instructions to use cin.get
(we're supposed to learn a bit of everything, after all) so please, avoid a fix that uses getline
.
Header file:
/*
+----------------------------------------+
| CountChars |
+----------------------------------------+
| -countVorC : Integer |
| -countEOL : Integer |
| -totalChars : Integer |
| -vowelCount : Boolean |
+----------------------------------------+
| <<constructor>> |
| CountChars() |
| +inputChars() : |
| +vowelCheck(characterToCheck : Boolean)|
| +setVowelCount(VorC : Character) |
| +getCountVorC() : Integer |
| +getCountEOL() : Integer |
| +getTotalChars() : Integer |
| +getVowelCount() : Boolean |
+----------------------------------------+
*/
using namespace std;
#ifndef COUNTCHARS_H
#define COUNTCHARS_H
class CountChars
{
private:
unsigned int countVorC;
unsigned int countEOL;
unsigned int totalChars;
bool vowelCount;
public:
CountChars();
void inputChars();
bool vowelCheck(char characterToCheck);
void setVowelCount(char VorC);
int getCountVorC();
int getCountEOL();
int getTotalChars();
bool getVowelCount();
};
#endif
Implementation file:
#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
#include <cstdio>
#include "CountChars.h"
using namespace std;
CountChars::CountChars()
{
unsigned int countVorC = 0;
unsigned int countEOL = 0;
unsigned int totalChars = 0;
bool vowelCount = false;
}
void CountChars::inputChars()
{
int letter;
while ((letter = cin.get()) != EOF && letter != EOF){
if (vowelCount == true && (vowelCheck(letter) == true)) {
countVorC++;
}
else if (vowelCount == false && (vowelCheck(letter) == false)) {
countVorC++;
}
if (isalpha(letter)) {
totalChars++;
}
if (letter == '\n') {
countEOL++;
}
}
}
bool CountChars::vowelCheck(char characterToCheck)
{
characterToCheck = toupper(characterToCheck);
if ((isalpha(characterToCheck)) &&
(characterToCheck == 'A' || characterToCheck == 'E' ||
characterToCheck == 'I' || characterToCheck == 'O' ||
characterToCheck == 'U')) {
return true;
}
else {
return false;
}
}
void CountChars::setVowelCount(char VorC)
{
VorC = toupper(VorC);
if (VorC == 'V') {
vowelCount = true;
}
else {
vowelCount = false;
}
}
int CountChars::getCountVorC()
{
return countVorC;
}
int CountChars::getCountEOL()
{
return countEOL;
}
int CountChars::getTotalChars()
{
return totalChars;
}
bool CountChars::getVowelCount()
{
return vowelCount;
}
Main:
#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
#include <cstdio>
#include "CountChars.h"
using namespace std;
void printCounts(CountChars);
int main()
{
char VorC;
char repeat = 'Y';
CountChars charCounter;
cout << "Welcome to the Character Counter Program!" << endl;
cout << "\nWould you want to count vowels or consonants?" << endl;
cout << "Type 'V' for vowels and 'C' for consonants: ";
cin >> VorC;
cout << endl;
while (toupper(VorC) != 'V' && toupper(VorC) != 'C') {
cout << "\nSorry, that was an invalid choice. Please try again: ";
cin >> VorC;
cout << endl;
}
do {
cout << "You may being typing input below.\n" << endl;
charCounter.setVowelCount(VorC);
charCounter.inputChars();
cin.clear();
printCounts(charCounter);
cout << "\nWould you like to enter new input?" << endl;
cout << "Type 'Y' for yes or 'N' for no: ";
cin >> repeat;
cout << endl;
while (toupper(repeat) != 'Y' && toupper(repeat) != 'N') {
cout << "\nSorry, that was an invalid choice. Please try again: ";
cin >> repeat;
cout << endl;
}
} while (toupper(repeat) == 'Y');
cout << "\nThank you for using the Character Counter Program!\n" << endl;
system("pause");
return 0;
}
void printCounts(CountChars charCounter)
{
cout << "\nTotal characters: " << charCounter.getTotalChars() << endl;
if (charCounter.getVowelCount() == true) {
cout << "Total vowels: " << charCounter.getCountVorC() << endl;
}
else {
cout << "Total consonants: " << charCounter.getCountVorC() << endl;
}
cout << "Total end-of-lines: " << charCounter.getCountEOL() << endl;
}