Need help with college assignment!

Feb 16, 2013 at 7:21pm
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;

system("pause");

return 0;
}
Last edited on Feb 16, 2013 at 7:21pm
Feb 16, 2013 at 7:41pm
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.

Feb 16, 2013 at 11:21pm
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.
Last edited on Feb 16, 2013 at 11:22pm
Feb 17, 2013 at 12:19am
One thing you could do, if you must do things without using simple arithmetic, is to use something like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
if ()
{

}
else if ()
{

}
else if ()
{

}
else
{

}

where the final else will trap any character which was not caught by any of the preceding if statements.

If I say too much, I will have to write out the entire code for you, which improves my coding skills, not yours.

But I will repeat my previous suggestion, use a single loop such as this one:
1
2
3
4
5
6
7
8
9
10
t = 0;
while (s[t] != 0)
{
    // Place all of your tests
    // and counts of different characters
    // here    


    t++;
}


Topic archived. No new replies allowed.