Charge Account Validation

This program that prompts a user to enter a charge account number and
then determines whether that number is valid by checking for it in a list
of valid numbers.
*/

#include <iostream>
using namespace std;

// Function prototype
int searchList(int [], int, int);

const int arraySize = 18;

int array_pause;
int main()
{
int tests[arraySize] = {5658845, 450125, 7895122, 8777541, 8451277,
1302850, 8080152, 4562555, 5552012, 5050552, 7825877, 1250255,
1005231, 654231, 3852085, 7576651, 7881200, 4581002};

int array_pause,
results;

results = searchList(tests, arraySize, array_pause);

cout<<" Enter a valid Charge Account"<<endl;
if (results == -1)
cout << "The number entered is InValid.\n"; //Display the number is InValid

else
{
cout << "The number entered is valid.\n"; //Displays the number is valid

cout << (results) << ".\n";

}
system("pause");
return 0;
}

int searchList(int list[], int numElems, int value)
{
int index = 0; // Used as a subscript to search array
int position = -1; // Used to record position of search value
bool found = false; // Flag to indicate if the value was found

while (index < numElems && !found)
{
if (list[index] == value) // If the value is found
{
found = true; // Set the flag
position = index; // Record the value's subscript
}
index++; // Go to the next element
}
return position; // Return the position, or -1
}
// THE QUESTION: perform binary search to locate valid account numbers . Use the //selection sort algorithms to sort the array before the binary search is performed.
//The program requires a binary search of the data. The program should read the //charge account numbers from a file and the contents of the file should be unsorted.
Last edited on
Can I know one thing.. what is the question?

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
#include <iostream>
using namespace std;

// Function prototype
int searchList(int [], int, int);

const int arraySize = 18;

int array_pause;

int searchList(int list[], int numElems, int value)
{
	int index = 0; // Used as a subscript to search array
	int position = -1; // Used to record position of search value
	bool found = false; // Flag to indicate if the value was found
	
	while (index < numElems && !found)
	{
		 if (list[index] == value) // If the value is found
		 {
			found = true; // Set the flag
			position = index; // Record the value's subscript
		 }
		index++; // Go to the next element
	}
		
	return position; // Return the position, or -1
}

int main()
{
	int tests[arraySize] = {5658845, 450125, 7895122, 8777541,
 8451277,1302850, 8080152, 4562555, 5552012, 5050552, 7825877,
 1250255,1005231, 654231, 3852085, 7576651, 7881200, 4581002};
	
	int array_pause = 0;
	int results = 0;
	
	cin >> array_pause;

	results = searchList(tests, arraySize, array_pause);
	
	cout<<" Enter a valid Charge Account"<<endl;
	if (results == -1)
	cout << "The number entered is InValid.\n"; //Display the number is InValid
	else
	{
		cout << "The number entered is valid.\n"; //Displays the number is valid	
		cout << (results) << ".\n";	
	}
	system("pause");
	return 0;
}
Last edited on
array_pause varuable was never set but you are using it in calculations
Please use code tags when you post.

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
#include <iostream>
using namespace std;

// Function prototype
int searchList(int [], int, int);

//need to add a function to get data from the user

const int ARRAYSIZE = 18; //should be capitalized and it should also be declared in main

int array_pause;  //this variable is declared twice see line 16 this should be deleted
int main()
{
	int tests[ARRAYSIZE] = {5658845, 450125, 7895122, 8777541, 8451277,
		1302850, 8080152, 4562555, 5552012, 5050552, 7825877, 1250255,
		1005231, 654231, 3852085, 7576651, 7881200, 4581002};

	//need a function call to a function that gets data from the user
	
	
	int array_pause,  //this variable is declared as a global variable
		results;
	                     //array_pause is being passed without a value
	results = searchList(tests, ARRAYSIZE, array_pause);

	cout<<" Enter a valid Charge Account"<<endl;
	if (results == -1)
		cout << "The number entered is InValid.\n"; //Display the number is InValid

	else
	{
		cout << "The number entered is valid.\n"; //Displays the number is valid

		cout << (results) << ".\n";

	}
	system("pause");
	return 0;
}

int searchList(int list[], int numElems, int value)
{
	int index = 0; // Used as a subscript to search array
	int position = -1; // Used to record position of search value
	bool found = false; // Flag to indicate if the value was found

	while (index < numElems && !found)
	{                        //There is nothing stored in value at this point
		if (list[index] == value) // If the value is found
		{
			found = true; // Set the flag
			position = index; // Record the value's subscript
		}
		index++; // Go to the next element
	}
	return position; // Return the position, or -1
} 



Please state what your problem is the only problem that i see is that you never prompt the user to input data. Along with some minor stuff like declaring array_pause twice.
The question is perform binary search to locate valid account numbers . Use the selection sort algorithms to sort the array before the binary search is performed.

I really need help. I posted it last night but I was too tired so I felt asleep along the line. Today is the last day to submit. Your program should read the charge account numbers from a file and the contents of the file should be unsorted. The charge accounts are found the problem entitled Charge Account Validation.

The file will have one charge account number on each line of the file. You will need to create your own data file. The file should be named: chargeaccounts.dat
/*
This program that prompts a user to enter a charge account number and
then determines whether that number is valid by checking for it in a list
of valid numbers.
*/
#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std;

// Named Constants
const int ACCOUNT_SIZE = 18;

// Function Prototypes
bool Search_Valid_Account(int[], int, int); // Using Bool as function return type.
//---------------------------------------------------------------------------

bool Do_Again (void);
//---------------------------------------------------------------------------
int main (void) {

cout.setf (ios::fixed, ios::floatfield);
cout.setf (ios::showpoint);
cout.precision (2);

// Storing the valid account numbers in integer array.
int Valid_Charge_Account[ACCOUNT_SIZE] = {5658845, 8080152, 1005231, 4520125,
4562555, 6545231, 7895122, 5552012,
3852085, 8777541, 5050552, 7576651,
8451277, 7825877, 7881200, 1302850,
1250255, 4581002};
int Charge_Account_Num;
int Num_Elements = 0;
// Getting input from user to check if it is valid or not.
do {
cout << endl << "Please Enter Charge Account Number: ";
cin >> Charge_Account_Num;
// Displaying the valid account numbers so that they know where they went wrong.
cout << endl << endl << "Valid Charge Account Numbers are: " << endl << endl;
for (int i = 0; i < 18; ++i) {
cout << " " << Valid_Charge_Account[i];
++ Num_Elements;
}
// If the function return true, the account is valid else its Invalid.
if (Search_Valid_Account (Valid_Charge_Account, Charge_Account_Num,
Num_Elements)) {
cout << endl << endl << "This Account Number is Valid!";
} else {
cout << endl << endl << "This Account Number is Invalid";
}

} while (Do_Again ());
return 0;
}// Function Definitions
bool Search_Valid_Account(int valid_charge_account[], int charge_account_num,
int num_elements) {
// This function checks from index 0 to index one less than the number of
// elements(our case: 18) if the account number entered by user is in any
// of the index of our array of valid acount numbers. If it finds it then
// it returns true else it return false to main.
int index = 0;
while (index < num_elements &&
charge_account_num != valid_charge_account[index]) {
++ index;
}
if (index < num_elements) {
return true;
} else {
return false;
}
}
//------------------------------------------------------------------------
bool Do_Again (void) {

(void) fflush (stdin);
cout << endl << endl << "Do Again? (y/n): ";
return static_cast (tolower (_getch ())) == 'y';
}


Last edited on
http://www.learncpp.com/cpp-tutorial/64-sorting-an-array-using-selection-sort/
This is selection sort implementation example

Were you told that you need to write binary seac yourself? if not:
1
2
3
4
5
#include <algorithm>
//...
if(std::binary_search(tests, tests + ARRAYSIZE, array_pause) )
{
//... 
not really
The link you provided might be very very helpful in future.
I was never asked to use binary_search
Well why not just use a search algorithm in the STL in conjuction with a suitable STL container rather than a C array?
Oh Are you done. So do you think I will be ok adding this code?
Topic archived. No new replies allowed.