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; You have to pay attention to detail and think about these things. * You have two functions - one called ...

  1. #16
    Registered User
    Join Date
    Jan 2005
    Posts
    7,338
    You have to pay attention to detail and think about these things.

    * You have two functions - one called isVowel and one called countVowels. In your main function, you say "There are " isVowel(sum) " vowels in this sentence". Does it make sense to output isVowel, or maybe it would be better to output the result of countVowels. Also, shouldn't you be counting the vowels in the string you read in - myString - instead of sum?

    * Your isVowel function should probably just check a single character like it did originally. So instead of taking a string, it should probably take a char.

    * To compare a char to a character literal, you would do something like this:
    if (myChar == 'A' || myChar == 'B' || myChar == 'a' || myChar == 'b')

    notice the use of ' instead of ", and notice that you have to use == each time. You should be able to adapt that example for use in your isVowel function.

    * Your countVowels function takes a bool as you have it written. You should probably be counting the vowels in a string, so maybe you should change it to take a string as an argument instead.

    * Your loop in countVowels doesn't make sense. You want to loop through every character in the string. The characters in the string go from index 0 to one less than the length, so you can go from index = 0 to index < myString.length().

    * You want to have two separate variables in your countVowels function - one for looping through the string and one for counting the vowels.

    See if you can put those changes in and good luck. Remember to think about what each thing is doing or is supposed to do and pay attention to each detail.

  2. #17
    Registered User
    Join Date
    Jun 2005
    Posts
    75
    I'm really starting to get the hang of this I think. This is now what I have and the only compiling problem I'm having is coming up with 2 errors when I build, but I have no idea what they mean. How's everything looking?

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

  3. #18
    *this
    Join Date
    Mar 2005
    Posts
    498
    Code:
    countVowels(string)
    Does this string have a name??

    Code:
    bool isVowel(char vowel)
    here your using the same name as the global variable vowel...not good.

  4. #19
    *this
    Join Date
    Mar 2005
    Posts
    498
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    bool isVowel(char);
    int countVowels(string);
    
    // you dont need sum here
    // take away vowel
    // neither index
    // dont need string
    
    int main()
    {
       string myString; //declare it here
       
        cout << "Enter a sentence" << endl;
        getline(cin, myString);
        cout << "There are " << countVowels(myString) << " vowels in this sentence." << endl;
    
        return 0;
    }
    
    bool isVowel(char vowel)
    {
        if (vowel =='A' || vowel =='a' || vowel =='E' ||
            vowel =='e' || vowel =='I' || vowel =='i' ||
            vowel =='O' || vowel =='o' || vowel =='U' || vowel =='u')
            return true;
        else
            return false;
    }
    
    int countVowels(string myString)
    {
       int sum = 0;
        for (int index = 0; index < myString.length(); index++)
        {
            if (isVowel(myString[index]))
                sum++;
            // dont need sum = sum because it already = itself!
        }
        return sum;
    }
    Last edited by JoshR; 07-11-2005 at 02:20 PM.

  5. #20
    Registered User
    Join Date
    Jan 2005
    Posts
    7,338
    You have more than the two errors, but it is looking better.

    * Your isVowel function itself looks perfect. It's done.

    * You have a few problems with understand variables and passing them to functions. I didn't read your functions thread, so I don't know what problems you were having there, but you need to understand passing arguments to a function.

    For example, pretend you have a function called countVowels that counts the number of vowels in a string. To run that function, you must pass give it a string to look at. You can use that function over and over on different strings if you want, so you must tell it which string to use.

    Now look at your code. You call the countVowels function with this line:
    cout << "There are " << countVowels(index) << " vowels in this sentence." << endl;

    You are passing it index. What is index? It is not a string. You should pass it the string you want it to look at. In this case, you used getline to read text into the variable called myString, so you should pass myString into the function.

    * Your global variables are not necessary.

    You use the sum variable only in the countVowels function, so declare it there instead (and initialize it to 0, since at the start you have 0 vowels).

    You need the vowel variable only in the isVowel function, and you already declared it there as a parameter of the function, so you can remove the global version.

    You use the index variable only in the countVowels function, so declare it there instead (you initialize it to 0 in your for loop already).

    You use a string called myString in two places, but those two places are separate. First, in the main function, you use myString to read in a line of text. So declare string myString in the main function before you use. It might be better to call it lineOfText or something else so you don't get confused.

    Then, you also use a string in the countVowels function. Conceptually, this is a different string than the one in main() because the countVowels function doesn't know about the main function. It just knows that some code is going to give it a string. You need to name the parameter in the function definition, so add a name to it (like myString) as you did in the code a few posts ago.

    At this point you don't need any more global variables, so the four lines of code that declare variables at the top of your program can be removed.

    * Your function prototypes should match your function definitions. So for example your isVowel function definition (at the bottom) looks like this:
    Code:
    bool isVowel(char vowel)
    {
    ...
    }
    so your prototype at the top should look like this:
    Code:
    bool isVowel(char vowel);
    You'll need to change the prototype for countVowels to match as well.

    * And finally, the way to get a character from an index into a string is to use the array subscript (e.g. myString[index]) or the at function (e.g. myString.at(index)).

    Let's see if that helps you along.

    ... Or somebody can give you code and you'll learn less than if you figured these things out yourself.
    Last edited by Daved; 07-11-2005 at 02:17 PM.

  6. #21
    *this
    Join Date
    Mar 2005
    Posts
    498
    lol lets just say the code goes with your explanation...

  7. #22
    Registered User
    Join Date
    Jun 2005
    Posts
    75
    Thank you both, Daved and JoshR. Once I went through your instructions Daved, I was able to fix everything! Thanks for putting up with my questions, which I'm sure for you guys seems like "how could she NOT know that?!!?" LOL. I'm gettin' there, slowly but surely!

Page 2 of 2 FirstFirst 12
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