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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
//Chris Cochran
//COP2000.0M2
//Project 8
//This program will validate a users
//number within the array
#include<iostream>
using namespace std;
//prototypes
int getInput();
void validate(int& n);
class Accounts
{
private:
int number; // number entered by user
bool correct; // user entered correct number
public:
Accounts();
void setnumber(int);
void compare(const int [], const int);
void display();
};
Accounts::Accounts()
{
correct = false;
}
void Accounts::setnumber(int n)
{
number = n;
}
void Accounts::compare(const int ticket[], const int SIZE)
{
for (int i = 0; i < SIZE; i++)
{
if (ticket[i] == number)
correct = true;
}
}
void Accounts::display()
{
cout << endl << endl;
if (correct)
cout << "The number is valid!\n";
else
cout << "The number is invalid!\n";
}
int main()
{
const int SIZE = 18;
int ticket[SIZE] = { 5658845, 4520125, 7895122, 8777541, 8451277, 1302850,
8080152, 4562555, 5552012, 5050552, 7825877, 1250255,
1005231, 6545231, 3852085, 7576651, 7881200, 4581002};
int correctNum; //raw input from the user
Accounts chance; //object of class Accounts
correctNum = getInput();
//move valid input value to the class
chance.setnumber(correctNum);
//compare values
chance.compare(ticket, SIZE);
//display results
chance.display();
return 0;
}
int getInput()
{
int in; //variable to read into
cout << "Please enter your account number. ( 7 numbers)\n";
cin >> in;
validate(in);
return in;
}
void validate(int& n)
{
while ( n < 1000000 || n > 9999999 || !cin)
{
cin.sync();
cin.clear();
cout << "Please enter a 7 digit number.\n";
cin >> n;
}
}
| |