#include <iostream>
usingnamespace std;
int main()
{
int a;
cout<<"Enter a:";
cin>>a;
if(a>10)
cout<<a;
else
cout<<"Wrong input. Enter again: ";
return 0;
}
H everyone. I'm a newbie so I have a basic question for my code above. If I enter a value for a < 10. What should I do to enter the value again instead of exit the program?
You probably haven't learned about while or do/while loops yet, but one method for repeatedly asking for input until you get an acceptable value is (rewriting the if/else):
#include <iostream>
int main()
{
std::cout << "Enter a: ";
int a;
do
{
std::cin >> a;
if (a <= 10)
{
std::cout << "Wrong input. Enter again: ";
}
}
while (a <= 10);
std::cout << a << '\n';
}
Your if statement actually checks for a value greater than 10, so 10 and below is rejected. If you want a value to be 10 or greater the if should be if (a >= 10).