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
|
Hi Zap, thanks for your suggestions. It now does look for the actual letter now. However, if I move the cout statements, I get an immediate error of "Subscript out of range" after the input is accepted.
#include <iostream>
#include <string>
using namespace std;
//Declare function for counting the characters in the string
void countString(int& acount, int& ecount, int& icount, int& ocount, int& ucount);
int main()
{
//Declare variables
int r, s, t, u, v, y;
string vowels;
int acount, ecount, icount, ocount, ucount;
//Prompt for user input
cout << "Enter a string: ";
//Call to countString function
countString(r, s, t, u, v);
//Display results to the user
cout << vowels.at(y) <<endl;
cout << "The number of a's: " <<acount << endl;
cout << "the number of e's: " <<ecount << endl;
cout << "The number of i's: " <<icount << endl;
cout << "The number of o's: " <<ocount << endl;
cout << "The number of u's: " <<ucount << endl;
//System pause
system ("pause");
//End Program
return 0;
}
void countString(int& acount, int& ecount, int& icount, int& ocount, int& ucount)
{
//Declare string variable
string vowels;
acount = 0;
ecount = 0;
icount = 0;
ocount = 0;
ucount = 0;
//Accept user input
cin >> vowels;
//Declare variable
int length;
//Initialize variable length
length = vowels.length();
//Keep count of letters in string
for(int y = 0; y <= length; y++)
{
//Decision if letter is a or A
if ((vowels[y] == 'a') || (vowels[y] == 'A'))
acount += 1;
//Decision if letter is e or E
else if ((vowels[y] == 'e') || (vowels[y] == 'E'))
ecount += 1;
//Decision if letter is i or I
else if ((vowels[y] == 'i') || (vowels[y] == 'I'))
icount += 1;
//Decision if letter is o or O
else if ((vowels[y] == 'o') || (vowels[y] == 'O'))
ocount += 1;
//Decision if letter is u or U
if ((vowels[y] == 'u') || (vowels[y] == 'U'))
ucount += 1;
}
}
|