In C++ - NASA has decided to go back and use modems to communicate between compu
ID: 3779909 • Letter: I
Question
In C++ - NASA has decided to go back and use modems to communicate between computers (they must have missed the War Games video). In order to provide security, the last 6 digits of the phone number must add to 33. If the fourth number is odd the fifth number must be even. Likewise, if the fourth number is even the fifth number must be odd. Write a phone number generator that asks for the first four digits. The computer must generate a list of all possible phone numbers that meet the new security rule, and display them as follows: (XXX) - XXX - XXXX
Explanation / Answer
#include<iostream>
#include<time.h>
#include <string>
using namespace std;
int main()
{
string phone_number;
int num;
int ch,len;
cout<<"Enter the first 4 digits of phone number: "<<endl;
getline(cin,phone_number);
len = phone_number.length();
for( int i = 3 ; i < 10 ; i++)
{
//convert character to number
num = phone_number[i] - 48;
//check 4th number is odd or even to generate next number
if( num % 2 == 0)
{
//even number so generate next number odd
srand( time( NULL ) ); // using the time seed from srand explanation
num = rand();
//first make sure number is between 0-9
num = num % 10;
//if number is even , then make it odd by adding 1
if( (num % 2) == 0)
{
num +=1;
}
//convert num to character
ch = num + 48;
}
else
{
//this is odd number so generate next number even
srand( time( NULL ) ); // using the time seed from srand explanation
num = rand();
//first make sure number is between 0-9
num = num % 10;
//if number is od , then make it even by adding 1
if( (num % 2) == 1)
{
num +=1;
}
//convert num to character
ch = num + 48;
//push this number to phone_number
}
phone_number.push_back(ch);
}
//display phone number to output
cout<<"Phone number generated : ";
for( int i = 0 ; i < phone_number.length(); i++)
{
if(i==0 )
{
cout<<"(";
}
if( i == 3 )
{
cout<<")";
}
if( i == 3 || i==7)
{
cout<<'-';
}
cout<<phone_number[i];
}
}
---------------------------------------------------------------------------------------------------
output
Enter the first 4 digits of phone number:
7022
Phone number generated : (702)-2101-0101