program not running

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*Program to count the number of lowercase characters entered*/
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<ctype.h>
using namespace std;
int main()
{
    char ch;
    int count=0;
    cout<<"Enter Character: ";
    cin.get(ch);
    while(ch!='\n')
    {
        if(islower(ch))
        {
            count++;
        }
    }
    cout<<"\nThe number of lowercase characters are: "<<count<<"\n";
    system("pause");
    return 0;
}

The above code is not running as it should it is only asking for character i enter and nothing nothing happens.Please point out my mistake.
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
/*Program to count the number of lowercase characters entered*/
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<ctype.h>

using namespace std;
int main()
{
    char ch;
    int count=0;
    cout<<"Enter Character: ";
    cin.get(ch);
    while(ch!='\n')
    {
        if(islower(ch))
        {
            count++;
        }
        cin.get(ch); // Missing code
    }
    cout<<"\nThe number of lowercase characters are: "<<count<<"\n";
    system("pause");
    return 0;
}


You forgot to tell the program to input (ch) before the next loop, and therefore (ch) remains unchanged. (ch) is not the newline character and unchanged, so (while) condition is always true and therefore the program loops forever.
Last edited on
Thanks now it is working as it should.Yeah, one more question will it work with every input function like simple cin>>;or getchar()
Topic archived. No new replies allowed.