Hello. Need help with a project im working on. Getting stuck on writing it because it must be written in a while loop without using arrays. With a user inputed pin value, there are 4 rules that must be followed in order for the pin to work.
1.The PIN must be between 5to 7digits long (assume no leading zeros are entered –for example: 002345 is only 4 digits for our purposes).
2.The PIN cannot have the same digit in two consecutive positions(e.g. 12234 would not be a valid PIN)
3.The sum of the last 4 digits of the PIN must be a multiple of 3.
4.The value of n “choose” k, where n is the largest digit and k is the left-most digit of the PIN, should be an even number. The expression n “choose” k is computed as n!/(n−k)!k! where the ! denotes a factorial. For example, for PIN645823, the largest digit n=8 and the left-most digit k=6. 8!/(8−6)!6! =8∗7∗6∗5∗4∗3∗2∗1(2∗1)∗(6∗5∗4∗3∗2∗1)=28.Therefore 645823 would be a valid pin.
here is what I have so far. Any help would be apprecaited. Also the digits must be ecstracted using a loop. No arrays. Here is my code.
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
// initialize PIN value and i
int pin=0;
int i=0;
int count=0;
int num=0;
int prevDig=0;
int rem=0;
int temp;
// initialize loop
while (pin != -1)
{
cout << "Please enter your PIN" << endl;
cin >> pin;
// use statemets to test rule one
if (pin >= 10000 && pin <=9999999)
{
// rule 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
for (i=0; i<7; i++)
{
rem = pin %10;
num = num / 10;
cout << rem << endl;
if(prevDig==temp)
{
temp = (pin/(int) pow(10,i))%10;
cout << "Invalid pin. Consecutive numbers." << endl;
}
}
]pin % 10000;
if (pin%3 !=0)
{
cout << "Invalid pin. Some digits sum are not divisible by 3." << endl;
}
}
|
if (pin >= 10000000)
{
cout << "Invalid pin. Too many digits." << endl;
}
if (pin <= 9999 && pin >= 0)
{
cout << "Invalid pin. Not enough digits." << endl;
}
if (pin < 0)
{
cout << "Invalid pin. Cannot be negative." << endl;
}
}
return 0;
}