有人可以帮我解决这个问题吗?
Can someone help me solve this issue?
//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;
}
}
这是我的程序代码,如果制作了一个爬行动物对象,它运行良好。但是,当创建两个对象时,它不会采用第二个对象的名称。当制作两个昆虫对象时也会发生同样的情况。我还没有测试哺乳动物物体,因为我正在尝试先解决这个问题
编辑:输出:
请输入物种名称:
山 姆
请输入物种颜色:
橙
请输入物种的预期寿命(以年为单位(:
100
请输入物种是否为威胁:真(1(或假(0(
0
请输入长度(英寸(:
60
请输入杀伤力(0-100(:
0
请输入物种名称:
请输入物种颜色:
黄色
请输入物种的预期寿命(以年为单位(:
20
请输入物种是否为威胁:真(1(或假(0(
0
请输入长度(英寸(:
10
请输入杀伤力(0-100(:
0
请输入物种的毒性(0-100(:
0
姓名:山姆 颜色:橙色 预期寿命: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((, ''(;在切换到面向线路的输入之前。
因此,在cin >> lethality
之后,cin
流中会留下换行符。species
的第二个实例中的getline
看到此换行符并立即返回。
另请参阅以下内容:std::cin::以及为什么保留换行符
若要解决此问题,请将getline
调用更改为使用 cin >>
方法。
- Project Euler问题4的错误解决方案
- 在java中解决这段代码时面临循环中的问题
- C++:Application.cpp中抛出了未解析的外部符号(解决方案在问题的末尾,供未来的读者参考)
- 难以理解某些人解决IOI问题的源代码
- 节俭并发:未解决的外部问题
- IpOpt拒绝解决不受约束的问题
- 如何解决这个超硬恒星的创造问题
- 循环无限运行C++解决骑士之旅问题
- 使用 bfs 解决连接组件问题时得到错误的答案
- 如何解决 Ninja c++ 构建和执行问题
- 无法在问题解决方案中执行输出逻辑
- 如何在不到O(N)的时间内解决这个问题?
- Arduino 用于语句错误。令牌之前的预期')' ';'。如何解决这个问题?
- 移动赋值运算符;尝试引用已删除的函数.我该如何解决这个问题?
- 如何在不使用额外空间的情况下解决这个问题?
- 我无法在Visual Studio代码中使用CIN输入答案,它说输入您的年龄,但它说只读文本编辑器如何解决这个问题?
- 最大的回文产品 - 程序未运行,编写解决方案但无法理解问题
- 如何解决"no Qt platform plugin could be initialized"问题?
- 如何解决可能的唯一组合问题
- Windows上的模板有什么问题?解决方案是什么