Answer to Question #185933 in C++ for nethu

Question #185933

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


1
Expert's answer
2021-04-29T05:41:07-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS