2015-11-14 125 views
-1
//program8.cpp 
#include <iostream> 
#include "species.h" 
#include "reptilian.h" 
#include "mammalian.h" 
#include "insects.h" 

using namespace std; 

void VirtualPrint(species &typeofSpecies){ 
typeofSpecies.printme(); 
} 

void VirtualDanger(species &typeofSpecies){ 
typeofSpecies.showDanger(); 
} 

int main(int argv, char **args){ 

reptilian rep; 
insects ins; 
VirtualPrint(rep); 
VirtualPrint(ins); 
VirtualDanger(rep); 
VirtualDanger(ins); 
return 1; 
} 


//species.h 
#ifndef SPECIES_H 
#define SPECIES_H 
#include <iostream> 
#include <string> 

using namespace std; 

class species{ 
public: 
    species(); 
    virtual void printme(); 
    virtual void showDanger() = 0; 

protected: 
    string name; 
    string color; 
    int lifeExp; 
    bool thr; 
}; 

#endif 

//species.cpp 
#include <iostream> 
#include <string> 
#include "species.h" 

using namespace std; 

species::species(){ 
cout << "Please enter name of species:" << endl; 
getline(cin, name); 
cout << "Please enter color of species:" << endl; 
getline(cin, color); 
cout << "Please enter life expectancy of species in years:" << endl; 
cin >> lifeExp; 
cout << "Please enter if species is threat:true(1) or false(0)" << endl; 
cin >> thr; 
} 

void species::printme(){ 
cout << "Name: " << name << " Color: " << color << " Life Expectancy: " << lifeExp << " Threat: " << thr << endl; 
} 

//reptilian.h 
#ifndef REPTILIAN_H 
#define REPTILIAN_H 
#include <iostream> 
#include <string> 
#include "species.h" 

using namespace std; 

class reptilian : public species{ 
public: 
    reptilian(); 
    virtual void printme(); 
    virtual void showDanger(); 

protected: 
    int length; 
    int lethality; 
}; 

#endif 


//reptilian.cpp 
#include <iostream> 
#include "reptilian.h" 

using namespace std; 

reptilian::reptilian(){ 
cout << "Please enter length(inches): " << endl; 
cin >> length; 
cout << "Please enter lethality(0-100): " << endl; 
cin >> lethality; 
} 

void reptilian::printme(){ 
species::printme(); 
cout << " Length: " << length << " Lethality: " << lethality << endl;; 
} 

void reptilian::showDanger(){ 
cout << endl; 
if(thr == true){ 
    cout << "This species is a threat" << endl; 
    cout << "The name of the species is " << name << " has a color of " << color << ", has a life expectancy of " << lifeExp << " has a length of " << length << ", and a lethality of " << lethality << endl; 
} 
} 

這是我的程序代碼,它運行良好,如果有一個爬行動物物體。但是,當兩個被創建時,它不會取第二個對象的名字。當兩個昆蟲物體被製造時也會發生同樣的情況。因爲我試圖解決這個問題,首先我沒有測試哺乳動物對象尚未有人可以幫我解決這個問題嗎?

編輯: 輸出:

物種請輸入名稱:

SAM

請輸入種類的顏色:

橙色

請年內進入物種的壽命:

請輸入,如果物種的威脅:真(1)或假(0)

請輸入長度(英寸):

請輸入致死率(0-100):

請輸入物種名稱:

請輸入種顏色:

黃色

請在年內進入物種的壽命:

請輸入,如果物種是威脅:真(1)還是假(0)

請輸入長度(英寸):

請輸入殺傷力(0-100):

請輸入物種如何惡毒的是(0-100) :

名稱:sam顏色:橙色Life Expec天信:100威脅:0

長度:60致死:0

名稱:顏色:黃色壽命:20威脅:0

長度:10致死:0

毒蛇:0

+0

你能顯示你的輸出和錯誤嗎? – drum

回答

1

您的問題是使用getline>>運算符混合。從http://en.cppreference.com/w/cpp/string/basic_string/getline

當在空格分隔的輸入之後立即使用在int n之後; std :: cin >> n,getline消耗運算符>>留在輸入流上的結束字符,並立即返回。一個常見的解決方案是用cin.ignore(std :: numeric_limits :: max(),'\ n');忽略輸入行上的所有剩餘字符。在切換到面向行的輸入之前。

因此在cin >> lethality之後在cin流中留下換行符。在species的第二個實例中的getline看到此換行符並立即返回。

也請參閱本:std::cin:: and why a newline remains

爲了解決這個問題,改變你的來電getline使用cin >>方法。

相關問題