Program help needed!
Mar 6, 2013 at 10:07pm UTC
I am working on a program that asks the user to input the first 4 digits of a phone number, the computer generates all possible phone numbers that meet the new security rule: 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. The outcomes must be displayed as follows: (XXX) - XXX - XXXX
Here is what I have so far.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
#include "stdafx.h"
#include <iostream>
#include <ctime>
#include <vector>
#include <algorithm>
#include <iomanip>
using namespace std;
int main()
{
vector<int > telNums; //list of numbers given by user
vector<int > genNums (5, 0); //list of complete numbers generated by computer
int telNumber;
int even = 0;
int odd = 1;
int iRand = 0;
srand((unsigned int )time(NULL));
cout << "Please enter the first four digits of the telephone number: " << endl;
for ( int i=0; i<4; i++){
cout << " Enter number " << i+1 << ": " << endl;
cin >> telNumber;
telNums.push_back(telNumber);
}
if ( (telNums[3] % 2) != 0){ //If telNums 4 is odd...
for ( int even=0 ; even < 9; even+=2){
for ( int j=0; j<10; j++){
genNums[0]++;
for ( int k=0; k<9; k++){
genNums[1]++;
for ( int l=0; l<9; l++){
genNums[2]++;
for ( int m=0; m<9; m++ ){
genNums[3]++;
for ( int n=0; n<9; n++ ){
genNums[4]++;
if ( (even + genNums[0] + genNums[1] + genNums[2] + genNums[3] + genNums[4]) == 33){
cout << "(" << telNums[0] << telNums[1] << telNums[2] << ")" << " " << telNums[3] << even << genNums[0] << " " << genNums[1] << genNums[2] << genNums[3] << genNums[4] << endl;
}
}//It only outputs one outcome and it doesn't even add up to 33
}
}
}
}
}
}
else { //if telNums 4 is even...
for ( int odd=1; odd<10; odd+=2){
for ( int j=0; j<10; j++){
genNums[0]++;
for ( int k=0; k<9; k++){
genNums[1]++;
for ( int l=0; l<9; l++){
genNums[2]++;
for ( int m=0; m<9; m++ ){
genNums[3]++;
for ( int n=0; n<9; n++ ){
genNums[4]++;
if ( (odd + genNums[0] + genNums[1] + genNums[2] + genNums[3] + genNums[4]) == 33){
cout << "(" << telNums[0] << telNums[1] << telNums[2] << ")" << " " << telNums[3] << even << genNums[0] << " " << genNums[1] << genNums[2] << genNums[3] << genNums[4] << endl;
} //It only outputs one outcome and it doesn't even add up to 33
}
}
}
}
}
}
}
system("pause" );
return 0;
}
Mar 7, 2013 at 2:25am UTC
Hi, for
vector<int > genNums (5, 0);
, I think it will be
vector<int > genNums (6, 0);
, since you are generating 6 more numbers.
Here is the program,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
#include "stdafx.h"
#include <iostream>
#include <ctime>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <numeric>
using namespace std;
int main()
{
vector<int > telNums; //list of numbers given by user
vector<int > genNums (6, 0); //list of complete numbers generated by computer
int telNumber;
int even = 0;//Not needed
int odd = 1; //Not needed
int iRand = 0; //Not needed
srand((unsigned int )time(NULL)); //Not needed
cout << "Please enter the first four digits of the telephone number: " << endl;
for ( int i=0; i<4; i++)
{
cout << " Enter number " << i+1 << ": " ;
cin >> telNumber;
telNums.push_back(telNumber);
}
for (int i = 0; i < 9; i+=2)
{
if (telNums[3]%2 != 0)
genNums.at(0) = i; //if the 4th number is odd
else
genNums.at(0) = i+1; // if the 4th number is even
for (int n2 = 0; n2 < 10; n2++)
for (int n3 = 0; n3 < 10; n3++)
for (int n4 = 0; n4 < 10; n4++)
for (int n5 = 0; n5 < 10; n5++)
for (int n6 = 0; n6 < 10; n6++)
{
genNums.at(1) = n2;
genNums.at(2) = n3;
genNums.at(3) = n4;
genNums.at(4) = n5;
genNums.at(5) = n6;
if (accumulate(genNums.begin(),genNums.end(), 0) == 33)
cout << "Numbers: (" << telNums[0] << telNums[1] << telNums[2] << ")-"
<< telNums[3] << genNums[0] << genNums[1] << "-"
<< genNums[2] << genNums[3] << genNums[4] << genNums[5] << endl;
}
}
system("pause" );
return 0;
}
Hope it helps! :)
Last edited on Mar 7, 2013 at 2:25am UTC
Mar 8, 2013 at 2:37am UTC
That does help a lot! I tried with the code changes you made, however it is now giving me every possible numbers instead of just the ones that equal 33
Mar 8, 2013 at 6:30pm UTC
Bump!
I need to get this figured out today, if anyone has suggestions please help!
Mar 9, 2013 at 4:00am UTC
I got it to work! Thanks
Topic archived. No new replies allowed.