2016-04-17 4 views
-2

ユーザー入力によって提供されるアレルギーに関する情報を記録するために、アレルギープログラム(以下のコードを参照)を開発しました。私は、ユーザーが所定の値に基づいてアレルギーの「重症度」を入力するための別のオプションを追加したいと考えています。クラスの列挙型を使用してユーザー入力の値オプションを指定する

ユーザーが選択しなければならない値を保持する列挙型を作成します。ここまでは私がこれまで持っていたことですが、enumについては無知であり、正確に実装する必要があるだけです。

Allergy.hpp:

#ifndef Allergy_hpp 
#define Allergy_hpp 

#include <iostream> 
#include <string> 
#include <list> 
using namespace std; 


class Allergy { 
public: 
    enum severity {mild, moderate, severe}; 

    Allergy(); 
    Allergy(string, string, list <string>); 
    ~Allergy(); 

    //getters 
    string getCategory() const; 
    string getName() const; 
    list <string> getSymptom() const; 


private: 

    string newCategory; 
    string newName; 
    list <string> newSymptom; 



}; 

#endif /* Allergy_hpp */ 

Allergy.cpp:

include "Allergy.hpp" 

Allergy::Allergy(string name, string category, list <string> symptom){ 
    newName = name; 
    newCategory = category; 
    newSymptom = symptom; 
} 

Allergy::~Allergy(){ 

} 

//getters 
string Allergy::getName() const{ 
    return newName; 
} 

string Allergy::getCategory() const{ 
    return newCategory; 
} 


list <string> Allergy::getSymptom() const{ 
    return newSymptom; 
} 

main.cppに:あなたはループを利用することができ

#include <iostream> 
#include <string> 
#include "Allergy.hpp" 

using namespace std; 


int main() { 
    string name; 
    string category; 
    int numSymptoms; 
    string symptHold; 
    list <string> symptom; 

    cout << "Enter allergy name: "; 
    getline(cin, name); 
    cout << "Enter allergy category: "; 
    getline(cin, category); 
    cout << "Enter number of allergy symptoms: "; 
    cin >> numSymptoms; 

    for(int i = 0; i < numSymptoms; i++){ 
     cout << "Enter symptom # " << i+1 << ": "; 
     cin >> symptHold; 
     symptom.push_back(symptHold); 
    } 

    Allergy Allergy_1(name, category, symptom); 
    cout << endl << "Allergy Name: " << Allergy_1.getName() << endl << 
    "Allergy Category: " << Allergy_1.getCategory() << endl << 
    "Allergy Symptoms: "; 

    for(auto& s : Allergy_1.getSymptom()){ 
     cout << s << ", "; 
    } 

    cout << endl; 
    return 0; 
} 

答えて

1

、およびif文のシリーズ文字列から列挙型の値を評価するいくつかの言語とは違って不幸にも、列挙型には名前の文字列がありません。これを行う方法のサンプルは次のようになります:

enum MyEnum{ 
    Value1, 
    Value2, 
    Value3 
}; 

int main() 
{ 
    bool isParsed=false; 
    std::string line(""); 
    MyEnum myEnum; 
    std::cout<<"Please enter your selection: "; 
    while(!isParsed&&std::getline(std::cin,line)){ 
     if(line == "Value1"){ 
      isParsed = true; 
      myEnum = Value1; 
     } 
     else if(line == "Value2"){ 
      isParsed = true; 
      myEnum = Value2; 
     } 
     else if(line == "Value3"){ 
      isParsed = true; 
      myEnum = Value3; 
     }else{ 
      std::cout<<"Selection invalid, please enter a new selection: "; 
     } 
    } 
} 
+0

ありがとう - これが私を助けました!私の列挙型の使用は正しいですか?私が見ているすべての例は、実際に宣言について話していますが、実装が実際にはわかりません。 – user1748681

+1

私は一般的にあなたのenumの使い方は良いと言います。そのような値の数値のリストを持つたびに、通常は列挙型でモデル化することができます。 (そうでなければ、 'int severity'のようなもので、_magic numbers_のかなりの量を使用している可能性があります。これは避ける方が一般的です。) –

関連する問題