Can someone help me why when I run my code it ask for population, rate and days 2 times. Im just trying to make it so you cant put in a string or decimal values but I cant seem to figure it out
#include <iostream>
#include <iomanip>
usingnamespace std;
double endpop(int days, float rate, int population, int a);
int main()
{
int days;
int population;
float rate;
int endpopulation = 0;
int startpop;
int test;
cout << "What is the current population?\n";
cin >> population;
while (!(cin >> test) || population < 2)
{
if (!(cin >> test))
{
cout << "Please specify a number larger than 1.\n";
cin.clear();
cin.ignore(100, '\n');
}
}
cout << "What is the growth rate? Use 10 for .1 no decimals.\n";
cin >> rate;
while (!(cin >> test) || rate < 0 )
{
if (!(cin >> test))
{
cout << "Please use a number greater than 0\n";
cin.clear();
cin.ignore(100, '\n');
}
}
cout << "For how many days will this increase take place?\n";
cin >> days;
while (!(cin >> test) || days < 1)
{
if (!(cin >> test))
{
cout << "Please specify a number larger than zero.\n";
cin.clear();
cin.ignore(100, '\n');
}
}
cout << "" << endl;
cout << "DAYS BEGINING POPULATION END DAY POPULATION\n";
cout << "----------------------------------------------------\n";
startpop = population;
for (int a = 1; a <= days; a++)
{
cout << a << " " << startpop;
endpopulation = endpop(days, rate, population, a);
cout << " " << endpopulation << endl;
startpop = endpopulation;
}
return 0;
}
double endpop(int days, float rate, int population, int a)
{
int endpopulation = (population * a) * (rate * .01) + population;
return endpopulation;
}
im just tring to get a user input and if its lets say, "12.123" then I want it to say invalid response or "asdfasdf" same thing and then make the user put a valid response
#include<iostream>
usingnamespace std;
//Sample of how to do what I think you are trying to accomplish.
int userinput = 0;
cout << "Enter your input user..: "; cin >> userinput;
while(userinput >12)
{
cout << "Invalid response..try again" << endl;
cout << "Your input: " cin >> userinput;
}
Also, you do realize that if you set the users response to be stored in an integer, than you will never get a decimal right?