Need Help with my code please!!!!

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



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <iomanip>
using namespace 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;
}
Last edited on
Hey, please use code tags - http://www.cplusplus.com/articles/jEywvCM9/

you have to type everything twice because thats what you are telling your code to do.

1
2
3
4
cout << "What is the current population?\n";
	cin >> population;

	while (!(cin >> test) || population < 2)


Even if you have the cin>> in the while loop, it will still works the same way. what are you trying to do there?
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
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
#include<iostream>
using namespace 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?
Topic archived. No new replies allowed.