c++ - uppercase letter input can't working in cpp programs -
i reading c++ right ,and got stuck in following program.
when provide string lower case letter, provides fine output, when go upper case letters, gets stuck after input.
here code:
`#include <iostream>` #include <stdio.h> #include "string.h" using namespace std; class base { public: int array(){ int i, n, p, z = 0; char g[50]; string c[50]; char abc; cout << "enter name :" << endl; cin >> g; = 0; while (g[i] != 0) if ((g[i] >= 'a' && g[i] <= 'z') || (g[i] <= 'a' && g[i] >= 'z')){ z++; i++; } cout << "name of " << z << " elements" << endl; { (p = 0; p < z; p++) cout << "a[" << p + 1 << "]=" << g[p] << endl; } cout << "enter element no.:"; cin >> n; if(n >0 && n <= z){ cout << "a[" << n << "]=" << g[n-1] << endl; } (p = 0; p < z; p++){ char integer_string[50]; int integer = p+1; sprintf(integer_string, "%d", integer); char other_string[50] = "g["; strcat(other_string, integer_string); strcat(other_string, "]"); c[p]= other_string; } cout << "enter character :"; cin >> abc; (p = 0; p < z; p++){ if(g[p] == abc){ cout <<abc<< "=a[" << p + 1 << "]"<< endl; break; } } return 0; } }; //-------------------------------------------------------------- int main(){ base b; b.array(); return 0; }
could tell me problem in program?
try changing
if ((g[i] >= 'a' && g[i] <= 'z') || (g[i] <= 'a' && g[i] >= 'z'))
to
if ((g[i] >= 'a' && g[i] <= 'z') || (g[i] >= 'a' && g[i] <= 'z'))
and deleting 3 ` , 2 in first line , 1 in last line.
update
adding #incude <cctype>
head of code , using
if (islower(g[i]) || isupper(g[i]))
is better. avoid depending character code.
if (isalpha(g[i]))
may work.
Comments
Post a Comment