Begginer needs assistance

I am required to make a program where i input a sentence and the program outputs the number of characters and the number of times a certain letter is used within that sentence.

So far my program counts only the number of chracters for the first word.

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>

int main()
{
cout<< "please write a sentence\n";
string Charcount;
int a;
int e;
int i;
int o;
int u;

cin>>Charcount;
Cout<< "your number of characters is\n";

a = 0;
e = 0;
i = 0;
o = 0;
u = 0;
cout<<Charcount.length();
cout<<endl;
int z = 0;

if(Charcount[z] =='a' || Charcount[z]=='A');
{
a++;
}
else
return 0;


Remember that i am extremly new to this. So i appologize if my coding makes very little sense.
Hi there,

You shouldn't cin a string from keyboard because it only gets to the first space, you can cout that string to see, what you can use is

getline (cin, Charcount)

You can read more from http://www.cplusplus.com/doc/tutorial/basic_io/

About the counting of the specific character, you should use a loop:

1
2
3
4
5
6
7
for(int z=0; z<Charcount.lenght();++z)
{
if(Charcount[z] =='a' || Charcount[z]=='A');
{
a++;
}
}


No need an else at the end if there is no other if.


Hope this will help :).
Topic archived. No new replies allowed.