-3

I am having trouble with my program, there's something wrong in how it counts vowels and consonants

#include<iostream>
using namespace std;

int main(){
    int num[10],even = 0,odd = 0;
    char choice;
    int vowelcount = 0;
    int concount = 0;
    string word;

    cout<<"MENU:"<<endl<<"[N]umber"<<endl<<"[L]etter"<<endl<<"Choice : ";
    cin>>choice;

    switch(choice){
    case 'n': case 'N':
        cout << "Enter 10 integers: \n"; 
        for(int i = 0; i < 10; i++) { 
            cin >> num[i]; 
            if((num[i] % 2) == 0) { 
                even++; 
            } 
        } 
        odd = 10 - even; 
        cout << "Even: " << even << endl; 
        cout << "Odd: " << odd << endl; 
        system("pause");
        cout<<"Do you want to repeat the program ? Y/N ";
        break;

    case 'l': case 'L':
        cout<< "Enter 10 Letters : \n";
        cin>> word;
        for (int i=0; word [i] != '\0'; i++){
            word[i] = tolower (word[i]);
            for (int i=0; word [i] != '\0'; i++)
                switch(choice){
                case 'A' :
                case 'E' :
                case 'I' :
                case 'O' :
                case 'U' :
                    vowelcount++;
                    break;
                default:
                    concount++;
                }
        }
        cout<<" total vowels = " <<vowelcount << endl;
        cout<<" total consonant = " <<concount << endl;
        system("pause");
        return 0;
    }
}
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • is there an error? or are you just getting the wrong numbers? – user1950929 Jan 27 '14 at 20:35
  • Make your job a LOT easier, take a look here: http://en.cppreference.com/w/cpp/algorithm/count – olevegard Jan 27 '14 at 20:36
  • @olevegard It's obviously an exercise, using a built-in function won't help him learn the underlying techniques. – Barmar Jan 27 '14 at 20:39
  • You just called your mechanic and said "I'm having trouble with my car it doesn't start" and then gave him no more information. Do you really think he'd be able to figure out the problem on that alone? – Avery3R Jan 27 '14 at 20:39
  • possible duplicate of [Counting of consonants and vowels](http://stackoverflow.com/questions/21362159/counting-of-consonants-and-vowels) – Mysticial Jan 27 '14 at 20:39
  • 2
    Please do not repost your questions just because they get closed. Doing so will only get you banned. – Mysticial Jan 27 '14 at 20:40
  • See http://stackoverflow.com/questions/9438209/c-for-every-character-in-string for how to loop over the characters of `std::string`. Your `for` loop for `word` is not correct. – Barmar Jan 27 '14 at 20:46

2 Answers2

1

Okay, several problems here. First off, always try to give more information then 'there is something wrong'. I simply copied your example in to visual studio and was pretty quickly able to figure out your problems, but with more information I probably wouldn't have needed to do this. Also, there's no need for the entire question to be uppercase. :)

So... your switch statement is being done on a variable called choice. This variable is the one that you're using to select your menu option. You need to be running your switch statement on the character you're testing. Also, you've got two loops and you only need the one.

Right now, because you're running the program on choice and you have two loops, each time through each loop choice is always 'l' or 'L' which are consonants, but it's being run a number of times equal to the length of the input string squared. So your response is 0 for the number of vowels, because it never sees any, and the length of your input string squared because you have the nested loops and it's counting 'L' that many times.

Darinth
  • 511
  • 3
  • 14
0

Instead of using a switch statement, you could use a string and the std::string::find method:

std::string vowels = "AEIOUaeiou";
std::string consonants = "BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz";

if (vowels.find(letter) != std::string::npos)
{
  ++vowelcount;
}
else
{
    if (consonants.find(letter) != std::string::npos)
    {
        ++consonantcount;
    }
}
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154