why is this vowel removal program not working

i fiddled with it a bit so i might have changed the index syntax a few times im also sposed to use substring but i cant understand how in this context
lastly when ive inevitably sussed this out the next practice is a tic tac toe game, i understand i have to create an array and display it on the consol just i dont know where to look to study in preparation for it, i have enough tutprials, its more about learning to use what you know...


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
#include <iostream>
#include <string>

using namespace std;

int main () {
string stringone;
cout << "put in the stinking sentence mofo" << endl;
getline (cin,stringone);
string string1;

string1.assign(stringone);

for (int q=0;stringone.length();q++)
{
 switch (stringone[q])
    {
        case 'a':
        string1.replace(q,q++," ");
        break;
        case 'e':
        string1.replace(q,q++," ");
        break;
        case 'i':
        string1.replace(q,q++," ");
        break;
        case 'o':
        string1.replace(q,q++," ");
        break;
        case 'u':
        string1.replace(q,q++," ");
        break;

    }

}
cout << stringone << " is " << string1 << endl;


return 0;

}






cheers you guys are the best...FACT
for (int q=0;stringone.length();q++) there is no condition there.
http://www.cplusplus.com/reference/string/string/replace/ check out what the parameters mean.
two errors
1.in the for loop
second condition should be q<stringone.length()
2. in the replace statementstring1.replace(q,q++," ")
you are actually replacing the letter next to the vowel with " ".
so change the statementstring1.replace(q,q," "
Three comments.
1. Take a close look at this line (and maybe consider what's wrong with it) for (int q=0;stringone.length();q++)

2. string1.replace(q,q++," "); It's generally not a good idea to modify the loop control variable (in this case q) unless you want to change the normal execution of the loop. Rather than q++, consider (q + 1).

3. Review the string.replace() function to see what parameters it takes and how it is supposed to work: http://www.cplusplus.com/reference/string/string/replace/
Last edited on
When I try for the input, say," How are you", the compiler say string script out of range.
So I replaced the replace statement string1.replace(q,q," "); with
string1[q]=' ';

It works fine
you know what i did what you guys said and the console just sits there and blinks at me...no answer just a vacant computer blink

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
#include <iostream>
#include <string>


using namespace std;

int main ()
{


string stringone;
cout << "put in the stinking sentence mofo" << endl;
getline (cin,stringone);
string string1;

string1.assign(stringone);

for (int q=0;q < stringone.length();q+1)
{
 switch (stringone[q])
    {
        case 'a':
        string1[q]=' ';
        break;
        case 'e':
        string1[q]=' ';
        break;
        case 'i':
        string1[q]=' ';
        break;
        case 'o':
        string1[q]=' ';
        break;
        case 'u':
        string1[q]=' ';
        break;

    }

}
cout << stringone << " is " << string1 << endl;

return 0;
}


oh wait the < WAS > but it still no workee
Last edited on
Please mark as solved
@devonrevenge

This for loop, is messing up your program.
for (int q=0;q < stringone.length();q+1)
It should be
 
for (int q=0;q < stringone.length();q++)

And if you want the capital vowels also to be removed, you should add more case checks: as in
1
2
3
4
5
6
7
8
case 'a':
case 'A':
string1[q]=' ';
break;
case 'e':
case 'E':
string1[q]=' ';
// etc. 
Last edited on
It looks like my recommendation not to use q++ here string1.replace(q,q++," "); was misinterpreted.

(Instead, a different part of the program was changed)
still doesnt work
Show your latest non-working code?
Topic archived. No new replies allowed.