わかった。私はダミーのために C++ を読んでいて、彼らがオブジェクト指向について話し合っているところまで来ました。学んだ概念からコードを書こうとして失敗した後、本からコードをほとんどコピーしました。コードの要点は、オブジェクト GoodPen および BadPen を記述する列挙型を使用して、Pen というクラスを作成することです。Color と Pentype という名前の列挙変数を作成し、それぞれに個別のオプションを設定しました。これが問題のようです。オブジェクトに値を割り当てますが、何らかの理由で、割り当てられた値を計算すると、実際の値ではなく、各値の数値配列の場所が返されます。ここにコードがあります
pen.h ヘッダー ファイル:
#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 ファイル:
#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;
}
申し訳ありませんが、私のcodespeakがひどいことは知っています。私は初心者であり、おそらく初心者の間違いを犯していることを認めます. pen.h コードは Pen クラスを作成してプロパティを割り当て、main.cpp ファイルは Pen クラスのオブジェクトを作成し、これらのオブジェクトにプロパティを割り当てます。ただし、オプション「黒」が選択されている場合、これは出力です。
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 . . .
ありがとうございました。そして小説でごめんなさい。:P
PSコンパイルにVisual Studioを使用しています。