Making it harder than it is?

This is a discussion on Making it harder than it is? within the C++ Programming forums, part of the General Programming Boards category; I'm trying to write a program where the user inputs a sentence, then outputs the number of vowels. But I'm ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    75

    Question Making it harder than it is?

    I'm trying to write a program where the user inputs a sentence, then outputs the number of vowels. But I'm trying to use a function "isVowel" where I first go through the sentence and return true for any vowel, and otherwise false. This is what I have... and I have a feeling I'm making this harder than it needs to be. Help?

    Code:
    #include <iostream>
    
    using namespace std;
    
    bool isVowel(char);
    
    int main()
    {
    	int sum;
    	char vowel;
    	string sentence;
    	cout << "Enter a sentence" << endl;
    	getline(cin, sentence);
    	cout << "There are " << isVowel(sum) << " vowels in this sentence." << endl;
    
    	return 0;
    }
    
    bool isVowel(char sum)
    {
    	sum = sum + true;
    	if ("A" || "a" || "E" || "e" || "I" || "i" || "O" || "o" || "U" || "u")
    		return true;
    	else
    		return false;
    	
    	return sum;
    }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    Code:
    if ("A" || "a" || "E" || "e" || "I" || "i" || "O" || "o" || "U" || "u")
    could be
    Code:
    if(strchr("AaEeIiOoUu", sum))
    or, if you want to keep it your way, then do this:
    Code:
    if(sum == 'A' || sum == 'a' ...)
    ---new section---
    Code:
    sum = sum + true;
    That doesn't do anything. In fact, it might be a syntax error.

    You call isVowel() with a char, not a string/array. Either make it handle strings (and return int, not bool), or call it in a loop with each char in your string.
    Last edited by dwks; 07-10-2005 at 12:55 PM. Reason: Fix typos
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    The variable sum, declared in main(), is not used.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    [note]
    strchr() searches the string, "AaEeIiOoUu", for a char. If it finds a match, it returns the address of the matching char. (This will be true in an if statement.) If there is no match, it returns NULL (false in an if statement).
    [/note]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Banned
    Join Date
    Jun 2005
    Posts
    594
    this might be a little overkill but how bout this
    Code:
    #include <iostream>
    #include <string>
    #include <algorithm>
    
    using namespace std;
    
    int isVowel(string mystring);
    
    
    int main()
    {
    	string vowelcheck;
    	cout << "Enter a sentence  : ";
    	getline(cin, vowelcheck, '\n');
    	cout << "There are " << isVowel(vowelcheck) << " vowels in this sentence." << endl;
    	cin.get();
    	return 0;
    }
    
    int isVowel(string mystring)
    {
    	int numvow = 0;
    	transform (mystring.begin(), mystring.end(), mystring.begin(), tolower);
    	string::iterator forward;
    	for(forward = mystring.begin(); forward != mystring.end(); forward++)
    	{
    		if(*(forward) == 'a' || *(forward) == 'e' || *(forward) == 'i' || *(forward) == 'o' || *(forward) == 'u' )
    		{
    			numvow++;
    		}
    	}
    	return numvow;
    }

  6. #6
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    We could save ourselves from copying the string when passing it to the function, and we could save ourselves from having to compare with ten vowels and instead only compare five vowels by setting the lowercase bit on the character just before we compare. (Sort of like the previous solution, except that we do it just-in-time.) (And let's pretend that every character set is ASCII-like. :-) )

    So this is similar to the previous solution -- it also uses count_vowels as the function name because isVowel is deceptively named.

    Code:
    #include <string>
    #include <iostream>
    
    using std::string; using std::cout; using std::endl; using std::getline;
    
    size_t count_vowels(string & s);
    
    int main()
    {
      string s;
      cout << "Enter a line, please:\n";
      getline(cin, s);
      cout << "Vowel count: " << count_vowels(s) << endl;
    
      return 0;
    }
    
    size_t count_vowels(string & s)
    {
      size_t ret = 0;
      string::iterator	p = s.begin(), e = s.end();
    
      while (p != e) {
    	ret += (bool) strchr("aeiou", *p | 32); /* Adds 1 if vowel */
    	++p;
      }
    
      return ret;
    }
    Last edited by Rashakil Fol; 07-10-2005 at 03:10 PM.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    75
    Quote Originally Posted by ILoveVectors
    this might be a little overkill but how bout this
    Code:
    #include <iostream>
    #include <string>
    #include <algorithm>
    
    using namespace std;
    
    int isVowel(string mystring);
    
    
    int main()
    {
    	string vowelcheck;
    	cout << "Enter a sentence  : ";
    	getline(cin, vowelcheck, '\n');
    	cout << "There are " << isVowel(vowelcheck) << " vowels in this sentence." << endl;
    	cin.get();
    	return 0;
    }
    
    int isVowel(string mystring)
    {
    	int numvow = 0;
    	transform (mystring.begin(), mystring.end(), mystring.begin(), tolower);
    	string::iterator forward;
    	for(forward = mystring.begin(); forward != mystring.end(); forward++)
    	{
    		if(*(forward) == 'a' || *(forward) == 'e' || *(forward) == 'i' || *(forward) == 'o' || *(forward) == 'u' )
    		{
    			numvow++;
    		}
    	}
    	return numvow;
    }
    Thank you for your help. I understand things a lot better. But when I execute this, I have to enter twice. What do I need to change so that the rest of the program executes after i enter once?

  8. #8
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    You don't need that cin.get(); line up there.

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    75
    Quote Originally Posted by Rashakil Fol
    You don't need that cin.get(); line up there.
    OK, got rid of that... but I still have to enter twice for the rest of the program to execute. Help?

  10. #10
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Did you recompile? It works for me.

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    75
    The program works, yes. But after i input a sentence and push enter, nothing happens. It's only after I push enter again that it tells me how many vowels there are. Is it not doing that for you?

  12. #12
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    This is what the count_if function is for:

    Code:
    #include <iostream>
    #include <algorithm>
    #include <string>
    
    using namespace std;
    
    struct isVowel
    {
        bool operator()(char sum)
        {
            return sum=='A' || sum=='a' || sum=='E' || sum=='e' || sum=='I' || sum=='i' ||
                   sum=='O' || sum=='o' || sum=='U' || sum=='u';
        }
    };
    
    int main()
    {
        string sentence;
    
        cout << "Enter a sentence" << endl;
        getline(cin, sentence);
    
        cout << "There are " << count_if(sentence.begin(),sentence.end(),isVowel())
             << " vowels in this sentence." << endl;
    
        return 0;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Do a board search for 'isvowel' and have fun.


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    As said in another thread, hitting enter twice is probably due to a compiler bug.

    Why are you using other people's code? Just fix your own, its not that far off. As you can see by ILoveVector's example, a good way to count the vowels is to keep a count that starts at zero, then loop through each letter in the string and compare it to the 10 possible characters (5 vowels upper and lower case). If the current character is a vowel, increment the counter.

    What it seems like you are trying to do is combine an isVowel function and a count Vowels function, consider making them two separate functions. The countVowels function would loop through the string and the isVowel function would look a lot like your original code in the first post.

    They are very good tools and things you should learn eventually, but I wouldn't turn in a homework assignment using transform or count_if. They are obviously too advanced for your current level, and your instructor might get suspicious that it's not your code.

  15. #15
    Registered User
    Join Date
    Jun 2005
    Posts
    75
    Quote Originally Posted by Daved
    As said in another thread, hitting enter twice is probably due to a compiler bug.

    Why are you using other people's code? Just fix your own, its not that far off. As you can see by ILoveVector's example, a good way to count the vowels is to keep a count that starts at zero, then loop through each letter in the string and compare it to the 10 possible characters (5 vowels upper and lower case). If the current character is a vowel, increment the counter.

    What it seems like you are trying to do is combine an isVowel function and a count Vowels function, consider making them two separate functions. The countVowels function would loop through the string and the isVowel function would look a lot like your original code in the first post.

    They are very good tools and things you should learn eventually, but I wouldn't turn in a homework assignment using transform or count_if. They are obviously too advanced for your current level, and your instructor might get suspicious that it's not your code.
    Thanks for the advice Daved. This is what I've come up with. I think it's pretty close to what it needs to be.... but if anyone could point out my errors, I'd really appreciate it. I have to turn this in finished tomorrow and I really want to get what I'm doing here. Thanks.

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    bool isVowel(string);
    int countVowels(bool);
    
    int sum;
    char vowel;
    int counter;
    string myString;
    
    int main()
    {
    	cout << "Enter a sentence" << endl;
    	getline(cin, myString);
    	cout << "There are " << isVowel(sum) << " vowels in this sentence." << endl;
    
    	return 0;
    }
    
    bool isVowel(string myString)
    {
    	if ("A" || "a" || "E" || "e" || "I" || "i" || "O" || "o" || "U" || "u")
    		return true;
    	else
    		return false;
    }
    
    int countVowels(bool)
    {
    	for (counter = 0, counter != false, counter++)
    	{
    		if (isVowel(myString) = true)
    			counter = counter + 1;
    		else counter = counter;
    	}
    	return counter;
    }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. making sprites
    By DavidP in forum Game Programming
    Replies: 9
    Last Post: 02-20-2010, 06:00 AM
  2. Making great graphics
    By MadCow257 in forum Game Programming
    Replies: 1
    Last Post: 02-20-2006, 10:59 PM
  3. Making control...
    By Finchie_88 in forum C++ Programming
    Replies: 2
    Last Post: 09-07-2004, 01:42 PM
  4. Replies: 2
    Last Post: 01-13-2003, 12:28 PM
  5. About Unix Programming - Making a career desision
    By null in forum C Programming
    Replies: 0
    Last Post: 10-14-2001, 07:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21