Write main method of a c++ program to read until user enters a 4 digit integer. The program outputs the number of digits in the user input.
SAMPLE OUTPUT 1
Enter a 4 digit number:14
Output:14 has 2 digits.Please enter a 4 digit number
SAMPLE OUTPUT 2
Enter a 4 digit number:145
Output:145 has 3 digits.Please enter a 4 digit number
SAMPLE OUTPUT 3
Enter a 4 digit number:1188
Output:You have entered the correct combination
#include <iostream>
using namespace std;
bool check(int a){
return (a > 999 && a < 10000);
}
int main(){
int a = 0;
cout<<"Enter a 4 digit number: ";
while(!check(a)){
cin>>a;
if(!check(a)){
cout<<a<<" has "<<(to_string(a)).length()<<" digits. Please enter a 4 digit number\n";
}
else cout<<"You have entered the correct combination";
}
return 0;
}
Comments
Leave a comment