Removing and counting vowel, C++

->Hello, my program is to get user input and print their sentence into a non vowel sentence while showing the number of vowel in their sentence. It should keep asking the user for sentence until they decide to quit by inputting "-".

->My problem with this program is that I am not able to do a loop to repeat the process from the beginning for the user to input another sentence. Also, I am not sure how to write the complete non-vowel sentence. Lastly, I am not able to get the program to stop with "--"

->I've tried putting a while-loop in front of the inner loop of the For-statement but it didn't work. And, I though of making a new string that will hold all the non vowel and output it on the screen but I fail to do so. Any hints will be greatly appreciate, thank you for the help in advance.


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
#include<iostream>
#include<string>
using namespace std;
int main()
{
	cout << "Please enter a sentence" << endl;

	string sentence;
	getline(cin, sentence);
	
	while (true)
		if (sentence == "--")
			break;
			int j, a = 0, e = 0, i = 0, o = 0, u = 0;
			for (j = 0; j < sentence.length(); j++)
			{
				if (sentence[j] == 'a' || sentence[j] == 'A')
				{
					a++;
				}
				else if (sentence[j] == 'e' || sentence[j] == 'E')
				{
					e++;
				}
				else if (sentence[j] == 'i' || sentence[j] == 'I')
				{
					i++;
				}
				else if (sentence[j] == 'o' || sentence[j] == 'O')
				{
					o++;
				}
				else if (sentence[j] == 'u' || sentence[j] == 'U')
				{
					u++;
				}
				else if (sentence[j] == '--')
					break;
				
				string newstring;
			
				//not sure how to implement this for-function to print non-vowel sentence.
					for (i = 0; newstring[i] != '\n' ; i--)
					{

					}

			}
		
			cout << "The sentence : " << newstring << " has " << endl << " a :" << a << endl << " e :"
				<< e << endl << " i :" << i << endl << " o :" << o << endl << " u :" << u << endl;
return 0;
}
You need braces for the while loop on line 11. Currently only the if on line 12 is in your infinite loop.

Further more you need line 9 within the loop too (after line 11).

Move line 40 before line 15.

The else case on line 37 should be newstring += sentence[j];

The loop on line 43 is unnecessary. newstring is updated within the loop line 15.
Last edited on
Wow thanks a lot, you are amazing. I was trying to debug it so hard but I only had to move a few code to fix it. Once again, thanks a bunch for the clear explanation was very helpful!
I am currently going to school for programming, up until now, I am self taught.

I hope that you don't mind, but I was playing around with your code. Here is what i come up with. I learn by actually playing around with code and changing things to see what effect it has.

Apologies ahead of time if I broke any rules.

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
#include<iostream>
#include<string>
using namespace std;
int main()
{
    string sentence;
	string newstring = "";
    int a = 0, e = 0, i = 0, o = 0, u = 0;

	cout << "Please enter a sentence" << endl;

    getline(cin, sentence);
    if (sentence.length() != 0)
    {
        for (unsigned int j = 0; j < sentence.length(); j++)
        {
            switch (tolower(sentence[j]))
            {
            case 'a':
                a++;
                break;
            case 'e':
                e++;
                break;
            case 'i':
                i++;
                break;
            case 'o':
                o++;
                break;
            case 'u':
                u++;
                break;
            default:
                newstring += sentence[j];
                break;
            }
        }
    }
    cout << "The sentence : " << newstring << " has " << endl << " a :" << a << endl << " e :" << e << endl << " i :" << i << endl << " o :" << o << endl << " u :" << u << endl;
    return 0;
}
Last edited on
No worries, I really do mind if you use my code. I'll be more than happy to let you experiment with it. Also, it is quite interesting how you changed into a switch statement which make the readability much better and shorter.
Topic archived. No new replies allowed.