Finding consonant and vowel number.

Sorry, I know I asked this question before, but I tried a different code, and the program crashes every time I execute.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "stdafx.h"

#include <iostream>
#include <string>
int main(){
	std::string myname[4] {"My", "Name", "is", "Matilda"};
	int i;
	i = 0;
	for (i = 0; i < myname[4].length(); i++){
		if (myname[i] == "a" || myname[i] == "e" || my name[i] =="i" || myname[i] == "o" || myname[i] == "u"){
			int a = 0;
			a = a + 1;
std::cout <<a;

		}
	}
}

Please help.I'm totally new to programming, only started about 2 days ago, excuse me.
Last edited on
You are trying to access your array out of bounds in your loop. What you probably want is something like:

1
2
3
4
5
6
7
8
9
int main()
{
    const int ARRAY_SIZE = 4;

    std::string myname[ARRAY_SIZE]{"My", "Name", "is", "Matilda"};

    for(int i = 0; i < ARRAY_SIZE; ++i)
        for(size_t j = 0; j < myname[i].length(), ++j)
...


Although you really should consider using a vector instead of the array.

And you could use one of the string.find() functions instead of the inner loop.
Last edited on
Topic archived. No new replies allowed.