to count the number of spaces vowels consonants and digits in a line

wads wrong with tis...

#include <iostream>
#include <cstdlib>
#include <cctype>
using namespace std;

int main()
{
int digit=0,consonant=0,space=0,vowel=0,i;
char text;

cout<<"Input a line of text terminated with the @ character -> ";
cout<<endl;


for (i=0;i<=256;i++)
{
cin.get(text);


if (text=='@')
{
break;
}
else if (isspace(text))
{
space++;
}
else if (isdigit(text))
{
digit++;
}
else if (text!='a'||'e'||'i'||'o'||'u')
{
consonant++;
}
else if (text=='a'||'e'||'i'||'o'||'u')
{
vowel++;
}
}




cout<<"5a) The number of spaces in the line is ";
cout<<space<<endl;
cout<<"5b) The number of vowels in the line is ";
cout<<vowel<<endl;
cout<<"5c) The number of consonants in the line is ";
cout<<consonant<<endl;
cout<<"5d) The total number of digits in the line is ";
cout<<digit<<endl;

cout<<endl;
system("pause");
return 0;
}
Last edited on
1
2
3
4
5
6
7
8
else if (text!='a'||'e'||'i'||'o'||'u')
{
consonant++;
}
else if (text=='a'||'e'||'i'||'o'||'u')
{
vowel++;
}

should be
1
2
3
4
if (text=='a'||text=='e'||text=='i'||text=='o'||text=='u')
    vowel++;
else//You don't need to check if text!='a' etc. as it is the result of the previous condition. You may need to check if text is a letter
    consonant++;
If you ever want to check multiple inequalities, you need to use a logical AND rather than OR. For instance,

1
2
if (text!='a'&&text!='e'&&text!='i'&&text!='o'&&text!='u')
    consonant++;


On your current program this is a moot point since Bazzy's solution is more clear, but it is something to keep in mind for the future.
Topic archived. No new replies allowed.