Strings and stuff

This is a discussion on Strings and stuff within the C++ Programming forums, part of the General Programming Boards category; Hi all, I'm having trouble diagnosing a while loop I created to analyze a string's number of vowels and consonants. ...

  1. #1
    casanova0o7
    Guest

    Unhappy Strings and stuff

    Hi all,
    I'm having trouble diagnosing a while loop I created to analyze a string's number of vowels and consonants. Here is my code:


    while (string[i] != '\n')
    {
    if (string[i] == 'a' )
    vowels++;
    else
    consonants++;

    i++;

    }//end of analyzing loop

    Some help would be greatly appreciated.

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    For a start, the condition tests for '\n' not '\0' or NULL.....

    What other problems have you got?

  3. #3
    casanova0o7
    Guest

    Counting vowels

    I think I need one more tweak to make the whole engine work. The change from '/n' to '/0' worked, but the loop doesn't separate the vowels from the consonants. Instead, it counts all the characters as vowels.

    Here is the code I used:

    char string[256];
    int vowels=0, consonants=0;

    cout << "Input string: ";
    cin.getline( string, 256, '\n');

    cout << "You typed: " << string << endl;

    int i=0;

    while (string[i] != '\0')
    {
    if (string[i] == 'a' || 'e' || 'i' || 'o' || 'u' )
    vowels++;
    else
    consonants++;
    i++;

    }//end of analyzing loop
    char string[256];
    int vowels=0, consonants=0;

    cout << "Input string: ";
    cin.getline( string, 256, '\n');

    cout << "You typed: " << string << endl;

    int i=0;

    while (string[i] != '\0')
    {
    if (string[i] == 'a' || 'e' || 'i' || 'o' || 'u' )
    vowels++;
    else
    consonants++;

    i++;

    }//end of analyzing loop

    cout << "Number of vowels in your string: " << vowels << endl;
    cout << "Number of non-vowels in your string:" << consonants << endl;

  4. #4
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    Code:
    if (string[i] == 'a' || 'e' || 'i' || 'o' || 'u' )
    not legal!!!

    Have to include seperate equals statements. Use a newline for each vowel comparison to make ur code look neat.

    if (string[i]=='a' ||
    string[i]=='b' ||

    and so on...

  5. #5
    casanova0o7
    Guest
    Good call! I had a similar line work in a different application (with int) so I never suspected that illegality. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fwrite can't write dynamic strings???
    By Devil Panther in forum C Programming
    Replies: 26
    Last Post: 05-19-2009, 03:18 AM
  2. Creating Strings from other stuff
    By Manitoadlet in forum C++ Programming
    Replies: 7
    Last Post: 09-10-2002, 07:07 PM
  3. Strings and stuff
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 08-01-2002, 04:36 PM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 03:09 AM
  5. Adding stuff to strings from files..
    By R@m0n in forum C++ Programming
    Replies: 4
    Last Post: 02-03-2002, 02:38 PM

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