Thread: Counting vowels in a string - invalid identifier

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    23

    Counting vowels in a string - invalid identifier

    This is just a small fragment of my code so I didn't include all the vowels.

    Code:
        string response;
        int array [5]= {0, 0, 0, 0, 0};
    
    
        cout << "enter a string of text\n" << endl;
        cin >> response;
     
          for(int i=0;i<strlen(response);i++)
        {
        if response == 'A'        //invalid identifier
            array[i]++;
        if response == 'E'     //invalid identifier
            array[i]++;
           }
    I am getting the error that "response" is not a valid identifier? When I'm trying to check the characters in the string for either A or E and increment the count in the appropriate area of the array...
    Last edited by hencherz; 02-24-2012 at 04:53 AM.

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    #include <string> and replace strlen(response) with response.length() .

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The condition of an if needs to be in parentheses.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    23
    I now have a working code.

    Problem is when I output the incremented array (its supposed to increment) I'm being told that all the vowels have occured 0 times when this is not the case?

    I've tried changing the array[i] to a number i.e. [1] instead of [i]
    Last edited by hencherz; 02-24-2012 at 06:34 PM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is your current code?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I've tried changing the array[ i] to a number i.e. [1] instead of [ i]
    That's actually a correct change. Just remember to use 0 for A, 1 for E, etc. Also, are you forgetting to count lowercase vowels?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Feb 2012
    Posts
    23
    Quote Originally Posted by CornedBee View Post
    That's actually a correct change. Just remember to use 0 for A, 1 for E, etc. Also, are you forgetting to count lowercase vowels?
    I am coding it so that it accepts lower case vowels only at the moment, I can change that later but its good to go for the moment.


    @laserlight, here is my current code:

    Code:
    #include <iostream>
    #include <string> 
    
    int main()
    {
    
    string response; int array [5]= {0, 0, 0, 0, 0}; cout << "enter a string of text\n" << endl; cin >> response; for(int i=0;i<response.length();i++) { if (response == "a") //invalid identifier array[0]++; if (response == "e") //invalid identifier array[1]++; } cout << array[1]; //doing this as a test but get 0 no matter how many e's I type in. return 0; }
    Last edited by hencherz; 02-25-2012 at 04:26 AM.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    strlen(response) shouldn't even compile. You were told to use response.length().

    Also, I don't see a place where you output the results.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User
    Join Date
    Feb 2012
    Posts
    23
    Quote Originally Posted by CornedBee View Post
    strlen(response) shouldn't even compile. You were told to use response.length().

    Also, I don't see a place where you output the results.
    Sorry, got a mix up there, it is fixed now.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Character literals are written with single quotes. Double quotes are string literals, which you can't meaningfully compare to single characters. That this compiles at all points out that you have another mistake, namely in your if conditions, you compare the entire input string against the single character string. Which obviously won't work.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Registered User
    Join Date
    Feb 2012
    Posts
    23
    Quote Originally Posted by CornedBee View Post
    Character literals are written with single quotes. Double quotes are string literals, which you can't meaningfully compare to single characters. That this compiles at all points out that you have another mistake, namely in your if conditions, you compare the entire input string against the single character string. Which obviously won't work.
    I see what you mean, I just put in one of the vowels and the output incremented accordingly.

    I changed the double quotes to single but the compiler begins throwing errors at me..
    I know in C that you can declare a char string which makes it easier to check a string for individual characters? I don't know how I would implement this in c++

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    if (response[i] == 'a')
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 11-08-2011, 01:13 PM
  2. Need help with counting vowels prog
    By truetrini20 in forum C++ Programming
    Replies: 9
    Last Post: 07-12-2010, 07:44 AM
  3. Replies: 6
    Last Post: 08-11-2008, 12:50 AM
  4. Counting Vowels within a string.
    By patso in forum C Programming
    Replies: 12
    Last Post: 04-09-2008, 04:21 PM
  5. counting vowels
    By trippedwire in forum C++ Programming
    Replies: 10
    Last Post: 10-01-2004, 11:58 PM