Enum Problem

Hi, I am writing a program to determine whether a letter in a paragraph is upper,lower,space or punc, I have the code mostly written and I am having problems using enums...YES THIS IS HOMEWORK, I am not asking for someone to write code, I am asking for why the isUpper and isLower and isPunc and isSpace are giving me problems, look at my code, I always get unkown as the result its not correctly determining that something is lower, space, or punc, however, it determines upper just fine..


here is my text.dat file:
All components of an array must be of the same data type; while not extreme, this
is a serious limitation since there are many situations in which this is not possible.
Fortunately, most programming languages provide another structed data type,
structures, which allow heterogeneous information to be stored, accessed, and
manipulated. A structure contains fields, which can be of different data types.


here is my code:

#include <iostream>
#include <fstream>

using namespace std;
enum cTypes {upper,lower,punc,space,unknown};
cTypes charType(char);

int main()
{
      char       charArray[500];
      char       charTemp;
      int             intCurrent;
      int             intSize;
      cTypes   cType;
      int             intLowerCount,intUpperCount,intPuncCount, intSpaceCount;
      ifstream inFile;
      bool     error;
      error  = false;

      inFile.open("text.dat");

      if (!inFile)
            error = true;
            
      if (!error)
      {
            inFile.get(charTemp);
            charArray[0] = charTemp;
            intCurrent = 1;

            while (!inFile.eof())
            {
                  inFile.get(charTemp);
                  charArray[intCurrent] = charTemp;
                  intCurrent++;
            }
            intSize = intCurrent;

            for (intCurrent = 0;intCurrent<intSize;intCurrent++)
            {
                   cType = charType(charArray[intCurrent]);
                   cout << cType;
            }
      }
      else
      {
            cout << "Error opening text.dat!";
            cout << endl;
      }

      system("PAUSE");
      return 0;
}

cTypes charType(char inChar)
{
      cTypes retVal;

            if (isupper(static_cast<int>(inChar)) == 1)
                  retVal = upper;
            else if (isspace(static_cast<int>(inChar)) == 1)
                  retVal = space;
            else if (islower(static_cast<int>(inChar)) == 1)
                  retVal = lower;
            else if (ispunct(static_cast<int>(inChar)) == 1)
                  retVal = punc;
            else
                  retVal = unknown;

      return retVal;
}
LVL 19
BrianGEFF719Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

BrianGEFF719Author Commented:
i've also tried the following ways:


isspace(inChar) == true
isspace(static_cast<int>(isChar)) == true
...etc


??
0
WoodsterCommented:
The functions you are using are only defined to return a non-zero value if true.  A non-zero value is not necessarily going to be 1.

Try using :

if (isupper(inChar))
  retVal = upper;
else if (isspace(inChar))
  retVal = space;
else if (islower(inChar))
  retVal = lower;
else if (ispunct(inChar))
  retVal = punc;
else
  retVal = unknown;

Also, inChar should be implicitly cast to an int from a char so you do not need to explicitly cast it although slomeone here may jump up and down and tell me that it should be explicitly cast or something.
0
WoodsterCommented:
Also, If confronted with problems like this, you may even want to add something like the following in order to find out why you are getting a certain result.

int testResult;

testResult = isupper(inChar);
testResult = islower(inChar);
testResult = isspace(inChar);
testResult = ispunct(inChar);

and then step through your code one line at a time and check the result that is being returned by each function.

(You could also declare 4 separate variables to store the 4 separate results if required(
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

SteHCommented:
Why do you try to get the C functions to work. STL contains isspace etc and works on chars.
0
SteHCommented:
The STL versions are returning a bool, so you can safely test for true:

if (true == isspace (inChar)) {
   ...
}
0
WoodsterCommented:
SteH,

Not sure if you are referring to me, Brian or both.

My comment was just using C functions as I am not familiar at all with templates and the C functions should do the job.  My only experience with templates in the past (std::map or something like that) turned out to be non portable between platforms so I quickly dumped them.
0
SteHCommented:
Woodster,

the first comment referred to Brian. I haven't seen yours by then. The second post was a remark to your correct comment about the return value.

To STL: Now it is part of the C++ standard and should be portable. But you are right that in the past the implementation was not fully compliant: VC++ did not implement it (at first) and BCB found their own way. And since Brians headers are clearly STL ones (no .h) isspace etc should be used from STL as well, I think. Only the cast to a char he used is pointing to the C functions.

BrianGeff,
I tested your code on Dev-Cpp and it worked without a problem, I just had to create a text.dat file. It might be that you need a header where the STL isspace is located.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.