943,104 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 906
  • C++ RSS
Feb 9th, 2010
0

Vowel counting program.

Expand Post »
Hey all I have another riveting problem from my genius professor T_T. I have to make a program using functions, reference Parameters and full string words. Now if it were just a single letter at a time this would be easy. But it wants us to let the user enter in a whole word, and I have to have the program look at each letter and test it. It give me these operators to use string.str , string.length Kinda clueless on this sense she did her usual and didnt tell us how to do this stuff before hand. Here is my code:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. void countVowels(string str, int& aCt, int& eCt, int& iCt, int& oCt, int& uCt,int& nV);
  7.  
  8. int main()
  9. {
  10. string inputString;
  11. cout << "Please enter a word and we will tell you how many vowels are in it!" << endl;
  12. getline(cin,inputString);
  13.  
  14. countVowels(inputString);
  15.  
  16. }
  17.  
  18. void countVowels (string s, int& aCont, int& eCont; int& iCont; int& oCont; int& uCont int& nonVowl)
  19. {
  20. string.str(s)
  21. while (string.str(s) <= s)
  22. {
  23. if (string.at(s) == 'a')
  24. aCont++;
  25. else
  26. if (string.at(s) =='e')
  27. eCont++;
  28. else
  29. if(string.at(s) == 'i')
  30. iCont++;
  31. else
  32. if(string.at(s) == 'o')
  33. oCont++;
  34. else
  35. if (string.at(s) == 'u')
  36. else nonVowl++;
  37. string.str(s)++;
  38.  
  39.  
  40. }
  41.  
  42. }

Forgive me if its 100 percent off, but as ive said im quite clueless on what I have to do.
Last edited by soapyillusion; Feb 9th, 2010 at 1:52 am.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
soapyillusion is offline Offline
34 posts
since Dec 2009
Feb 9th, 2010
0
Re: Vowel counting program.
str() is a method used with stringstreams to get the string portion. It doesn't seem like you need stringstreams or str() here.

Remember that you can access strings as an array, so string[0] is the first character of the string.

string.str(s) <= s doesn't make any sense. You were on to something when you were talking about the length() method. So you know how long the string is once you've read it in.

If you are calling your method in main, you need to pass in all of the parameters including the reference ones.
Sponsor
Featured Poster
Reputation Points: 1165
Solved Threads: 578
Quantitative Phrenologist
jonsca is offline Offline
4,271 posts
since Sep 2009
Feb 9th, 2010
0
Re: Vowel counting program.
hi....fo r your reference purpose here is a site whereby you can learn on some of your problem regarding string functions. what you have done doesn't really make sense to me. some function which you need to look on is str.length, str.find, str.erase and string::npos class. you will find evething on this site http://www.cplusplus.com/reference/string/string/erase

if any further queries let me know.
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
kesh1000 is offline Offline
69 posts
since May 2009
Feb 9th, 2010
0
Re: Vowel counting program.
Make use of the string functions like find_first_of and so one.
For example you can do something like this :
C++ Syntax (Toggle Plain Text)
  1. string vowels = "aeiou";
  2. string sentence = "hello baby!";
  3. if(sentence.find_first_of(vowels) != string::npos){
  4. cout << "There is a vowel in " << sentence << endl;
  5. }
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,859 posts
since Dec 2008
Feb 9th, 2010
-2
Re: Vowel counting program.
here work with this

i haven't used c++ to compile so mite have some silly errors but shudnt be much good luck and enjoy the programme you just have to some additions and use it as functions for eg 1 function to find all 'a' and other to find 'e' and so on. note i have not catered for uppercase in this which u will need to do it urself and also have to add lots of code. before u understand this prog you will need to do research. then only you will get it. this was purticurlarly the most basic concept i came up with

C++ Syntax (Toggle Plain Text)
  1. #include <iostream.h>
  2. #include <stdlib.h>
  3. #include <string>
  4. int main()
  5. {
  6.  
  7. int length;
  8. int a_count= 0;
  9.  
  10. string str ;
  11. str = "The quick brown fox jumps over the lazy dog";
  12. length = str.length();
  13. string temp;
  14. temp = str;
  15. for (int pos = 0;pos!=length;pos++)
  16. {
  17.  
  18. int curpos;
  19.  
  20. curpos = temp.find('a');
  21. if (curpos != string::npos)
  22. {
  23. a_count ++;
  24. temp.erase(curpos,1);
  25. }
  26.  
  27. else
  28. break;
  29.  
  30.  
  31. }
  32.  
  33. cout <<a_count<<"\n\n\n\n";
  34. system("PAUSE");
  35. return 0;
  36. }
Last edited by kesh1000; Feb 9th, 2010 at 4:07 am.
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
kesh1000 is offline Offline
69 posts
since May 2009
Feb 10th, 2010
0
Re: Vowel counting program.
Ok so I decided sense the other route wasn't working I wanted to go with using another. So I tried using a switch statement, although I still don't think I am using the loop correctly. Heres my new code:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. void countVowels(string str, int& aCt, int& eCt, int& iCt, int& oCt, int& uCt);
  7.  
  8. int main()
  9. {
  10. string inputString;
  11. cout << "Please enter in a word" << endl;
  12. cout << "We will tell you if it's a vowel or not" << endl;
  13. getline(cin,inputString);
  14. countVowels(inputString,0,0,0,0,0);
  15. cout << "The word has:" << endl;
  16. cout << "A's:" << aCt << endl;
  17. cout << "E's:" << eCt << endl;
  18. cout << "I's:" << iCt << endl;
  19. cout << "O's:" << oCt << endl;
  20. cout << "U's:" << uCt << endl;
  21. }
  22.  
  23. void countVowels(string a,int& aCont, ,int& eCont,int& iCont, int& oCont, int& uCont)
  24. {
  25. int nonVowl=0;
  26. string.str(a)
  27.  
  28. for(i=0;i<=string.length();i++)
  29. {
  30. switch(string.at(i))
  31. case 'a':
  32. aCont++;
  33. break;
  34. case 'e':
  35. eCont++;
  36. break;
  37. case 'i':
  38. iCont++;
  39. break;
  40. case 'o':
  41. oCont++;
  42. break;
  43. case 'u':
  44. uCont++;
  45. default:
  46. nonVowel++;
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54. }
  55.  
  56. }

agian I'm trying to get the program to read one letter out of a whole word at a time. Anyone help me fit this into a switch statement?
Reputation Points: 10
Solved Threads: 0
Light Poster
soapyillusion is offline Offline
34 posts
since Dec 2009
Feb 10th, 2010
0
Re: Vowel counting program.
string.str(a)
So you're still not doing anything with that statement. You have almost everything you need, but treat your string like an array a[0] is the first character a[a.length() - 1] is the last.

Also, how are your results getting back to main? Look up what it means to pass something by reference. You have it the right way in your method, just not in main.
Last edited by jonsca; Feb 10th, 2010 at 8:13 pm. Reason: syntax error
Sponsor
Featured Poster
Reputation Points: 1165
Solved Threads: 578
Quantitative Phrenologist
jonsca is offline Offline
4,271 posts
since Sep 2009
Feb 10th, 2010
0
Re: Vowel counting program.
And what's with the mega-indenting? What are you going to do when you have 4 nested loops with IF statements?

4 characters is enough, not 4 tabs:
cpp Syntax (Toggle Plain Text)
  1. void countVowels(string a,int& aCont, ,int& eCont,int& iCont, int& oCont, int& uCont)
  2. {
  3. int nonVowl=0;
  4. string.str(a)
  5.  
  6. for(i=0;i<=string.length();i++)
  7. {
  8. switch(string.at(i))
  9. case 'a':
  10. aCont++;
  11. break;
  12. case 'e':
  13. eCont++;
  14. break;
  15. case 'i':
  16. iCont++;
  17. break;
Moderator
Reputation Points: 3275
Solved Threads: 886
Posting Sage
WaltP is offline Offline
7,699 posts
since May 2006
Feb 10th, 2010
0
Re: Vowel counting program.
ya u will need to keep track of ur vowels so u can display em at a later time in main. what your currently doing is tat ur using ur function to change the values but as soon as u leave your function your values are reset to 0. that is why you need to get a pass by reference.its not a hard thing to do.

all your got to do is get variables in main to pass through the function.you have pass by reference indicated in your function already.

your requirements as of now would be :-
1) create int variables to keep track of vowels(you will need 5 for a,e,i,o,u)
and initialize it to zero
2) pass them in the function call instead of the 0.

your your new function call in your main would look like countVowels(inputString,a,e,i,o,u); intead of countVowels(inputString,0,0,0,0,0); where aeiou are declared int variables initialised to 0
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
kesh1000 is offline Offline
69 posts
since May 2009
Feb 11th, 2010
0
Re: Vowel counting program.
Thanks guys it took forever, but i finally got it :-)
Reputation Points: 10
Solved Threads: 0
Light Poster
soapyillusion is offline Offline
34 posts
since Dec 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: my problem
Next Thread in C++ Forum Timeline: Void Pointer to userdefined Object - Typecasting





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC