In: Computer Science
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
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
int i = 0;
char playAgain;
do
{
//random number generator
srand (static_cast<unsigned int>(time(0)));
//define variables
int firstFour[5];
int lastSix[7];
//Greeting and input of first four numbers
cout<<"\n\n\t\tWelcome to the NASA's modem number generator"<<endl
<<"\t\tYou will enter the first four digits of the phone number, "<<endl
<<"\t\tand then be given options for the last six numbers. "<<endl
<<"\n\t\tEnter the first four desired numbers: ";
//while statement to input numbers to array
for(int i = 0; i < 4; i++)
{
cout<<"\n\t\t";
cout<<i + 1<<". ";
cin>>firstFour[i];
}
//do loop to check if == to 33
int j=0;
do
{
int i = 0;
//check if 4th number is even
if(firstFour[3] % 2 == 0)
{
do
{
lastSix[0] = rand() % 9;
}while(lastSix[0] % 2 == 0);
for(int i = 1; i < 6; i++)
{
lastSix[i] = rand() % 10;
}
}else
if(firstFour[3] % 2 != 0)
{
do
{
lastSix[0] = rand() % 10;
}while(lastSix[0] % 2 != 0);
}
for(int i = 1; i < 6; i++)
{
lastSix[i] = rand()% 10;
}
int sum = 0;
for (int i=0; i<6; i++)
{
sum+= lastSix[i];
}
if(sum == 33)
{
//display chosen number
cout<<"(";
for(int i = 0; i < 3; i++)
{
cout<<firstFour[i];
}
cout<<")-"<<firstFour[3]<<lastSix[0]<<lastSix[1]<<"-";
for(int i = 2; i< 6; i++)
{
cout<<lastSix[i];
}
cout<<"\n";
}
j++;
}while(j < 500);
cout<<"Run again?";
cin>> playAgain;
}while (playAgain == 'y');
system("pause");
return 0;
}
Sample Output:
Welcome to the NASA's modem number generator
Enter the first four digits of the phone number,
and then give options for the last six numbers.
Enter the first four desired numbers:
1. 3
2. 4
3. 5
4. 6
(345)-672-4839
(345)-639-6465
(345)-675-6537
(345)-679-5534
(345)-655-7169
(345)-651-7398
(345)-656-7735
(345)-659-7291
(345)-636-8970
(345)-618-7944
(345)-639-6672
Run again? N