有人可以帮我解决这个问题吗?
//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
您的问题是使用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 >>
方法。
你能显示你的输出和错误吗? – drum