Sunday, July 12, 2009

C++: need to count consonants and vowels in a string, one word works but rest don't.... whats wrong?

//counting consonants and vowels


#include %26lt;iostream%26gt;


#include %26lt;cctype%26gt;





using namespace std;





int countcons (char *, char *);


int countvows (char *, char *);








int main()


{











const int size = 101;


char vowels[] = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U', '\0'};


char userstring[size];








cout %26lt;%26lt; "This program will count the number of consonants and vowels in your string, up to 100 characters. " %26lt;%26lt; endl %26lt;%26lt; endl;





cout %26lt;%26lt; "Enter your string: " %26lt;%26lt; endl;


cin.getline(userstring, size);











char choice;





cout %26lt;%26lt; "Would you like to: " %26lt;%26lt; endl


%26lt;%26lt; "A) Count the number of vowels in the string? " %26lt;%26lt; endl


%26lt;%26lt; "B) Count the number of consonants in the string? " %26lt;%26lt; endl


%26lt;%26lt; "C) Count both the vowels and consonants in the string? " %26lt;%26lt; endl


%26lt;%26lt; "D) Enter another string? " %26lt;%26lt; endl


%26lt;%26lt; "E) Exit the program? " %26lt;%26lt; endl %26lt;%26lt; endl;


cin %26gt;%26gt; choice;


switch (choice)


{

C++: need to count consonants and vowels in a string, one word works but rest don't.... whats wrong?
When you enter "D" to enter in another string, you get the string from the user, but don't go to the switch statement again.





Once the user enters in another string, you'll need to bring up the menu to "count the # of vowels..." etc.





Encapsulate the menu in it's own method, encapsulate the switch in it's own statement. Upon entering of 'D', you'd want to enter a string, then display the menu again.
Reply:huh? if your talking about it allows you to enter one word and do the calculations then when you press enter to enter another word the program quits.








if thats the case you should probably put it in a while loop
Reply:your doing things the hard way....





this is much easier (didn't have time to test this just wrote it right now)





#include %26lt;iostream%26gt;


#include %26lt;cctype%26gt;





using namespace std;





void inspectString();


int vowels,consonants;


const int size = 101;


char userstring[size];





int main()


{

















bool ok;








cout %26lt;%26lt; "This program will count the number of consonants and vowels in your string, up to 100 characters. " %26lt;%26lt; endl %26lt;%26lt; endl;





cout %26lt;%26lt; "Enter your string: ";


cin.getline(userstring, size);











char c;





while (ok){








cout %26lt;%26lt; "Would you like to: " %26lt;%26lt; endl;


cout %26lt;%26lt; "A) Count the number of vowels in the string? " %26lt;%26lt; endl;


cout %26lt;%26lt; "B) Count the number of consonants in the string? " %26lt;%26lt; endl;


cout %26lt;%26lt; "C) Count both the vowels and consonants in the string? " %26lt;%26lt; endl;


cout %26lt;%26lt; "D) Enter another string? " %26lt;%26lt; endl;


cout %26lt;%26lt; "E) Exit the program? " %26lt;%26lt; endl %26lt;%26lt; endl;


cout %26lt;%26lt; "Choice : ";


cin.get(c);


cin.ignore();





//make any lowercase letters into uppercase


if (c %26gt;= 'a' %26amp;%26amp; c %26lt;= 'z'){


c -= 'a' - 'A';


}








inspectString();





if (c=='A')


{





cout%26lt;%26lt;"Number of vowels: "%26lt;%26lt;vowels%26lt;%26lt;endl%26lt;%26lt;endl;





}


else if(c=='B')


{





cout%26lt;%26lt;"Number of consonants: "%26lt;%26lt;consonants%26lt;%26lt;endl;





}


else if(c=='C')


{





cout%26lt;%26lt;"Number of consonants and vowels: "%26lt;%26lt;consonants+vowels%26lt;%26lt;endl%26lt;%26lt;endl;





}


else if (c=='D')


{








cout %26lt;%26lt; "\n\nEnter your string: ";


cin.getline(userstring, size);








}


else if (c=='E')


{





ok=false;





}




















}











}











void inspectString()


{





vowels=0;


consonants=0;








for(int i=0;i%26lt;strlen(userstring);i++)


{








//convert all letters to uppercase


if (userstring[i] %26gt;= 'a' %26amp;%26amp; userstring[i] %26lt;= 'z')


userstring[i] -= 'a' - 'A';








if(userstring[i] %26gt;= 'A' %26amp;%26amp; userstring[i] %26lt;= 'Z')


{








if(userstring[i] == 'A' || userstring[i] == 'E' || userstring[i] == 'I' || userstring[i] == 'O' || userstring[i] == 'U')


{





vowels++;








}


else


{








consonants++;





}














}














}











}
Reply:I coded it from scratch...





#include %26lt;stdlib.h%26gt;


#include %26lt;stdio.h%26gt;


#include %26lt;conio.h%26gt;


#include %26lt;string.h%26gt;


#include %26lt;ctype.h%26gt;





typedef struct {


int cons;


int vowels;


}WordData;





void meh (WordData * wd,char *s)


{


int i=0;


wd-%26gt;cons=0;


wd-%26gt;vowels=0;


for(i=0;s[i];i++)


{


if(tolower(s[i]) %26gt;= 'a' %26amp;%26amp; tolower(s[i]) %26lt;= 'z')


switch (tolower(s[i]))


{


case 'a':


case 'e':


case 'i':


case 'o':


case 'u': wd-%26gt;vowels++;break;


default : wd-%26gt;cons++;


}


}


}





int main(void)


{


char s[]= "Hello World";


WordData * wd;


wd = malloc(sizeof(WordData));


meh(wd,s);


printf("Contains %d Vowels, and %d consonants",wd-%26gt;vowels,wd-%26gt;cons);


getch();


}





any questions email me phreak0matic@yahoo.com

online survey

No comments:

Post a Comment