有人可以帮我解决这个问题吗?

Can someone help me solve this issue?

本文关键字:问题 解决      更新时间:2023-10-16
//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 >> 方法。