Okay.. I had to swap some variable declarations to get this, but I think I've got 90% of it. I'm going to try to explain it the way I see it. Tell me if this is correct using the reworded code below:
01) Main function attempts to assign value to the string (X).
02) Main function goes to function input(); to find it.
03) User inputs value for Y.
04) function input() returns value Y and assigns it to X in main.
05) Main function attempts to assign value to the integer vCount.
06) Main function goes to function countVowels() to find it, and since X is in the parenthesis, main sens the value X to be acquired by the countVowels() function (X being what Y was, Y being whatever I inputted for the word).
07) In function countVowels, X is assigned to the string (Z). Z now equaling my inputted word.
08) The counter algorithm goes through the boolean true/false system and adds the numbers of Vowels in the word to the NewCount integer.
09) When NewCount is complete (i being equal to the word's length), function countVowels sends the NewCount back to main as the new vCount. (vCount is now identified in Main).
10) Program outputs specified message.
I think I get all of that now!
If I am correct on all of the above, then the only thing I still don't understand is why the global declaration of int countVowels() requires (string) in it.
:D :D :D
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
|
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
string input();
int countVowels(string);
// Main Function
int main(){
string X = input() ;
int vCount = countVowels(X);
cout << "There are " << vCount << " vowels in " << X << " which has " << X.length() << " letters." << endl;
return 0;
}
// Input Function
string input(){
string Y;
cout << "Please type word: " << endl;
cin >> Y;
cout << endl;
return Y;
}
// Count Vowel Function
int countVowels(string Z){
int NewCount = 0;
int i = 0;
while (i < Z.length()){
char Let = Z [i];
bool isVowel (Let == 'a' || Let == 'e' || Let == 'i' || Let == 'o' || Let == 'u' ||
Let == 'A' || Let == 'E' || Let == 'I' || Let == 'O' || Let == 'U');
if ( isVowel )
++NewCount;
++i;}
return NewCount;
}
|