Question #152636

Write a C++ program which repeatedly asks the user to enter their contact numbers. Create a string
variable named “Phone_No” and take the input in the following pattern: “+92423672123” where the
first three characters represent country code (e.g., +92 as shown in the example pattern), next two
characters (e.g., 42 according the shown example number) represent the city code, while next 7
characters represent the actual number.
Your task is to display appropriate messages for the numbers belonging to three cities: Islamabad (having city
code 51), Lahore (city code 42), and Karachi (city code: 21). Any number having different country code or city
code must be reported as “Un-known number“. Please also ensure that the user must enter the number
according to the shown pattern only and the size of the input string must be 12 characters.

Expert's answer

#include <iostream>
#include <string.h>
using namespace std;
int main () {
  string Phone_No;
  while (true) {
    cout << "Enter your contact number: " << endl;
    cin >> Phone_No;
    if (Phone_No[0] == '+' && Phone_No[1] == '5' && Phone_No[2] == '1') {
      cout << "The number is belon to Islamobod." << endl;
    }
    else if (Phone_No[0] == '+' && Phone_No[1] == '4' && Phone_No[2] == '2') {
      cout << "The number is belon to Lahore." << endl;
    }
    else if (Phone_No[0] == '+' && Phone_No[1] == '2' && Phone_No[2] == '1') {
      cout << "The number is belon to Karachi." << endl;
    }
    else {
      cout << "Un-known number" << endl;
    }
  }
  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!

LATEST TUTORIALS
APPROVED BY CLIENTS