continue(y/n) loop problem?

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

int main()
{
	char c, response;
	int Uc = 0, Dc=0, Vc=0, Wc=0;
	string vowels("aeiouAEIOU");

	cout << "I can count the number of upper case, lower case, vowels and words" << endl; 
	cout << "Enter a sentence: ";
	cin.get(c);

	while(c != '\n')
	{ 
		{
		if(isupper(c))  
			Uc++;  
		if(isdigit(c)) 
			Dc++; 
		if(vowels.find(c) != string::npos)
			Vc++;
		if (isspace(c))
			Wc++;

		cin.get(c);
		} 
	}
	 cout << setfill ('.');
	 cout << "  Number of uppercase letters: " << Uc << endl; 
	 cout << "  Number of digits: " << Dc << endl; 
	 cout << "  Number of vowels: " << Vc << endl;
	 cout << "  Number of words: " << Wc << endl;

	 cout << "CONTINUE(y/n)? ";
	 cin >> response;

	 if (response == 'Y' || response=='y') 
	{
	 cout << setfill ('.');
	 cout << "  Number of uppercase letters: " << Uc << endl; 
	 cout << "  Number of digits: " << Dc << endl; 
	 cout << "  Number of vowels: " << Vc << endl;
	 cout << "  Number of words: " << Wc << endl;
	}
	else if (response == 'N'|| response=='n')
	{
	cout << endl;
	}


	system("pause");
	return 0;
}



This is my output I'm getting:
1
2
3
4
5
6
7
8
9
10
11
12
I can count the number of upper case, lower case, vowels and words
Enter a sentence: Happy 2015 Valentine
  Number of uppercase letters: 2
  Number of digits: 4
  Number of vowels: 5
  Number of words: 2
CONTINUE(y/n)? y
  Number of uppercase letters: 2
  Number of digits: 4
  Number of vowels: 5
  Number of words: 2
Press any key to continue . . .


This is how it should look like.
1
2
3
4
5
6
7
8
9
10
11
12
13
I can count the number of upper case, lower case, vowels and words
Enter a sentence: Happy 2015 Valentine
  Number of uppercase letters: 2
  Number of digits: 4
  Number of vowels: 5
  Number of words: 2
CONTINUE(y/n)? y
 Enter a sentence: Prepare Yourself for Exam No 1
  Number of uppercase letters: 4
  Number of digits: 1
  Number of vowels: 8
  Number of words: 5
Press any key to continue . . .
Last edited on

char ans = 'y';
while(ans == 'y')
{
...
...
cout << "CONTINUE(y/n)? ";
cin >> ans;
}
what about the middle part?
You have the code already written but the loop for y/n is just in the wrong place. That is what I was referring to.
so do I put this inside loop?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
	while(c != '\n')
	{ 
		{
		if(isupper(c))  
			Uc++;  
		if(isdigit(c)) 
			Dc++; 
		if(vowels.find(c) != string::npos)
			Vc++;
		if (isspace(c))
			Wc++;

		cin.get(c);
		} 
	}
yeah sure, you allways want to count the number of letters ;)

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
cout << "I can count the number of upper case, lower case, vowels and words" << endl; 
do
{
	cout << "Enter a sentence: ";
	cin.get(c);

	while(c != '\n')
	{ 
		{
		if(isupper(c))  
			Uc++;  
		if(isdigit(c)) 
			Dc++; 
		if(vowels.find(c) != string::npos)
			Vc++;
		if (isspace(c))
			Wc++;

		cin.get(c);
		} 
	}
	 cout << setfill ('.');
	 cout << "  Number of uppercase letters: " << Uc << endl; 
	 cout << "  Number of digits: " << Dc << endl; 
	 cout << "  Number of vowels: " << Vc << endl;
	 cout << "  Number of words: " << Wc << endl;

	 cout << "CONTINUE(y/n)? ";
	 cin >> response;

} while(response == 'Y' || response=='y') ;

        std::cout << std::endl;
	system("pause");


So you just make the a loop, and if the response is Y or y you continue the loop and repeat the progress :)
so when I put it this way it came out weird:

1
2
3
4
5
6
7
8
9
10
11
12
13
I can count the number of upper case, lower case, vowels and words
Enter a sentence: Toaday Is Thu 45 gRa
  Number of uppercase letters: 4
  Number of digits: 2
  Number of vowels: 6
  Number of words: 4
CONTINUE(y/n)? y
Enter a sentence:   Number of uppercase letters: 4
  Number of digits: 2
  Number of vowels: 6
  Number of words: 4
CONTINUE(y/n)?
Last edited on
It loops but it won't let me enter a sentence the second time?
HOW DO YOU ADD THE CONTINUE LOOP??????????
When you get the single character asking for Y or N, cin accepts the character input but leaves a newline in the input buffer. When you start the loop, clean out the input buffer. Also, reset your counters.
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
#include <limits> // for std::numeric_limits
//...
    do
    {
        //reset your counters
        Uc = 0;
        Dc = 0;
        Vc = 0;
        Wc = 0;

        //clean out standard input before asking for more
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        cout << "Enter a sentence: ";
        cin.get(c);

        while(c != '\n')
        { 
            {
                if(isupper(c))  
                    Uc++;  
                if(isdigit(c)) 
                    Dc++; 
                if(vowels.find(c) != string::npos)
                    Vc++;
                if (isspace(c))
                    Wc++;

                cin.get(c);
            } 
        }
        cout << setfill ('.');
        cout << "  Number of uppercase letters: " << Uc << endl; 
        cout << "  Number of digits: " << Dc << endl; 
        cout << "  Number of vowels: " << Vc << endl;
        cout << "  Number of words: " << Wc << endl;

        cout << "CONTINUE(y/n)? ";
        cin >> response;

    } while(response == 'Y' || response=='y') ;
//... 
Last edited on
I'm not supposed to use include<limits> though
Then put an arbitrary size there. 80 is probably fine.
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
//...
    do
    {
        //reset your counters
        Uc = 0;
        Dc = 0;
        Vc = 0;
        Wc = 0;
 
        cout << "Enter a sentence: ";
        cin.get(c);

        while(c != '\n')
        { 
            {
                if(isupper(c))  
                    Uc++;  
                if(isdigit(c)) 
                    Dc++; 
                if(vowels.find(c) != string::npos)
                    Vc++;
                if (isspace(c))
                    Wc++;

                cin.get(c);
            } 
        }
        cout << setfill ('.');
        cout << "  Number of uppercase letters: " << Uc << endl; 
        cout << "  Number of digits: " << Dc << endl; 
        cout << "  Number of vowels: " << Vc << endl;
        cout << "  Number of words: " << Wc << endl;

        cout << "CONTINUE(y/n)? ";
        cin >> response;
        cin.ignore(80, '\n'); //get the newline out of the input buffer, too
    } while(response == 'Y' || response=='y') ;
//...  
Last edited on
I did it that way and it sorta worked but for some reason the vowels don't add up correctly, don't get why?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
I can count the number of upper case, lower case, vowels and words
Enter a sentence: Happy 2015 Valentine
  Number of uppercase letters: 2
  Number of digits: 4
  Number of vowels: 5
  Number of words: 2
CONTINUE(y/n)? y
Enter a sentence: Prepare Yourself for Exam No 1
  Number of uppercase letters: 4
  Number of digits: 1
  Number of vowels: 10
  Number of words: 5
CONTINUE(y/n)? N
Press any key to continue . . .
nvm counted wrong, thanks for all the help.
Last edited on
Topic archived. No new replies allowed.