who can solve this program for me ?

 DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

# Thread: who can solve this program for me ?

1. Registered User
Join Date
Apr 2006
Posts
3

## who can solve this program for me ?

Request :

write a program to count the vowels and alphabets in an input line of charachters, Read the charachters one at a time until you encounter the end of line charachter. then print out :
- the number of occurences of each the vowels (a,e,i,o,u) whether capital or small,
- the total number of alphabets, and
- the integer percentage of each vowel.

note that the program should ignore all non-alphabets.
use the switch statement to solve this problem

example:
for the following input:
A good word is a charity
suggested output format is :

Number of vowels:
a 3 ; e 0 ; i 2 ; o 3 ; u 0

Total number of letters is 19

Integer percentage of each vowel :
a 16%; e 0%, i 11% ; o 16% ; u 0%

my Program:

#include <iostream>
#include <string>
using namespace std;

int main()
{
char dummy;
int count, countA, countE, countI, countO, countU, num, length ;
string S1;
char S;

count = 0 ;
countA = 0 ;
countE = 0 ;
countI = 0 ;
countO = 0 ;
countU = 0 ;
num = 0 ;

cout << endl << endl;
cout << " Enter a sentence : ";
getline(cin,S1);
length = S1.length();

while ( num <= length )
{ S = S1.substr(num,1); // ERROR

while ( S >= 'A' || S <= 'Z' || S >= 'a' || S <= 'z')
{
count++ ;
switch (S)
{
case 'a' :
case 'A' : countA++;
break;
case 'e' :
case 'E' : countE++;
break;
case 'i' :
case 'I' : countI++;
break;
case 'o' :
case 'O' : countO++;
break;
case 'u' :
case 'U' : countU++;
}
}
num++;
}

cout << "\n Number of vowels:\n";
cout << " a : " << countA << endl;
cout << " e : " << countE << endl;
cout << " i : " << countI << endl;
cout << " o : " << countO << endl;

cout << " Total number of letters is : " << count << endl;

cout << " Integer Percentage of each vowel : \n";
cout << " a : " << int(countA*100/count) << "%" << endl;
cout << " e : " << int(countE*100/count) << "%" << endl;
cout << " i : " << int(countI*100/count) << "%" << endl;
cout << " o : " << int(countO*100/count) << "%" << endl;

cin >> dummy;
return 0;
}

2. Registered User
Join Date
Jan 2006
Posts
55
You just can't assign string type to char type like that. I did some changes so that your code should compile and execute but it still has some logical error. Just look into it.

I just started corrected your code as the error came and here is the changed code.

Code:
```#include <iostream>
#include <string>
using namespace std;

int main()
{
char dummy;
int count, countA, countE, countI, countO, countU, num, length ;
string S1;
string S;

count = 0 ;
countA = 0 ;
countE = 0 ;
countI = 0 ;
countO = 0 ;
countU = 0 ;
num = 0 ;

cout << endl << endl;
cout << " Enter a sentence : ";
getline(cin,S1);
length = S1.length();

while ( num <= length )
{
S = S1.substr(num,1); // ERROR

while ( S >= "A" || S <= "Z" || S >= "a" || S <= "z")
{
count++ ;
switch (atoi(S.c_str()))
{
case 'a' :
case 'A' : countA++;
break;
case 'e' :
case 'E' : countE++;
break;
case 'i' :
case 'I' : countI++;
break;
case 'o' :
case 'O' : countO++;
break;
case 'u' :
case 'U' : countU++;
}
}
num++;
}

cout << "\n Number of vowels:\n";
cout << " a : " << countA << endl;
cout << " e : " << countE << endl;
cout << " i : " << countI << endl;
cout << " o : " << countO << endl;

cout << " Total number of letters is : " << count << endl;

cout << " Integer Percentage of each vowel : \n";
cout << " a : " << int(countA*100/count) << "%" << endl;
cout << " e : " << int(countE*100/count) << "%" << endl;
cout << " i : " << int(countI*100/count) << "%" << endl;
cout << " o : " << int(countO*100/count) << "%" << endl;

cin >> dummy;
return 0;
}```
Last edited by Nokia2280; 04-11-2006 at 05:19 AM.

3. Registered User
Join Date
Jan 2006
Posts
55
Or you could do something like this
Code:
`S = reinterpret_cast<char>(S1.substr(num,1).c_str());`

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 FAQ Latest Articles Java .NET XML Database Enterprise