Nov 28, 2014 at 5:49pm UTC
Hey, so this is the question:
Write a program that reads a group of chars till $. Then, compute # of vowels, # of digits, # of word and # of special chars. Your program should display all required results.
So in what way should I do it? Loop, array, ...?
Nov 28, 2014 at 10:53pm UTC
Loops are control structures while variables and constants may be of any array type. I think you need to use both of them. Try it!
Nov 30, 2014 at 6:23pm UTC
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
#include <iostream>
using namespace std;
int main()
{
char enteredCharacter;
int vowels, digits, words, special =0;
cout<< "Enter characters or '$' to exit\n" ;
cin>> enteredCharacter;
while (enteredCharacter != '$' )
if (enteredCharacter "" 'a' || enteredCharacter "" 'e' || enteredCharacter "" vowels++;)
else
if (isdigit ( enteredCharacter) )
{
digits++;
}
else
if (isalpha ( enteredCharacter))
{
words++;
}
else
{
special++;
}
cin>> enteredCharacter;
}
cout<< "\nNumber of vowels: " << vowels;
cout<< "\nNumber of digits: " << digits;
cout<< "\nNumber of words: " << words;
cout<< "\nNumber of special characters: " << special;
cout<< "\n" ;
system ("pause" );
return 0;
}
what's wrong???
Last edited on Nov 30, 2014 at 6:24pm UTC
Nov 30, 2014 at 6:29pm UTC
What are you trying to do on line 10, it's full of syntax errors.
I think you want those "" to be ==, and there shouldn't be semi-colons in an if statement, either.
You should be incrementing vowels after you check if it's a vowel, inside the if clause, not the if-statement parentheses.
Also, note that you don't have any brackets around your while loop. I didn't run your program but that looks like an infinite loop.
Last edited on Nov 30, 2014 at 6:32pm UTC
Nov 30, 2014 at 6:32pm UTC
int vowels, digits, words, special =0;
You'll want to initialize all of these to 0, since you're using them as counters. Right now only special is being checked.
Should "words" be "letters" (or consonants?) since it's looking at one character at a time?
Edit: With a little fixing up, this is what you get. (You might want to convert the input to lowercase or check for upper case vowels.)
Enter characters or '$' to exit
Hello World 2014!$
Number of vowels: 3
Number of digits: 4
Number of words: 7
Number of special characters: 1
Last edited on Nov 30, 2014 at 6:42pm UTC
Nov 30, 2014 at 7:22pm UTC
Okay, so everything is fixed now except the
cout "doesn't name a type"
+ expected unqualified-id before return & expected declaration before '}' token!
^^ for the last steps!
Nov 30, 2014 at 7:33pm UTC
After cin you have written '}' which is not required as per the program is concerned..
so may be because of that you would be getting the error!!
Nov 30, 2014 at 7:39pm UTC
What is your current code?
Nov 30, 2014 at 7:41pm UTC
Yes thanks it worked but when I run the program and I write some characters
and then press enter, and then I can't something else ?
and when I enter $ I get this:
http://i60.tinypic.com/2888aqu.png
Last edited on Nov 30, 2014 at 7:53pm UTC
Nov 30, 2014 at 8:06pm UTC
Your program does read exactly one character, then it runs into an endless loop unless your character entered was the '$'-sign.
Put your input statement into the loop, but be careful. You have to change your design a little bit.
Nov 30, 2014 at 8:09pm UTC
enteredCharacter == vowels++;
Do you mean to just increment the vowels counter here? (vowels++
) Not check for equality with the entered character?
What about i,o and u?
Edit - you're missing brackets for the while statement.
Last edited on Nov 30, 2014 at 8:11pm UTC
Nov 30, 2014 at 8:39pm UTC
If you take the if..else out of the while loop, it's only going to run through that code once.
edit: like this? I'm not sure if this is what you mean to do.
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
#include <iostream>
using namespace std;
int main()
{
char enteredCharacter;
int vowels=0, digits=0, consonants=0, special=0;
cout<< "Enter characters or '$' to exit\n" ;
cin>> enteredCharacter;
while (enteredCharacter != '$' )
{
if (enteredCharacter == 'a' || enteredCharacter == 'e' || enteredCharacter == 'i' || enteredCharacter == 'o' || enteredCharacter == 'u' )
vowels++;
else if (isdigit(enteredCharacter))
digits++;
else if (isalpha(enteredCharacter))
consonants++;
else
special++;
cin >> enteredCharacter;
}
cout<< "\nNumber of vowels: " << vowels;
cout<< "\nNumber of digits: " << digits;
cout<< "\nNumber of consonants: " << consonants;
cout<< "\nNumber of special characters: " << special;
cout<< "\n" ;
return 0;
}
Enter characters or '$' to exit
Madrid
11:92
e
$
Number of vowels: 3
Number of digits: 4
Number of consonants: 4
Number of special characters: 1
Last edited on Nov 30, 2014 at 9:45pm UTC