Click to See Complete Forum and Search --> : C++ Problem


nikolas22t
03-28-2005, 05:19 PM
The Problem Is
Write a program to count the vowels and letters in free text given as standard input. Read text a character at a time until you encounter end-of-data.
Then print out the number of occurrences of each of the vowels a, e, i, o and u in the text, the total number of letters, and each of the vowels as an integer percentage of the letter total.
Suggested output format is:
Numbers of characters:
a 3 ; e 2 ; i 0 ; o 1 ; u 0 ; rest 17
Percentages of total:
a 13%; e 8%; i 0%; o 4%; u 0%; rest 73%


I wrote this
/* Program5:
* Print
* Nicholas Tryfonos
* Student Id: S024N0784
* Date: 21 MAR 2005
*/

#include <iostream.h>
#include <stdlib.h>
#include <string.h>
int j,k; //j for loop, k for counter
float a,e,i,o,u;//small letter for no of char
int A,E,I,O,U; // large for percantage
char in [100];
void main()
{
cout<<"Enter A Word Or frace"<<endl;
cin>>in;
k=strlen(in); // counter k take value of the lenght of string
cout<<endl;


for(j=0;j<=k;j++)//loop from 0 to the end of counter
{
if ('a'==in[j])
a++;


if ('e'==in[j])
e++;

if ('i'==in[j])
i++;

if ('o'==in[j])
o++;

if ('u'==in[j])
u++;
}


cout<<"You Typed "<<k<<" Characters, Which Was :"<<endl;
cout<<"a: "<<a<<" e: "<<e<<" i: "<<i<<" o: "<<o<<" u: "<<u<<endl;
cout<<endl;

cout<<"Percantage Of Letter:"<<endl;
cout<<"a: "<< (A=(a/k)*100)<<"% "<<" e: "<< (E=(e/k)*100)<<"% "<<" i: "<<(I=(i/k)*100)<<"% "<<" o: "<<(O=(o/k)*100)<<"% "<<" u: "<<(U=(u/k)*100)<<"% "<<endl;



}
BUT I READS ONLY THE INPUT UNTIL THE FIRST SPACE AND NOT UNTIL THE END OF THE FRACE ANY IDEAS HOW I CAN SOLVE THIS?

Exuro
03-28-2005, 10:44 PM
Maybe using cin.getline() (http://www.cplusplus.com/ref/iostream/istream/getline.html) would solve your problem. If not, then there's always cin.get() (http://www.cplusplus.com/ref/iostream/istream/get.html), for which you can specify the delineator.

Exuro
03-28-2005, 10:51 PM
for(j=0;j<=k;j++)//loop from 0 to the end of counter
{
if ('a'==in[j])
a++;


if ('e'==in[j])
e++;

if ('i'==in[j])
i++;

if ('o'==in[j])
o++;

if ('u'==in[j])
u++;
}
That's the perfect place to use a switch statement, but if you haven't learned about those yet then you should at least implement an else-if structure of some sort...