0

So theres the exercise where it should read number of word that begins with vowels, consonant, and one that doesn't fit neither categories. So far my code is :

#include <iostream>

int main()
{
   using namespace std;
   char a;

   cout << "Enter words (q to quit)\n";
   cin.get(a);

   int others = 0;
   int vowels =0;
   int consonant =0;

   while (a != 'q')
   {
      cin.get(a);
      if(isalpha(a)) {
              switch (a) {
                      case 'a':
                      case 'i':
                      case 'u':
                      case 'e':
                      case 'o':
                          vowels++;
                          break;
                      default:
                          consonant++;
                          break;
              }
      }
      else {
              others++;
      } 

   }


  cout << vowels << " words beginning with vowels\n";
  cout << consonant << " words beginning with consonant\n";
  cout << others << " others";


    return 0;
}

But it doesn't read the beginning of the word. Heres an example :

Enter words (q to quit)

The 12 awesome oxen ambled quietly across 15 meters of lawn. q

9 words beginning with vowels

11 words beginning with consonant

7 others.

Where is the problem here ?

EDIT: Its done now. If anyones interested

#include <iostream>
#include <cstring>
#include <cctype>

int main()
{
    using namespace std;
    string a;

    cout << "Enter words (q to quit)\n";

    int others = 0;
    int vowels =0;
    int consonant =0;

    cin >> a;

    while (a != "q")
    {
        cin >> a;
        if(isalpha(a[0])) {
            switch (a[0]) {
                case 'a':
                case 'i':
                case 'u':
                case 'e':
                case 'o':
                    vowels++;
                    break;
                default:
                    consonant++;
                    break;
            }
        }
        else {
            others++;
        }

    }


    cout << vowels << " words beginning with vowels\n";
    cout << consonant << " words beginning with consonant\n";
    cout << others << " others";

    return 0;
}

Thanks for all the suggestions

  • You need to tokenize the string. Have a look [here](http://stackoverflow.com/questions/53849/how-do-i-tokenize-a-string-in-c). – sanastasiadis Oct 19 '16 at 12:13
  • Using smaller test cases usually makes it easier to determine what's going wrong. You could start with "The" and "Ox", for instance. – molbdnilo Oct 19 '16 at 12:13
  • A good string splitting method also: http://stackoverflow.com/a/236803/3652270 – sanastasiadis Oct 19 '16 at 12:17
  • It's really weird.. If I entered "The" the output is 1 word vowel, 2 word consonant, 0 others. Even if its not true, it read the vowels and consonant correctly. But when it's "Awesome" the output is 3 word vowel, 4 word consonant, 0 others. Its not true, and it didn't read the vowels and consonant correctly. Wth is wrong with dis code.. xd –  Oct 19 '16 at 12:29
  • You read the first char outside the while, but you use it only to see if it's not 'q', then you read another char as soon as you enter the loop. Try reading a char or better a single word (otherwise how can you distinguish "quit" from "q"?), only in the condition of the while loop. – Bob__ Oct 19 '16 at 13:38
  • Its not allowed.. From the book "Write a program that reads input a word at a time until a lone 'q' is entered" –  Oct 19 '16 at 21:43

3 Answers3

1

The cin.get(a) reads a single character (letter). To read a word you may use operator >> with std::string:

// make a std::string variable to hold a single word
string word;

// later read the word from the standard input
cin >> word;
Juraj Blaho
  • 13,301
  • 7
  • 50
  • 96
1

You are reading chars 1 by 1 until you hit "q" and analyze all of them. If it was me, I would probably just concatenate everything into 1 string until the "q" and then evaluate the string. Which could be done by a split on space then looping all the words in the resulting array and doing your switch case on the substring of the first char of each words.

stack reader
  • 167
  • 11
0

Your task is to read a word at a time and consider only the first letter of that word, but your program is reading single chars (using cin.getc) at every iteration.

Besides, the first char is read outside the while loop and discarded immediately after checking if it isn't a 'q'.

A closer fit to your assignment could be this snippet:

#include <iostream>
#include <string>
#include <cctype>

int main()
{
    using std::cout;

    int others = 0,
        vowels = 0,
        consonants = 0;

    std::string word;

    cout << "Enter words (q to quit)\n";

    // read a word at a time till a single 'q'
    while ( std::cin >> word  &&  word != "q" )
    {
        // consider only the first letter of the word
        char c = word[0];
        if ( std::isalpha(c) )
        {
            if ( c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' ||
                 c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U' )
            {
                ++vowels;
            }
            else
            {
                ++consonants;
            }
        }
        else
        {
            ++others;
        } 
    }
    cout << vowels << " words beginning with vowels\n";
    cout << consonants << " words beginning with consonant\n";
    cout << others << " others";

    return 0;
}

Testing the program with your examplary input:

The 12 awesome oxen ambled quietly across 15 meters of lawn. q

Gave:

5 words beginning with vowels
4 words beginning with consonant
2 others
Bob__
  • 12,361
  • 3
  • 28
  • 42
  • @asdfg1234 Check the results of the last code you posted. – Bob__ Oct 19 '16 at 22:26
  • Is it not in your compiler ? –  Oct 19 '16 at 22:30
  • The output for me: 5 words beginning with vowels, 4 words beginning with consonant, 2 others –  Oct 19 '16 at 22:33
  • @asdfg1234 Consider the case when the first word start with a vowel, [for example](https://ideone.com/bhorPm) – Bob__ Oct 19 '16 at 22:34
  • That's because I'm too lazy to put the capitals for the vowels lol xd –  Oct 19 '16 at 22:48
  • @asdfg1234 [No](https://ideone.com/bhorPm), it's because your code consumes the first word without checking if it starts with a consonant or a vowel, then it reads the 'q', finds it's a consonant, and only then exits the loop. – Bob__ Oct 19 '16 at 22:51
  • Easy solution then, just erase cin before the while loop. Then it would check for the vowels and consonants. Just like you said, huh Bob –  Oct 19 '16 at 23:05
  • Meant to add this '?' –  Oct 19 '16 at 23:06