Thursday, July 30, 2009

C++... made a program that counts consonants and vowels by checking for vowels. i have the vowels in array...?

I'm make a class program that counts consonants and vowels in a user inputed c-string by checking for vowels. i have a, e, i, o, and u in the array "vowels". But there are 5 errors, each letter saying they're not identified.





What's wrong?

C++... made a program that counts consonants and vowels by checking for vowels. i have the vowels in array...?
Put the characters in single quotation marks when you declare the array.





char vowels[] = {'a', 'e', 'i', 'o', 'u'};





Also, when you go to compile, you will get an error dealing with the functions. You declare them as:





int countcons (char *, char[]);


int countvows (char *, char[]);





and the definitions start as:


int countcons(char *strptr, char vowels)


int countvows(char *strptr, char vowels)





Do you see the difference? You need to make the definitions the same as the declarations. The error you are getting means the declarations are telling the program the functions pass two strings, but it isn't finding the definition of a function with that name that takes to c strings (it is finding a function that takes a c string and a single char). Am I making sense? so try making the definitions something like:


int countcons(char *strptr, char *vowels){...code...}


int countvows(char *strptr, char *vowels){...code...}





-------------------


you could make them


int countcons(char *, char *);





int countcons(char *strptr, char *vowelsptr)





or you could use arrays such as:





int countcons(char *, char []);


int countcons(char *strptr, char vowelsptr[])





You could even mix the two.


They are more or less the same thing. (although, most times char* have a '\0' and individually defined vowels arrays will not unless you put one in with the other individual chars) You define a character array called vowels[]. When you pass vowels as a parameter in your functions, char[] and char* are interchangable because vowels is just the address of the first char in a group of characters.








However, you will still have to change your code. In countcons and countvows, You cannot compare a single char in *strptr to vowels and expect it to compare that one char to every char in the array. you have to do something like


if( *strptr != vowels[0] %26amp;%26amp;


*strptr != vowels[1] %26amp;%26amp;


*strptr != vowels[2] %26amp;%26amp;


*strptr != vowels[3] %26amp;%26amp;


*strptr != vowels[4])


times++;


for countcons. You could still use vowels[0], vowels[1], etc if you pass vowels as a char * instead of a char[].





Am I making sense, or just confusing you more?





-------


Part of your problem with errors in counting deals with case sensitivity and spaces. 'A' is not the same as 'a' and if you check the string "Amanda" against your vowel array, you will only find 2 vowels because your vowel array does not contain 'A' Also when you count consonants by seeing if they are in your vowel array, 'A' will be considered a consonant and so will a space between words.


-------


Glad to help. :) I hope you finish it on time. Let me know if you have any other questions.
Reply:You made me curious. What is the program your bf convinced you to write? Report It

Reply:The first answer isn't too bad. I think I would make it a little smaller and use isalpha() and strchr() would be cleaner.





char *vowels="aeiouAEIUO";





int countvows(char *strptr, vowels)


{


int times = 0;





while(*strptr) {


if(isalpha(*strptr)) { // we know it's a-zA-Z now


if(strchr(vowels,*strptr)) // It's a vowel


times++;


strptr++;


} // if


} // while





consonants would be the same procedure basically , except the if() would be:


if(!strchr(vowels,*strptr)) // It's a consonant


}


No comments:

Post a Comment