Counting vowels in a string - invalid identifier

This is a discussion on Counting vowels in a string - invalid identifier within the C++ Programming forums, part of the General Programming Boards category; This is just a small fragment of my code so I didn't include all the vowels. Code: string response; int ...

  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 03:53 AM.

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    #include <string> and replace strlen(response) with response.length() .
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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 05:34 PM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,725
    What is your current code?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,893
    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 03:26 AM.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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,893
    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,893
    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, 12: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

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