?
Solved

Enum Problem

Posted on 2004-11-30
7
Medium Priority
?
365 Views
Last Modified: 2008-03-06
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;
}
0
Comment
Question by:BrianGEFF719
  • 3
  • 3
7 Comments
 
LVL 19

Author Comment

by:BrianGEFF719
ID: 12703842
i've also tried the following ways:


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


??
0
 
LVL 4

Expert Comment

by:Woodster
ID: 12703904
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
 
LVL 4

Accepted Solution

by:
Woodster earned 2000 total points
ID: 12703921
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Expert Comment

by:SteH
ID: 12703927
Why do you try to get the C functions to work. STL contains isspace etc and works on chars.
0
 
LVL 13

Expert Comment

by:SteH
ID: 12703946
The STL versions are returning a bool, so you can safely test for true:

if (true == isspace (inChar)) {
   ...
}
0
 
LVL 4

Expert Comment

by:Woodster
ID: 12703953
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
 
LVL 13

Expert Comment

by:SteH
ID: 12704031
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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question