我需要在我的计划中验证我的Population
,birth
和death
。我希望Population
始终开始,永远不会被允许低于2
。对于birth
和death
,我从不希望它们小于0
。我该如何完成这项任务?如果您需要任何澄清,我可以提供。我是这个网站的新手。
#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
using namespace std;
class Population
{
private:
int current_pop;
int Births;
int annuel_births;
int deaths;
int annuel_deaths;
public:
void setpop(int);
int getpop(int);
void setborn(double);
void setdead(double);
double getdead(float deaths, int current_pop)
{
return deaths / current_pop;
};
double getborn(float Births, int current_pop)
{
return Births / current_pop;
};
};
void menu();
void Population::setpop(int people)
{
current_pop = people;
if (people < 2)
{
people = 2;
}
}
int Population::getpop(int people)
{
return current_pop;
}
void Population::setborn(double baby)
{
Births = baby;
if (baby < 0)
{
baby = 0;
}
}
void Population::setdead(double dead)
{
deaths = dead; // i tried to add the validation here but it still allows me to change it //below 0 same thing with the other validations
if (dead < 0)
{
dead = 0;
}
}
int main()
{
double Births = 0;
double Deaths = 0;
int people = 2;
Population location;
char choice;
cout << "Welcome to the population program.\nPlease select one of the menu items by entering a value 1-8. \n1 . Enter population, Annuel Births, Annuel Deaths.\n2 . Change Population.\n3 . Change Annuel Births.\n4 . Change annuel Deaths.\n5 . View current Population.\n6 . View Birthrate.\n7 . View DeathRate. \n8 . exit program." << endl;
do
{
const char Max_Choice = '8';
char getChoice(char);
choice = getChoice(Max_Choice);
switch (choice)
{
case '1': cout << "This is the town of Kakariko Village.\nPlease input the current population: "; cin >> people;
cout << "input the annuel number of births: "; cin >> Births;
cout << "input the annuel deaths in this village: "; cin >> Deaths;
cout << "==========================================" << endl;
menu();
char getChoice(char);
break;
case '2': cout << "ok you wish to change population!\nThis is the town of Kakariko Village.\nPlease input the current population: "; cin >> people;
cout << " " << endl;
menu();
char getChoice(char);
location.setpop(people); // calling function with variable as argument
cout << "current populaion = " << people << endl;
break;
case '3': cout << "you wish to change the number of annuel births\ninput the annuel number of births: "; cin >> Births;
location.setborn(Births);
cout << "your birthrate is: " << Births << endl;
cout << " " << endl;
menu();
char getChoice(char);
break;
case '4': cout << "ok you wish to change the annuel number of deaths\ninput the annuel deaths in this village: "; cin >> Deaths;
location.setdead(Deaths);
cout << " your death rate is: " << Deaths << endl;
cout << " " << endl;
menu();
char getChoice(char);
break;
location.setpop(people);
case'5': cout << "ok you wish to view population it's: " << people << endl;
break;
case'6':cout << "ok you wish to view the birthrate it's: " << location.getborn(Births, people) << endl;
cout << " " << endl;
menu();
char getChoice(char);
break;
case'7':cout << "ok you wish to view the deathrate it's: " << location.getdead(Deaths, people) << endl;
cout << " " << endl;
menu();
char getChoice(char);
break;
case'8':
return 0;
break;
}
} while (choice != '8'|| choice != '1'); cin >> choice;
system("pause");
return 0;
}
char getChoice(char max)
{
char choice = cin.get();
cin.ignore(256, '\n');
cin.clear();
while (choice < '1' || choice > max)
{
cout << "choice must be between 1 and " << max << ". "
<< "enter choice again.: "; choice = cin.get();
cin.ignore();
}
return choice;
}
void menu()
{
cout << "Please select one of the menu items by entering a value 1-8. \n1 . Enter population, Annuel Births, Annuel Deaths.\n2 . Change Population.\n3 . Change Annuel Births.\n4 . Change annuel Deaths.\n5 . View current Population.\n6 . View Birthrate.\n7 . View DeathRate. \n8 . exit program." << endl;
}
答案 0 :(得分:0)
检查您的功能setDead()
void Population::setdead(double dead)
{
deaths = dead;
// i tried to add the validation here but it still allows me to change it
//below 0 same thing with the other validations
if (dead < 0) // ???
{
dead = 0; // ???
}
}
您实际上正在更改函数的参数,而您应该验证类的数据成员。