need help debugging character input.

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.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#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.
You are using the extraction operator which implies formatted extraction. Formatted extraction means whitespace will be skipped. Therefore line 50 will never evaluate to true.

Try replacing line 14 with:

while (cont && std::cin.get(ch)) //continue while cont is true and the input succeded
Thanks it works now.
Topic archived. No new replies allowed.