Dev c++----- I am having trouble getting this program to run correctly. I did it wrong I know. I am very new to this. I need some help understanding the different loops and why my program is messed up. Helping me fix it and putting some comments into it would help me a ton! I cant use shortcuts like formulas and stuff like that yet. Thanks!
#include <iostream>
using namespace std;
main()
{
char s[50];
int l;
int lowercase = 0;
int u;
int uppercase = 0;
int n;
int numbers = 0;
int o;
int other = 0;
int t;
int total;
// get string from the user
cout << " Enter a continuous string of characters with no blank spaces: " << endl;
cout << "(Example: ABCabc1234!@#$%)" << endl << endl;
cout << " Enter your string: ";
cin >> s;
cout << endl;
// loop through the string counting numbers, letters, & others
t = 0;
while (s[t] != 0)
{
if ((s[t] != ' '))
total++;
t++;
}
// the following is for lower case letters
l = 0;
while (s[l] != 0)
{
if ((s[l] >= 'a' && s[l] <= 'z'))
lowercase++;
l++;
}
// the following is for uppercase letters
u = 0;
while (s[u] != 0)
{
if ((s[u] >= 'A' && s[u] <= 'Z'))
uppercase++;
u++;
}
// the following is for numbers
n = 0;
while (s[n] != 0)
{
if ((s[n] >= '0' && s[n] <= '9'))
numbers++;
n++;
}
// the following is for other characters
o = 0;
while (s[o] != 0)
{ // I do not know what I have wrong on this one..
if ((s[o] >= '!' && s[o] <= '/'))
other++;
o++;
}
cout << "Your string has " << total << " characters" << endl;
cout << lowercase << " lower case letters" << endl;
cout << uppercase << " upper case letters" << endl;
cout << numbers << " numbers" << endl;
cout << other << " other characters" << endl;
The variable total is not initialised to zero.
The result for "other characters" could be calculated as other = total - lowercase - uppercase - numbers;
Possibly, rather than looping through the string multiple times, you could have just a single loop and place all the code to count different types within that loop.
Can you explain maybe or show me please? Thank you for your help! I cant do the other characters that way. I am really supposed to be using while, if, if else, and else statements.