C++ while loop, using if else statements to count in an array -


i'm having trouble basic c++ program assignment, , appreciate help. assignment follows:

write program accepts input keyboard (with input terminated pressing enter key) , counts number of letters (a-z , a-z), numerical digits (0-9), , other characters. input string using cin , use following looping structure examine each character in string "if" statement , multiple "else if" statements.

my program far is:

#include <iostream> using namespace std;  int main() { char s[50]; int i; int numlet, numchars, otherchars = 0;  cout << "enter continuous string of       characters" << endl; cout  << "(example: abc1234!@#$%)" <<      endl; cout  << "enter string: "; cin  >> s;  = 0; while (s[i] != 0) // while character not have ascii code 0 { if ((s[i] >= 'a' && s[i] <= 'z') || s[i] >= 'a' && (s[i] <= 'z'))   {numlet++;   } else if (s[i] >= 48 && s[i] <= 57) {numchars++;     } else if ((s[i] >= 33 && s[i] <= 4) || (s[i] >= 58  && s[i] <=64) ||  (s[i] >= 9 && s[i] <= 96) || (s[i]   >= 123 && s[i] <= 255))   {otherchars++;   }   i++; }  cout  << numlet << " letters" << endl; cout << numchars << " numerical characters" << endl; cout << otherchars << " other characters" << endl;  return 0; } 

the letter count gives value little low, , number count gives large negative number. other chars seems function fine.

as mentioned in other answer, need initialize variables, have error in code:

if ((s[i] >= 'a' && s[i] <= 'z') || s[i] >= 'a' && (s[i] <= 'z')) 

the brackets wrong. , result, not counting lowercase letters (i think) anyways shoul (indented visibility):

 if (       (s[i] >= 'a' && s[i] <= 'z') ||       (s[i] >= 'a' && s[i] <= 'z')       ) 

you can use this. since you're using c++ , not c, right ;) (people here mad difference apparently)


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -