Hints with a code creation.

Pages: 12
Hey guys, I'm a young and upcoming potential programmer and while I am starting to understand the basics of C++, some things do still allude me greatly.


I just wanted to ask for a hint on a problem I having, I want to try and solve it myself, but I also want some help in how to start this problem. I'd really appreciate it.


The problem in question is that I have is that I have to create a program that tests inputted words for vowels. I understand I have to use quite a few functions and for loops for it, but I can't figure out the specifics. If someone could make it a bit more clearer for me, I'd like it.

This depends what you exactly need: test if there is a vowel in word, print all vowels in word, calculate how many of each vowel in word... (hint: those are all one-liners)

Anyway, you should consider making is_vowel function which would receive a char and return a bool. It would be useful for later use.
Basically what you have is:
1) Get word
2) Check if each letter is a vowel

Step one is pretty self explanatory. Step two could use a little expansion.
1) Create a list of vowels
2) On each letter in the word, see if it matches one of the vowels

You do not say what to do if you find a vowel, so it pretty much ends there. You could make the first step two a function, but unless that is a requirement its not really necessary for something this small.
1
2
3
4
5
6
7
8
start
ask for word
get word
for each letter in word
    for each vowel in list
        does it match
        do something if it does
end
OK Admkrk, that makes it a lot simpler actually, I appreciate it. I'll develop the code as best as I can and I'll come back with my results. Appreciate the responses.

And actually yea, I should have specified, I have to use two functions; one that takes the word and checks for the vowel and returns the amount(e.g. If word is hello, return 2, if word is hmm, return 0)

And the second function has to have two arguments, one that takes an array of strings with a list of words, and the second that is the size of the words. If no such words exist, then I have to return -1.


And finally, in the main function, I have to create an array of strings of size N(A constant that can be chosen) to store the listed words, and then prompts the user to input N word, one word each time, stores them in the list, and then checks them with the second function containing the two arguments.
Last edited on
Ok, here's what I got so far based on the prompts, feel free to critique any way you guys feel is appropriate:

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
#include<iostream>
using namespace std;

char countVowel(char);
char isNoVowelOnList(char);

int main()
{

char word;
const int arraysize = N;
cout << "Enter amount of words used: " << endl;
cin >> N;
cout << "Enter the words: ";
cin >> word; 
cout << countVowel(char) << endl;

}

count Vowel(char a[])
{ 

for(int i = 0; i < N; i++)
     for(int j = 0; j < i; j++)
     if( 
return
}


This is about as much as I could produce on my own based on the instructions I gave on the previous post. So can anyone give me a bigger tip.
Last edited on
It looks like you are all over the place with what you are trying to write. I do not fully understand the requirements, especially for the second function, so I might go astray.

It sounds like you need to create an array of an arbitrary size and have the user fill i t with words. It looks like you are asking the user for the array size which is different. If you need a variable sized array, you are better off using a vector. I am assuming an arbitrary size array here.
const int arraysize = N;
N is not an integer so arraysize cannot be N.
1
2
3
int N = 10;  // arbitrary number 
const int arraysize = N;
string words[arraysize];

will work though
char word; // string word; maybe?

Now that you have an array its time to fill it.
1
2
3
4
5
for each element in the array
    ask for a word
    get the word
    add word to array
    // check for vowels and do something with results? 

I might be misunderstanding that part and I cannot guess at how the rest is supposed to work.
one that takes the word and checks for the vowel and returns the amount(e.g. If word is hello, return 2, if word is hmm, return 0)
There is standard library algorithm for that:
1
2
3
4
5
6
7
8
9
10
bool is_vowel(char c) //helper function
{
    c = tolower(c);
    return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
}

int count_vowels(const std::string& s)
{
    return std::count_if(s.begin(), s.end(), is_vowel);
}
@Admrk: Hmm, well let me post the full problem so you can understand it better:


Part I: Pointer-based string processing(cont.) Write a interactive program that asks the user
to input a list of words and test whether there is a word that doesn't contain vowels.

1. You need to define a function called countVowel that checks whether a given word has
vowels(containing any of a, e, i, o, u), return the number of vowels in this word. E.g., if the
input word is "hello", you function should return 2, but if the input word in "hmm", then it
should return 0.

2. Use the function countVowel to build another function isNoVowelWordOnList. The
function isNoVowelWordOnList takes two arguments, the first argument is an array of
strings containing a list of words and the second one is the size of this list. This function
will scan the entire list and return the index of the first word that doesn't contain any
vowels. If no such word exists in the list, then it will return -1. E.g., if the input list is
{"what", "why", "when"}, then your function should return 1, but if the input list is {"hi",
"hey", "hello"}, then you should return -1.

3. In the main function, you need to create a array of strings of size N(constant that you
choose, say 5) to store the list of words, then ask user to input N word, one word each
time, store them in the list and then check this list with your isNoVowelWordOnList
function. Output the appropriate result.


I hope it makes things a bit clearer for you, and I'll go ahead and see if making the number arbitrary works.
Yes, that makes more sense. N is a number you decide on then ask for N words:
1
2
3
4
5
int N = 10;  // arbitrary number 
const int arraysize = N;
string words[arraysize];

cout << "Please enter " << N << " words: \n";

Then you add then to the array.

MiiNiPaa showed one way make the first function, I am not familiar with all that so I cannot explain it any better.

The second function would just loop through the array and send each word to the first function. If it finds a vowel, it then returns the index of that word. If not, it returns -1.
If it's possible, could you explain an alternate way to make the first function then, I'm not too familiar with MiiNipaa's method as well as I'm still in the beginner portions of the course, or Miinipaa, if it's possible could you explain your method so I can better understand please?


This is my attempt for the
main
function:


1
2
3
4
5
6
7
8
9
10
11
12
int main()
{	
	int index = 0;
	int N = 10;
	const int arraysize = N;
	string words[10];

	for ( int i = 0; i<10; i++)
	cout << "Please Enter " << N << " words: \n";
	cin >> N;
	index += N;
}



Right now, I will attempt to the second with the method Admrk gave to me.
You still have several problems right at the start.
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
int main()
{	
	int index = 0;
	// int N = 10;   You really do not need N, const int arraysize = 10; will do
	const int arraysize = 10;
	// string words[10]; One reason for arraysize is to use it here
        string words[arraysize];
        string word; // needed to hold the input words
        int nonvowel;

	// for ( int i = 0; i<10; i++) You only need to ask for the words once
	cout << "Please Enter " << arraysize << " words: \n"; 
	// cin >> N; N is an int, you are looking for a string
 	// index += N;
        while(index < arraysize)
        {
            cin >> word;
            words[index] = word;
            index++;
        }

        // Now your array is filled and you can do something with it.
        // This would be a good place for a for loop
        int vowels;
        for(int i = 0; i < arraysize; i++)
        {
            vowels = countVowel(words[i])
            cout << words[i] << " has " << vowels << " vowels in it.\n";
        }

        // Now you have used the 1st function to report how many vowels are in each word.
        // Just need to call the 1nd function and output the result
        nonvowel = isNoVowelWordOnList(words[], arraysizze);
        cout << nonvowel << '\n';

        return 0;
}


I did not test any of this and I am not sure the while loop will work right even if ten words are input, but it should give you a better idea of how to structure the program. I already explained one way to make the function in my first post, fill an array with the vowels and use a nested for loop to check if there is a match. Just do not forget to use tolower() so it does not have to be case sensitive.
Last edited on
Ok, I touched up the code as much as I could based on what I was told. Bare with me because I'm still very new to this.


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
#include<iostream>
using namespace std;


int countVowels(int c)
{
	for (c = 0; c < 10; c++)
	{
		if (c == 'a', 'e', 'i', 'o', 'u')
			c = isNoVowelWordOnList(c);
		else
			return 0;
	}
}

int isNoVowelWordOnList(int i)
{
	const int arraysize = 10;
	char words[arraysize];
	for (i = 0; i < arraysize; i++)
	if (i = 'a', 'e', 'i', 'o', 'u')
		return 1;
	else
		return -1;
}

int main()
{	
	int index = 0;
	const int arraysize = 10;
	char words[arraysize];
	char word;
	int nonvowel;

	
	cout << "Please Enter " << arraysize << " words: \n";
	while (index < arraysize)
	{
		cin >> word;
		words[index] = word;
		index++;
	}

	int vowels;
	for (int i = 0; i < arraysize; i++)
	{
		vowels = countVowels(words[i]);
		cout << words[i] << " has " << vowels << " vowels in it. \n";
	}


	nonvowel = isNoVowelWordOnList(words[10]);
	cout << nonvowel << endl;
	
	return 0;
}



Everything seems fine, but when I try to build it, I keep getting the error that the second function isn't be identified even though it's being called in the first one.
1
2
3
4
5
6
7
8
9
10
int isNoVowelWordOnList(int i)
{
	const int arraysize = 10;
	char words[arraysize];
	for (i = 0; i < arraysize; i++)
	if (i = 'a', 'e', 'i', 'o', 'u') // You are using assignment rather than checking if equal (==)
		return 1;
	else
		return -1;
}


I believe also you will have to OR them..something like

1
2
if (i == 'a' || i == 'e' || i == 'i' || i == 'o' || i == 'u') return 1;
else return -1;


Also you are checking a number for a character..

I have no clue what you are doing.
Last edited on
That's my attempt at checking vowels...
That's my attempt at checking vowels...


Let me get this straight..

You create an array of 10 chars, you assign the integer passed to the function to 0, then iterate through to 9. You then proceed to check if the number 0 to 9 is a vowel?


Does any of that seem right?
Last edited on
No, because I have to check the characters themselves, but that is what I was trying to figure out.
You still seem very confused on some of the basics.
char words[arraysize];
char, as the the name suggests is a character. a character is the same thing as a letter. If you plan to store words that are longer than one letter, this will not work.
http://www.cplusplus.com/doc/tutorial/variables/

int countVowels(int c)
Remember the requirements?
You need to define a function called countVowel that checks whether a given word has vowels...

How is this function supposed to know anything about any of the words?
http://www.cplusplus.com/doc/tutorial/functions/

if (c == 'a', 'e', 'i', 'o', 'u')
There are two problems with this line. The first has already been pointed out.
http://www.cplusplus.com/doc/tutorial/operators/
The second is a type mismatch. This might not throw and error, but you will not get the results you expect if it does not. Since c is declared as an int, 'a' and the rest will also get cast to their ASCII values (or the other way around?). Also 'a' and 'A' have different values.

c = isNoVowelWordOnList(c);
Again, you forgot the requirements,
...return the number of vowels in this word.

Even if everything else worked, this would be wrong.
int isNoVowelWordOnList(int i)
Remember the requirements?
The function isNoVowelWordOnList takes two arguments, the first argument is an array of strings containing a list of words and the second one is the size of this list.

I would not even worry about this function until you get the rest working.

If I were you, I would just get the main function to the point where it can successfully fill the array of words and print them back to the screen. Once you have that, start working on the first function to count the vowels. When you can do that, you should be able to understand what the last function is supposed to do.


You still seem very confused on some of the basics.

char words[arraysize];

char, as the the name suggests is a character. a character is the same thing as a letter. If you plan to store words that are longer than one letter, this will not work.


See, now this is the part that confuses me. My professor didn't go over this in real detail, but he was able to input an array of strings using only char, and was able to print the words fine by inputting them into the array after declaring them with char.

So what is the difference here? I'm sorry if I'm coming off as uninformed, but I really am trying to understand this.
He wasn't using char *aWord by any chance was he? Or am I getting confused by the meaning of your "word" meaning my "letter"?

That * makes all the difference.
Yea, it was a pointer. It was that exact phrase. What difference does it make compared to just char word?
Pages: 12