1

okay. I'm reading through C++ for dummies, and I'm at the point where they're discussing Object Orientation. I've all but copied the code from the book after trying, and failing, to write the code from concepts I've learned. The gist of the code is to create a class called Pen, with enumerations to describe the objects GoodPen and BadPen. I've created the enumerated variables called Color and Pentype, and have put separate options for each. This seems to be the problem. I assign the values for the objects, but for some reason when I cout the assigned values, they are returning the numerical array locations of each of the values, instead of their actual values. here is the code

pen.h header file:

#ifndef PEN_H_INCLUDED
#define PEN_H_INCLUDED

using namespace std;

enum Color
{
    red,
    blue,
    yellow,
    green,
    black,
    gray
};

enum PenType
{
    ballpoint,
    fountain,
    felttip,
    flammable
};

class Pen
{
public:
    Color InkColor;
    Color ShellColor;
    Color CapColor;
    PenType PenType;
    double length;
    double inklevel;
    string brand;

    void write(string words){
        if(inklevel <= 0){
            cout << "Uh-Oh, you're out of ink!" << endl;
        } else {
            cout << words << endl;
            inklevel -= words.length();
        }

    }

    void explode(){
        cout << "You used explosive ink, the tip became heated via friction with the paper" << endl << " and the pen exploded, killing you and your family..." << endl;
        inklevel = 0;
    }

};

#endif // PEN_H_INCLUDED

main.cpp file:

#include<iostream>
#include<string>
#include "pen.h"

using namespace std;

extern void explode();

int main(){

    string inpt;
    Pen GoodPen;
    Pen BadPen;




    GoodPen.brand = "OfficeDepot";
    GoodPen.CapColor = black;
    GoodPen.InkColor = gray;
    GoodPen.ShellColor = gray;
    GoodPen.PenType = ballpoint;
    GoodPen.length = 6;             //inches
    GoodPen.inklevel = 100;         //percent

    BadPen.brand = "Staples";
    BadPen.CapColor = red;
    BadPen.InkColor = red;
    BadPen.ShellColor = red;
    BadPen.PenType = flammable;
    BadPen.length = 6.66;           //inches
    BadPen.inklevel = 100;          //percent

    cout << "You have a choice: black pen or red pen. Choose wisely. ";
    getline(cin, inpt);
    if(inpt == "black" || inpt == "Black"){
        cout << "You picked the right pen. The ink level is " << GoodPen.inklevel << "%" << endl;
        cout << "The pen is " << GoodPen.length << " inches long, it is a " << GoodPen.PenType << " from " << GoodPen.brand << endl;
        cout << "The cap is " << GoodPen.CapColor << " and the shell is " << GoodPen.ShellColor << endl;
        cout << "The ink is " << GoodPen.InkColor << endl;
    }else if(inpt == "red" || inpt == "Red"){
        //explode();
        cout << "You picked the wrong pen. The ink level is " << BadPen.inklevel << endl;
        cout << "The pen is " << BadPen.length << " inches long, it is a " << BadPen.PenType << "from " << BadPen.brand << endl;
        cout << "The cap is " << BadPen.CapColor << " and the shell is " << BadPen.ShellColor << endl;
        cout << "The ink is " << BadPen.InkColor << endl;

    }

    return 0;
}

sorry, I know that my codespeak sucks. I acknowledge that I am a beginner and am probably making a beginner mistake. the pen.h code creates the Pen class and assigns it properties, the main.cpp file creates objects for the Pen class, and assigns properties to these objects. but, this is the output, if the option "black" is chosen:

You have a choice: black pen or red pen. Choose wisely. black
You picked the right pen. The ink level is 100%
The pen is 6 inches long, it is a 0 from OfficeDepot
The cap is 4 and the shell is 5
The ink is 5
Press any key to continue . . .

Thank you, very much, for the time. And sorry for the novel. :P

PS I am using Visual Studio for compilation.

1 Answers1

0

No "class issues" here.

Internally, enum in C/C++ are just integers. The labels are changed by the compiler to their (0-based) integer value. In fact, you could do

GoodPen.PenType = 2;

and the compiler would be ok with that.

So, getting the String representation of an enum requires that you produce it; v.g. How to convert enum names to string in c

Community
  • 1
  • 1
SJuan76
  • 24,532
  • 6
  • 47
  • 87
  • Update, an example that approachs more what you want at the cost of biggest complexity (not worth the cost IMHO) http://stackoverflow.com/questions/147267/easy-way-to-use-variables-of-enum-types-as-string-in-c/147283#147283 – SJuan76 Oct 17 '13 at 00:48
  • haha update is way* over my head at the moment. but, the options offered in the first post make sense. Just seems like a lot of unnecesary complexity to have to essentially run the outstreamed variables through a converter to get from an integer to a string. – Mitchell Gray Edwards Oct 17 '13 at 00:59
  • okay, so if I wanted to run the enum (or integer) through a enum->string converter, would I put it in the cout statement, or in the object assignments? like. if I wrote a converter that took the enumeration, used it as an index marker in an index with the available options for the object property in question, should I run the enumeration through the converter in the property assignments, or in the cout statement? haha i'm clueless here – Mitchell Gray Edwards Oct 17 '13 at 01:21
  • In the `cout`. Keeping `Pen.PenType` as a member of type `PenType` gives more information about its possible values than, say `char *`. – SJuan76 Oct 17 '13 at 08:42