Loop question

I am running into a issue with the loops. I need the program to give the user two chances to enter the correct variables. If they fail the second time it closes the program. However, it also closes the program if they input the correct variables without doing the for loop. The for statement is supposed to utilize the equation and give 25 years worth of data 0-25years. How do I make the loop not kick me out so the for statement will start?

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
#include <iomanip>
#include <iostream>
using namespace std;

int main(void)
{
  double CurrentYr, NextYr, Rate;
   
    cout << "Enter the initial egret population: \n";
    cin >> CurrentYr;
    cout << "Enter the Rate: \n";
    cin >> Rate;

    while ((CurrentYr>=0&&CurrentYr<=1000000)&(Rate<=4&&Rate>=0)) // if the input is correct
         {
            cout << "Enter the initial egret population: \n";
            cin >> CurrentYr;
            cout << "Enter the Rate: \n";
            cin >> Rate;
            cout << "\n Year    Population\n";
            cout << "----    ----------\n";
            NextYr=Rate*CurrentYr*(1-(CurrentYr)/1000000)
               for(NextYr=0; NextYr=<25; NextYr++)
                {
	         cout << setw(4) << right <<"";
                }
          
    if ((CurrentYr<0||CurrentYr>1000000)||(Rate>4||Rate<0)) // first trail
   {
     cout << "Enter a valid input range greater than 0 and less than 1000000 for population: \n";
     cout << "Enter a valid input range greater than 0 and less than 4 for rate \n";
     cout << "Enter the initial egret population: \n";
     cin >> CurrentYr;
     cout << "Enter the Rate: \n";
     cin >> Rate;
   
         if ((CurrentYr<0||CurrentYr>1000000)||(Rate>4||Rate<0))
         {			   
    	   cout << "The program will now close next time enter in the correct variables. \n";// last attempt
         }
   }
	 }
    cout << endl;
    cin.ignore();
return 0;
}
OK, now i am running into the exact opposite problem now my if statement will not work but the end output will
lol, programming is great right?

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
#include <iomanip>
#include <iostream>

using namespace std;

int main(void)
{
  double CurrentYr, NextYr, year; //making the variables
  float Rate;
  year=0;

    cout << "Enter the initial egret population: \n";
    cin >> CurrentYr;
    cout << "Enter the Rate: \n";
    cin >> Rate;

if((CurrentYr<=0&&CurrentYr>=1000000)&&(Rate>4&&Rate<=0))  // first trail
   {
     cout << "Enter a valid input range greater than 0 and less than 1000000 for population: \n";
     cout << "Enter a valid input range greater than 0 and less than 4 for rate \n";
     cout << "Enter the initial egret population: \n";
     cin >> CurrentYr;
     cout << "Enter the Rate: \n";
     cin >> Rate;
  
     if((CurrentYr<0||CurrentYr>1000000)||(Rate>4&&Rate<0))
         {              
           cout << "The program will now close next time enter in the correct variables. \n";// last attempt
         }
   
   }
     else 
         {
	   cout << "\n Year    Population\n"; // this is making the record over 25 years
            cout << " ----    ----------\n";
            for(year=0; year<=25; year++)
	      {
		NextYr=Rate*CurrentYr*(1-(CurrentYr)/1000000);
	        cout << setw(4) << year << setw(15) << CurrentYr<< endl;
	        CurrentYr = NextYr;
	      }
	 }
   
    cout << endl;
    cin.ignore();
return 0;
}
try to put it like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
if((CurrentYr<=0&&CurrentYr>=1000000)&&(Rate>4&&Rate<=0))  // first trail
   {
     cout << "Enter a valid input range greater than 0 and less than 1000000 for population: \n";
     cout << "Enter a valid input range greater than 0 and less than 4 for rate \n";
     cout << "Enter the initial egret population: \n";
     cin >> CurrentYr;
     cout << "Enter the Rate: \n";
     cin >> Rate;
  
     else if((CurrentYr<0||CurrentYr>1000000)||(Rate>4&&Rate<0))
         {              
           cout << "The program will now close next time enter in the correct variables. \n";// last attempt
         }
sadly the if else statement doesn't work it still just compiles the program with the negative
I just dont understand why it doesnt work, becuase it was working fine earlier
closed account (48T7M4Gy)
What's you latest code?
just create a loop counter variable.. if that variable becomes 2 then end the program...
This is the latest code
i still have no clue why my if statements earlier do not work though becuase they were what i wrote first and worked perfectly

the code below does work there are a couple things missing but it is mostly the same
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
int main(void)
{
	double CurrentYr, NextYr, year; //making the variables
	double count = 0;
	float Rate;
	year=0;	
	cout << "Enter the initial egret population: \n";
	cin >> CurrentYr;
	cout << "Enter the Rate: \n";
	cin >> Rate;
	while(count != 2)
	{ 
		if((CurrentYr<0||CurrentYr>10)||(Rate>4||Rate<0))
		{
			double x;
			CurrentYr = 0;
			Rate = 0;
			cout << "Please enter a value greater than 0, but less than 11 for 'Egret Population' and a value between 0-4 for 'Rate'" << endl;
			cout << "Enter the initial egret population: \n";
			cin >> CurrentYr;
			cout << "Enter the Rate: \n";
			cin >> Rate;
			count++, x++;			
			if(x = 2)
			{
				return 0;
			}
		}
		else
		{
			count++;
		}
	}   
Last edited on
Hey this may help check out line 24
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
int main(void)
{
	double CurrentYr, NextYr, year; //making the variables
	double count = 0;
	float Rate;
	year=0;	
	cout << "Enter the initial egret population: \n";
	cin >> CurrentYr;
	cout << "Enter the Rate: \n";
	cin >> Rate;
	while(count != 2)
	{ 
		if((CurrentYr<0||CurrentYr>10)||(Rate>4||Rate<0))
		{
			double x;
			CurrentYr = 0;
			Rate = 0;
			cout << "Please enter a value greater than 0, but less than 11 for 'Egret Population' and a value between 0-4 for 'Rate'" << endl;
			cout << "Enter the initial egret population: \n";
			cin >> CurrentYr;
			cout << "Enter the Rate: \n";
			cin >> Rate;
			count++, x++;			
			if(x == 2)   /////////////////////<------------ !if(x=2) a single '=' is for assignment not comparison
			{
				return 0;
			}
		}
		else
		{
			count++;
		}
	}
Last edited on
Tell me if that works
the latest code i posted worked I was just giving it so people could see what I changed, the main code which I could never get to work was the original with the two if statements
Topic archived. No new replies allowed.