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 ?
#include <iostream>
usingnamespace 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.
#include <iostream>
usingnamespace 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;
}
}
#include <iostream>
usingnamespace 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