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.