What is wrong with code

Bellow code is only counting other infact it count many characters (upper,lower,digits) as others.i want a simple beginner program which count lower,upper,digits,space,punc etc..

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
#include <fstream>
#include <iostream>
#include <conio.h>
#include <ctype.h>
using namespace std;

int main ()
{
char c;
int lc=0;
int uc=0;
int dc=0;
int wc=0;
int pc=0;
int ot=0;
cout<<"Please Enter a character string : ";
cin>>c;
while(c=getchar()!='\n')
{
if(islower(c))
lc++;
else if(isupper(c))
uc++;
else if(isdigit(c))
dc++;
else if(isspace(c))
wc++;
else if(ispunct(c))
pc++;
else ot++;}
cout<<"\nTotal Number of lower case are :"<<lc;
cout<<"\nTotal Number of Upper case are :"<<uc;
cout<<"\nTotal Number of digits are :"<<dc;
cout<<"\nTotal Number of spaces are :"<<wc;
cout<<"\nTotal Number of punctuations are :"<<pc;
cout<<"\nTotal Number of other character are :"<<ot;

   return 0;
}
Last edited on
You have an extra } in your program. You want the loop to end on line 21 or 30?

You need to add parentheses on line 18
 
while((c=getchar())!='\n')
otherwise it will be treated as
 
while(c=(getchar()!='\n'))
Last edited on
i figured it out there was not parenthesis around c=getchar() it was like(c=getchar()).but now this code is not count first character if i type SS it counting 1 uper case if i ss it counting 1 lower case ..if i type ssSS it count 1 lower and 2 uper case.if i type SSss it counting 1uper and 2 lower case..
Last edited on
The first char is consumed by cin>>c; on line 17.
you mean you prefer using get() function?
See to the code below, it might help you
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
// #include <fstream> You dont need this
#include <iostream>
//#include <conio.h>
#include <cctype> //#include <ctype.h>
#include <string>
using namespace std;
int main ()
{
//use descriptive naming
    int lowerCount = 0;
    int upperCount = 0;
    int digitCount = 0;
    int wspaceCount = 0;
    int puntCount = 0;
    int othersCount = 0;
    string str;
    cout<<"Please Enter a character string : ";
    //use getline() for strings
    getline(cin, str); // cin>>c;
    for(unsigned int x = 0; x < str.size(); ++x)
    {
        char c = str[x];
        if(islower(c)) ++lowerCount;
        else if(isupper(c)) ++upperCount;
        else if(isspace(c)) ++wspaceCount;
        else if (isdigit(c)) ++digitCount;
        else if(ispunct(c)) ++puntCount;
        else ++othersCount;
    }
    cout <<"Total Number of lower case are: " << lowerCount << endl;
    cout <<"Total Number of Upper case are: " << upperCount << endl;
    cout <<"Total Number of digits are: " << digitCount << endl;
    cout <<"Total Number of spaces are: " << wspaceCount << endl;
    cout <<"Total Number of punctuations are: "<< puntCount << endl;
    cout <<"Total Number of other character are: " << othersCount << endl;;

   return 0;
}
aah using cin.get(c); also consuming first character..why it consuming first letter?
What is the purpose of that line?
Thanx peter and bazeto assignment is working now but i have a question that why Cin>>c and cin.get(c); are consuming first letter of string?
Both of them read the next character from the standard input stream and store the value in c. It is the first read operation in your program so the next character is also the first.
cin and get are both used to read from stream. so it is not an error for cin to read the first character. the program just worked the way you sequenced the instructions.
Line 17, takes 1 character away form the string
Topic archived. No new replies allowed.