Reputation:
I am trying to create an Array which would let the user input up to 80 characters and then when the program halts, for the program to return the amount of times each vowel shows up in the Array. I successfully got it to work last week, but the coding got lost and I had to do it from scratch.
Edit: The problem I am having is that based on the current structure, all the counters still return 0 at the end.
I remember that I used the following coding, but I am having issues with the counters actually working. I think I had the segment for the counters in a loop of its own, but I can't remember it.
I also want the program to halt if the user presses ENTER, but I have no idea how that works.
Any help would be appreciated.
#include <iostream>
#include <iostream>
using namespace std;
int main()
{
int x = 0;
char thisArray[80];
int aCounter = 0, eCounter = 0, iCounter = 0, oCounter = 0, uCounter = 0;
cout << "Please enter up to 80 characters and you will be told" << endl;
cout << "how many times each vowel is in the array." << endl;
do{
cout << "Please enter a character" << endl;
cin >> thisArray;
x++;
} while (x < 80);
for (x = 0; x <= thisArray[x]; x++) {
if (thisArray[x] == 'a')
aCounter = aCounter + 1;
else if (thisArray[x] == 'e')
eCounter = eCounter + 1;
else if (thisArray[x] == 'i')
iCounter = iCounter + 1;
else if (thisArray[x] == 'o')
oCounter = oCounter + 1;
else if (thisArray[x] == 'u')
uCounter = uCounter + 1;
}
cout << "Vowel count:" << endl;
cout << "Total number of A's." << aCounter << endl;
cout << "Total number of E's." << eCounter << endl;
cout << "Total number of I's." << iCounter << endl;
cout << "Total number of O's." << oCounter << endl;
cout << "Total number of U's." << uCounter << endl;
system("pause");
}
Upvotes: 0
Views: 53
Reputation: 206747
The do
loop you are using is not correct. It will try to read a string 80 times, and will store only the last string. Each of those strings will contain only non-whitespace characters.
Change the do
loop so that you read one character at a time, using unformatted input, and store all the characters in the array, including whitespace characters.
Stop the loop when you reach the limit of the number of characters you can hold or the newline character is encountered.
cout << "Please enter a line of text" << endl;
// Read the characters one by one.
// Don't read more than 79 characters.
// Stop if a newline or EOF is encountered.
int c;
while ( x < 79 && (c = cin.get()) != '\n' && c != EOF )
{
thisArray[x] = c;
++x;
}
// null terminate the string.
thisArray[x] = '\0';
Upvotes: 1
Reputation: 181068
You have a couple issues in you code. First you are not inputting into the array in your do...while
loop. You need to use the index operator([]
) with the array name for that:
do{
cout << "Please enter a character" << endl;
cin >> thisArray[x];
x++; ^^^ input each character into the correct index.
} while (x < 80);
Secondly your for loop has the wrong condition.
for (x = 0; x <= thisArray[x]; x++)
Should be
for (x = 0; x < 80; x++)
Notice that I changed the <=
to <
as an array of size 80 has the elements of [0, 79]
Upvotes: 0