Question about do while loop

Hi everyone. i have wrong result for the variable year. when i press 2000 the result should be correct but instead it give me the both outcome. I am beginner for c++can anyone help ?
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
#include <iostream>
using namespace std;
int Year;
bool c3;
int main()
{
	do
	{
		cout << "Please enter in a four(4) digit number for the year." << endl;
		cin >> Year;
		c3 = (Year >= 1812) && (Year <= 2012);


		if (c3 != Year)
		{
			cout << "Value of Year not in the range" << endl;
		}
		
	} while (true);
	{
		cout << "correct" << endl;

	}
	system("pause");
}
You have a couple issues with your code that I can see right away.

the biggest thing is is line 21 will never run, the way this is coded.
other than that it is just nit picky stuff that is more about my opinion than
actual good information.

Below is how I would write what your trying to do.


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
#include <iostream>
using namespace std;
int Year;
bool c3;
int main()
{

    bool run_menu = true;
    while (run_menu) {
        cout << "Please enter in a four(4) digit number for the year." << endl;
        cin >> Year;
        c3 = (Year >= 1812) && (Year <= 2012);
        
        if (!c3) // if c3 is false
        {
            cout << "Value of Year not in the range" << endl;
        }
        if (c3) // if c3 is true
        {
         cout << "correct" << endl;
            run_menu =false;
        }

    }
    
    


let me know if you have any questions.
Here I fixed it for you I hope that this is what you were trying to do
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
#include <iostream>
using namespace std;

int Year;
bool c3;

int main()
{
	do
	{
		cout << "Please enter in a four(4) digit number for the year." << endl;
		cin >> Year;
		if(Year >= 1812 && Year <= 2012)
		{
		cout << "Correct" << endl;
		c3 = true;
		}
		
		else
		{
			cout << "Value of Year not in the range" << endl;
			c3 = false;
		}
	}
	while (c3 == true);

}

Basically what i did is i used a if an else statement.. If the year is in the range that you want it to be then it prints the correct message and sets c3 to true and else means if its anything else than that then its false and returns the false message. and its doing that while c3 is true so when it is false it quits the program
thanks guys . i will try to add another if condition to see how it works
Topic archived. No new replies allowed.