Counting vowels and consonants

I am trying to write a program that counts the number of vowels and consonants from user input. I also would like to count the number of words and lines from the users input. I also would like to give the user the option to quit the program but mine isn't working. This is an example of what I want the output to look like:

Vowels:
A: 34
E: 34

..
Total: 68

Consonants:
D: 5
F: 3

..

Total: 7
Summary:
XXX words on YYY lines with a vowel/consonant ratio of ZZZ

I am new to c++ and I honestly have no idea what I am doing
Here is some code that I have so far but it is incorrect.
#include "stdafx.h"

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include<cstring>


int main(int argc, char* argv[])
{
char alphabet[] = { 'a', 'e', 'i', 'o', 'u' ,'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z' };
char user_input[255];
char *tokPtr;
int a, e, i, o, u;
a=e=i=o=u=0;


int vowCount;
int conCount;

vowCount = 0;
conCount = 0;

cout << "Please enter text or enter quit to exit:";
cin.getline(user_input,255);

// begin tokenization of sentence
tokPtr = strtok( user_input, " " );

// continue tokenizing until tokPtr becomes NULL
while( tokPtr != NULL )
{
tokPtr = strtok( NULL, " "); // get next token


for ( int x=0; user_input[x] != '\0'; x++ )

{
if(user_input[x]== 'a' || 'e' || 'i' || 'o' || 'u' )
{
cout << "Vowels" << endl;
cout << "A:" << a << endl;
cout << "E:" << e << endl;
cout << "I:" << i << endl;
cout << "O:" << o << endl;
cout << "U:" << u << endl;
++vowCount;
cout << "Total: " << endl;
}

else
{
cout << "Consonants:"<< endl;
cout << "B: " <<endl;
cout << "C: " <<endl;
cout << "D: " <<endl;
cout << "F: " <<endl;
cout << "G: " <<endl;
cout << "H: " <<endl;
cout << "J: " <<endl;
cout << "K: " <<endl;
cout << "L: " <<endl;
cout << "M: " <<endl;
cout << "N: " <<endl;
cout << "P: " <<endl;
cout << "Q: " <<endl;
cout << "R: " <<endl;
cout << "S: " <<endl;
cout << "T: " <<endl;
cout << "V: " <<endl;
cout << "W: " <<endl;
cout << "X: " <<endl;
cout << "Y: " <<endl;
cout << "Z: " <<endl;
conCount++;
}

}






}
system("pause");
return 0;

}


Someone help me please : (
Last edited on
The following code does what I think you are trying to achieve, heavily commented, let me know if more help is needed:
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include<iostream> // use C++ input/output library
using namespace std; // use the namespace "std" functions
int main()
{
 int n[256]={0}; // an array to hold the count of all characters entered
 int consonants=0,lines=0,vowels=0,words=1; // number of consonants, vowels, words & lines
 char user_input[255]; // the user input

 cin.getline(user_input,255); // get the user input
 cout<<endl; // leave a line

 if(user_input[0]=='\0') // if the user input is nothing
 {
  words=0; // then there are no words
 }

 for(int c=0;user_input[c]!='\0';++c) // loop through the entire user input
 {
  ++n[user_input[c]]; // add 1 to the array holding the count of each character
  if(user_input[c]=='\n') // if the user input is a new line
  {
   ++lines; // increase the number of lines
  }
  if(user_input[c]==' '&&user_input[c+1]!='\0') // if the character is a space & the next character is not null
  {
   ++words; // then increase the number of words
  }
 }

 vowels=n['A']+n['a']+n['E']+n['e']+n['I']+n['i']+n['O']+n['o']+n['U']+n['u']; // add up the number of vowels

 cout<<"Vowels:"<<endl; // output the vowels
 cout<<"A: "<<n['A']+n['a']<<endl;
 cout<<"E: "<<n['E']+n['e']<<endl;
 cout<<"I: "<<n['I']+n['i']<<endl;
 cout<<"O: "<<n['O']+n['o']<<endl;
 cout<<"U: "<<n['U']+n['u']<<endl;
 cout<<"Total: "<<vowels<<endl<<endl;

 // add up the number of consonants
 consonants=n['B']+n['b']+n['C']+n['c']+n['D']+n['d']+n['F']+n['f']+n['G']+n['g']+n['H']+n['h']+n['J']+n['j']+n['K']+n['k']+n['L']+n['l']+n['M']+n['m']+n['N']+n['n']+n['P']+n['p']+n['Q']+n['q']+n['R']+n['r']+n['S']+n['s']+n['T']+n['t']+n['V']+n['v']+n['W']+n['w']+n['X']+n['x']+n['Y']+n['y']+n['Z']+n['z'];
 cout<<"Consonants:"<<endl; // output the consonants
 cout<<"B : "<<n['B']+n['b']<<endl;
 cout<<"C : "<<n['C']+n['c']<<endl;
 cout<<"D : "<<n['D']+n['d']<<endl;
 cout<<"F : "<<n['F']+n['f']<<endl;
 cout<<"G : "<<n['G']+n['g']<<endl;
 cout<<"H : "<<n['H']+n['h']<<endl;
 cout<<"J : "<<n['J']+n['j']<<endl;
 cout<<"K : "<<n['K']+n['k']<<endl;
 cout<<"L : "<<n['L']+n['l']<<endl;
 cout<<"M : "<<n['M']+n['m']<<endl;
 cout<<"N : "<<n['N']+n['n']<<endl;
 cout<<"P : "<<n['P']+n['p']<<endl;
 cout<<"Q : "<<n['Q']+n['q']<<endl;
 cout<<"R : "<<n['R']+n['r']<<endl;
 cout<<"S : "<<n['S']+n['s']<<endl;
 cout<<"T : "<<n['T']+n['t']<<endl;
 cout<<"V : "<<n['V']+n['v']<<endl;
 cout<<"W : "<<n['W']+n['w']<<endl;
 cout<<"X : "<<n['X']+n['x']<<endl;
 cout<<"Y : "<<n['Y']+n['y']<<endl;
 cout<<"Z : "<<n['Z']+n['z']<<endl;
 cout<<"Total: "<<consonants<<endl<<endl;

 cout<<"Summary:"<<endl; // running total
 cout<<words<<" words on "<<lines<<" lines with a vowel consonant ratio of "<<vowels<<":"<<consonants<<" ("<<(float)vowels/consonants<<")"<<endl;

 cin.get(); // wait for user to press enter (so they see output)
 return 0; // exit program
}
Topic archived. No new replies allowed.