Strange bug in program.

I've written a code to remove the vowels in a sentence. It mostly seems to work, but the remove function strangely begins to ignore certain vowels, and take out the first letter upon repeating.
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
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <string>

using namespace std;

bool isVowel(char ch);
void noVowel(string& line);

char a, b;
string line;

int main()
{
	char end = 'y';

	cout << "This program will take your input and remove the vowels. Such that \"Hey There!\" " << endl;
	cout << "Becomes: \"Hy Thr!\"" << endl << endl;

	while (end != '#')
	{
		cout << "Please enter a word or sentence:" << endl;
		getline(cin, ::line);

		noVowel(line);
		
		cout << endl << ::line << endl << endl;
		cout << "If you would like to enter another word or phrase enter any key." << endl;
		cout << "If you would like to terminate press #" << endl;
		cin >> end;
	}
	return 0;
}

bool isVowel(char ch)
{
	ch = tolower(ch);

	if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
		return true;
	return false;
}

void noVowel(string& line)
{
	string vowels;
	for (size_t i = 0; i < line.size(); i++)
	{
		if (isVowel(line[i]))
		{
			vowels += line.substr(i,1);
			line[i] = '*';
		}
		line.erase(remove(line.begin(), line.end(), '*'), line.end());
			
	}
}


Output:
This program will take your input and remove the vowels. Such that "Hey There!"

Becomes: "Hy Thr!"

Please enter a word or sentence:
There are more things in Heaven and Earth

Thr r mr thngs n Havn nd arth

If you would like to enter another word or phrase enter any key.
If you would like to terminate press #
g
Please enter a word or sentence:



If you would like to enter another word or phrase enter any key.
If you would like to terminate press #
There
Please enter a word or sentence:

hr

If you would like to enter another word or phrase enter any key.
If you would like to terminate press #.


Could somebody help me?

This program will take your input and remove the vowels. Such that "Hey There!"
Becomes: "Hy Thr!"

Please enter a word or sentence:
iiit

it

If you would like to enter another word or phrase enter any key.
If you would like to terminate press #
itetutot
Please enter a word or sentence:

tttt

If you would like to enter another word or phrase enter any key.
If you would like to terminate press #


So your program works if there is only one vowel but if there are multiple it has problems.

I'm not sure why yet but the problem is in this part of code

1
2
3
4
5
6
		if (isVowel(line[i]))
		{
			vowels += line.substr(i,1);
			line[i] = '*';
		}
		line.erase(remove(line.begin(), line.end(), '*'), line.end());


Ok I see the problem but will leave it to you to fix.

if you type would
The output is "wuld"

The problem is with your line.erase.
when you remove a vowel, it throws off your line.size.

Here's what I did to figure it out and fix it.

1
2
3
4
5
6
7
		if (isVowel(line[i]))
		{
		cout << i << ":"<<isVowel(line[i]) << ":"<< line[i] << endl; // for testing
			vowels += line.substr(i,1);
			line[i] = '*';
			i--;
		}


The problem I created is now it hacks off the first char.
not sure how you want to fix that but I would recommend create a new variable and if a vowel do nothing, if not copy that char to the new string.

If you really want to use string erase, you might read the the string backwards that way if you delete a vowel, it doesn't affect the rest of the string.
Last edited on
closed account (ybf3AqkS)
1
2
3
4
5
6
void noVowel(string& line)
{
    for(string::size_type i = 0; i<line.size(); i++)
        if(isVowel(line[i]))
            line.erase(i, 1);
}


Also try to keep variables inside some function, global variables are bad, and in this case it adds to the confusing because you have global variables with the same name as other variables.
Last edited on
What a simple, elegant solution, Samuel Adams. Thank you.
The hacking off the first Character problem was an artifact of how I was containing the loop. I used a char at the end as my while exit condition. I changed over my while exit condition to simply include the first cin string (line), and it solved that problem. Here is my finished code, check to see if it has any bugs I couldn't detect.

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>
#include <iomanip>
#include <algorithm>
#include <string>

using namespace std;

bool isVowel(char ch);
void noVowel(string& line);

char a, b;
string line;

int main()
{
	char end = 'y';

	cout << "This program will take your input and remove the vowels. Such that \"Hey There!\" " << endl;
	cout << "Becomes: \"Hy Thr!\"" << endl << endl;

	while (line != "#")
	{
		cout << "If you would like to exit program type \"#\" at any time." << endl;
		cout << "Please enter a word or sentence:" << endl;
		getline(cin, ::line);

		noVowel(line);

		cout << endl << ::line << endl << endl;
	}
	return 0;
}

bool isVowel(char ch)
{
	ch = tolower(ch);

	if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
		return true;
	return false;
}

void noVowel(string& line)
{
	string vowels;
	for (size_t i = 0; i < line.size(); i++)
	{
		if (isVowel(line[i]))
		{
			vowels += line.substr(i, 1);
			line[i] = '*';
			i--;
		}
		line.erase(remove(line.begin(), line.end(), '*'), line.end());
	}
}

exf(). Thank you, but for various reasons I have to use substr protocol for this program. Thank you, though, that would work.
Last edited on
closed account (ybf3AqkS)
That function uses substr for no reason, you place the vowels into a string but then never use that string.
You are correct, thee substr is a distraction. But the program needs a substr, as this is an assignment and it calls for it. So is there another method to accomplish this with a substr?
Topic archived. No new replies allowed.