Question: C++ Programming Hello I need help with validating the enter key to exit the program. When the user enters y or Y, the user can

C++ Programming

Hello I need help with validating the enter key to exit the program. When the user enters y or Y, the user can continue using the program however, if the user inputs anything else (including pressing the Enter Key) the program should close. My program is currently allowing special characters, numerical values and alphabetical characters but not the Enter Key. So in short, I need the program to accept the enter key along with the other characters to terminate the program. So once a user presses the enter key(or any other key besides y or Y), the program should end. Any help is greatly appreciated! In the code, it will be located at the end of main.

RESTRICTIONS:

No global variables, labels or go-to statements, infinite loops, break statements. String literals are allowed BUT not variables of type string.

MY CODE:

#include

#include

#include

#include

using namespace std;

//Function Prototypes

char getVowelAtIndex(int index, bool isUpperCase);

int getIndexOfFrequentVowel(int vowels[]);

char getVowelAtIndex(int index, bool isUpperCase) {

//based on bool, function will return correct upper or lower case

//used conditional expression

if (index == 0) {

return isUpperCase == true ? 'A' : 'a';

}

else if (index == 1) {

return isUpperCase == true ? 'E' : 'e';

}

else if (index == 2) {

return isUpperCase == true ? 'I' : 'i';

}

else if (index == 3) {

return isUpperCase == true ? 'O' : 'o';

}

else if (index == 4) {

return isUpperCase == true ? 'U' : 'u';

}

}

int getIndexOfFrequentVowel(int vowels[]) {

//started with null however if there are no vowels at index 0, null will be changed to the right index

int largest = NULL;

//check if index counter is > largest, and assign. If not, ignore

for (int index = largest; index < 5; index++) {

if (vowels[largest] < vowels[index]) {

largest = index;

}

}

//reset counter

int index = 0;

while (index < 5) {

//if the current running index = largest index, ignore.

if (index == largest) {

index++;

}

else if (vowels[index] == vowels[largest]) {

largest = 9;

index++;

}

else {

index++;

}

}

return largest;

}

int main()

{

char continu[1] = {'Y'};

do {

int lowerVowel[5] = {0};

int upperVowel[5] = {0};

cout << "Enter a string: ";

int inputCharCount = 0;

char c;

char *temp = nullptr;

char *UserInputCharArray = nullptr;

UserInputCharArray = new char[inputCharCount];

//Dynamic Memory Allocation

while ((cin.get(c)) && (c != ' ')) {

temp = new char[inputCharCount + 1];//make new pointer array dynamically

temp[inputCharCount] = c; //count holds count + 1, so count is assigned to c

//temp will hold ONLY the cin char at its last index

for (int i = 0; i < inputCharCount; i++) {

temp[i] = UserInputCharArray[i];

}

delete[] UserInputCharArray;//free memory with delete operator

UserInputCharArray = temp;

inputCharCount++;

}

//loop through input and count vowels while char is not the terminator

int vowelCounter = 0;

while (*(UserInputCharArray + vowelCounter) != '\0') {

if (*(UserInputCharArray + vowelCounter) == 'a') {

lowerVowel[0]++;

}

else if (*(UserInputCharArray + vowelCounter) == 'e') {

lowerVowel[1]++;

}

else if (*(UserInputCharArray + vowelCounter) == 'i') {

lowerVowel[2]++;

}

else if (*(UserInputCharArray + vowelCounter) == 'o') {

lowerVowel[3]++;

}

else if (*(UserInputCharArray + vowelCounter) == 'u') {

lowerVowel[4]++;

}

else if (*(UserInputCharArray + vowelCounter) == 'A') {

upperVowel[0]++;

}

else if (*(UserInputCharArray + vowelCounter) == 'E') {

upperVowel[1]++;

}

else if (*(UserInputCharArray + vowelCounter) == 'I') {

upperVowel[2]++;

}

else if (*(UserInputCharArray + vowelCounter) == 'O') {

upperVowel[3]++;

}

else if (*(UserInputCharArray + vowelCounter) == 'U') {

upperVowel[4]++;

}

vowelCounter++;

}

//Print vowels in string

cout << " The vowels present in the string are: ";

for (int i = 0; i < 5; i++) {

if (lowerVowel[i] != 0) {

cout << getVowelAtIndex(i, false) << " ";

}

if (upperVowel[i] != 0) {

cout << getVowelAtIndex(i, true) << " ";

}

} cout << endl;

//LOWER CASE

//if there are no vowels

if (lowerVowel[0] == 0 && lowerVowel[1] == 0 && lowerVowel[2] == 0 && lowerVowel[3] == 0 && lowerVowel[4] == 0) {

cout << " There are no lower case vowels" << endl;

}

//if there is no frequent vowel

else if (getIndexOfFrequentVowel(lowerVowel) == 9) { //9 is a signal that there is no highest number as there is no index associated with 9

cout << " There is no highest frequency lower case vowel" << endl;

}

//display frequent vowel and frequency

else {

cout << " The highest frequency lower case vowel is "

<< getVowelAtIndex(getIndexOfFrequentVowel(lowerVowel), false);

cout << " with a frequency of " << lowerVowel[getIndexOfFrequentVowel(lowerVowel)] << endl;

}

//UPPER CASE

//if there are no vowels

if (upperVowel[0] == 0 && upperVowel[1] == 0 && upperVowel[2] == 0 && upperVowel[3] == 0 && upperVowel[4] == 0) {

cout << "There are no upper case vowels" << endl;

}

//if there is no frequent vowel

else if (getIndexOfFrequentVowel(upperVowel) == 9) { //9 is a signal that there is no highest number as there is no index associated with 9

cout << "There is no highest frequency upper case vowel" << endl;

}

//display frequent vowel and frequency

else {

cout << "The highest frequency upper case vowel is "

<< getVowelAtIndex(getIndexOfFrequentVowel(upperVowel), true);

cout << " with a frequency of " << upperVowel[getIndexOfFrequentVowel(upperVowel)] << endl;

}

cout << endl;

system("pause");

cout << " Enter y/Y to continue, anything else to exit" << endl;

cin >> continu;

cin.ignore(100, ' ');

cout << endl;

} while (toupper(continu[0]) == 'Y');

exit(0);

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!