0

I wrote a program that reads input a word at a time until a lone 'q' entered.The program then report the number of words that began with vowels,the number that began with consonants,and the number that fit neither of those categories.

#include <iostream>
#include <cstdlib>

int main()
{
char ch;
bool cont = true; //for controlling the loop
bool space = false; //see if there is a space in the input
    int i = 0; //checking if the input is the first word
int consta, vowel, others;
consta = vowel = others = 0;
std::cout<<"Enter words (q to quit)\n";

while (cont && std::cin>>ch) //continue while cont is true and the input succeded
{
    if (i == 0) //check if this is the first word
    {
        if (isalpha(ch))
            if ((ch == 'a' ||ch == 'e' ||ch== 'i' ||ch== 'o' ||ch== 'u') || (ch == 'A' ||ch== 'E' ||ch== 'I' ||ch== 'O' ||ch== 'U'))
                ++vowel;
            else
                ++consta;
        else
            ++others;
        ++i; //add 1 to i so this if statement wont run again
    }


    if (space == true) //check if the last input was a space
    {
        if (!isspace(ch)) //check if the current input is not a space
        {
         if (ch != 'q') //and ch is not 'q'
         {
        if (isalpha(ch))
            if ((ch == 'a' ||ch == 'e' ||ch== 'i' ||ch== 'o' ||ch==   'u') || (ch == 'A' ||ch== 'E' ||ch== 'I' ||ch== 'O' ||ch== 'U'))
                ++vowel;
            else
                ++consta;
        else
            ++others;

        space = false;
        }

        }
        else
            cont = false;
    }
    if (isspace(ch)) //check if ch is a space
        space = true;
}

std::cout<<"\n"<<consta<<" words beginnig with constants\n";
std::cout<<vowel<<" words beginnig with vowels\n";
std::cout<<others<<" words beginning with others\n";

system("pause");
return 0;
}

But it doesn't terminate when i input a space and a 'q'. But if i in put '^Z' it does terminate but constantans are always 1 and the others are always 0.

fish_shoes
  • 115
  • 9

3 Answers3

1

The problem is that std::cin by default ignores all tabs and white spaces. So in your code

if (isspace(ch)) //check if ch is a space
    space = true;

never sets space to true. One way to avoid this problem is to use std::cin.get(ch) instead of std::cin>>ch

Alexandru Barbarosie
  • 2,952
  • 3
  • 24
  • 46
0

Hope this helps you

#include<iostream>
#include<cstdlib>
#include<string>
int main()
{
    char ch[50];
    int consta, vowel, others;
    consta = vowel = others = 0;
    bool flag = false;
    std::cout<<"Enter words (q to quit)\n";
    while(1)
    {
        if(flag == true)
        {
            break;
        }
        gets(ch);
        char* pch;
        pch = strtok(ch," ");
        if(strcmp("q",pch)==0)
        {
            break;
        }
        while (pch != NULL)
        {
            if(strcmp("q",pch)==0)
            {
                flag = true;
                break;
            }
            if(isalpha(pch[0]))
            {
                if ((pch[0] == 'a' ||pch[0] == 'e' ||pch[0]== 'i' ||pch[0]== 'o' ||pch[0]== 'u') || (pch[0] == 'A' ||pch[0]== 'E' ||pch[0]== 'I' ||pch[0]== 'O' ||pch[0]== 'U'))
                    ++vowel;
                else
                    ++consta;
            }
            else
                ++others;
            pch = strtok (NULL, " ");
        }
    }
    std::cout<<"\n"<<consta<<" words beginnig with constants\n";
    std::cout<<vowel<<" words beginnig with vowels\n";
    std::cout<<others<<" words beginning with others\n";
    std::cin.ignore();
    return 0;
}

Please refer this for strtok. This counts all the words whether separated by spaces or in separate lines.

Saksham
  • 9,037
  • 7
  • 45
  • 73
0

Assuming that you want to use the same code structure, In the while loop use

while (cont && std::cin>> std::noskipws >>ch)

std::noskipws : This changes the behavior of the cin stream to not ignore spaces. It also adds a space at the end of the stream too. Beware of the end space. Fortunately your code handles spaces so you wont see its affect.

As you are handling spaces in the code this will work. Also, by default the behavior of cin is to ignore spaces.

However I think you can reduce the complexity of the code and you may not need this statement

virusrocks
  • 861
  • 1
  • 5
  • 19