class

OK...I made a thread the other day about the array but what I need is a private class then in main do the code or something like that. Here's the instructions:
1
2
3
4
5
6
7
8
9
Write a program that lets the user enter a charge account number.   The program should determine if the number is valid by checking for it in the following list:

        5658845    4520125    7895122    8777541    8451277    1302850
        8080152    4562555    5552012    5050552    7825877    1250255
        1005231    6545231    3852085    7576651    7881200    4581002

Initialize a one-dimensional array with these values.   Then use a simple linear search to locate the number entered by the user.   If the user enters a number that is in the array, the program should display a message saying the number is valid.  If the user enters a number not in the array, the program should display a message indicating it is invalid.

Your program should use a class with appropriate data variables and methods.  The array that holds the charge account numbers should be a private data variable.


Here's the codes that 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
//Chris Cochran
//COP2000.0M2
//Project 8
//This program will validate a users
//number within the array

#include<iostream>
using namespace std;

class Accounts
{
private:
	int account[ 18 ];
	int number;
	char choice;
	

	
int main()
{
	int account[ 18 ] = {5658845, 4520125, 7895122,8777541, 8451277, 1302850,
					8080152, 4562555, 5552012, 5050552, 7825877, 1250255,
					1005231, 6545231, 3852085, 7576651,7881200, 4581002};
	bool found = false;
	do
	{
		do
		{
			cout << "Please enter your account number (7 digits).\n";
			 cin >> number;
		} while (number < 1000000 || number > 9999999);
		
		for (int i=0; i < 18; i++)
		{
			if (number == account[i])
			{
				cout << "The number is valid.\n";
				found=true;
				break;
			}
		}
		
		if(found==false)
			cout<<"The number is invalid.\n";

        cout << "Do you want to enter another account number?(y/n)\n";
        cin >> choice;
	} while (choice == 'y' || choice == 'Y');

}
return 0;
}


The book i have is confusing when talking about classes.
Line 16: You have no }; terminating your class declaration.

No methods in your class.

You never instantiated (used) your class.
I know. lol. That's what I need help with. My book is very confusing with the class. And I just forgot to close the class :p
I know I need a void in the class, but I'm not sure which one. I also fixed the return 0; and put it in the correct place and added the variables in main. But I'm not sure on how to use my class in main.
The class needs a constructor. You should initialize the account array within the constructor.

Then create a method bool isValid(int number); This should return true if number is valid and false otherwise.
The class needs a constructor. You should initialize the account array within the constructor.- I thought thats what I did when I did int accounts[ 18 ]; :p

the bool goes in the class right? Then all i do is bool isValid(number); in main?
thought thats what I did when I did int accounts[ 18 ];

No, that's a simple array declaration. Not a constructor.

the bool [function] goes in the class right?

Yes.

Then all i do is bool isValid(number); in main?

That's not quite right.

You need to implement the Accounts::isValid() function.
You need to instantiate an instance of Accounts in main.
You need to initialize the account[18] member of that instance from your global account array (of ints).
Then you need to call isValid through the instance of Accounts you created.

Sounds easy if I knew what most of that meant lol. When I do Accounts::isValid(); it says that the function isnt accessible. I'm guessing because where the bool isValid is at is in private so it can't be used in main unless I use tell it to use it....but that didn't work either...what does instantiate mean? lol.
Let's break it down. You need your class to have the accounts[] array, the constructor and the isValid method. So the declaration of the class looks like this
1
2
3
4
5
6
7
class Accounts {
public:
    Accounts();
    bool isValid(int number);
private:
    int accounts[18];
};


You will need to write the constructor and isValid functions.

1
2
3
4
5
6
7
8
9
10
Accounts::Accounts()
{
     // add code to initialize accounts array
}

bool Accounts::isValid(int number)
{
    // add code to check if the number is valid.  Return true or false
    depending on what you find.
}


Finally, your main program will need to create an Accounts object and call its isValid method:
1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
	Accounts accts;
...
		cout << "Please enter your account number (7 digits).\n";
		cin >> number;
		if (accts.isValid(number)) {
			cout << "that number is valid\n";
		} else {
			cout << "invalid account number!  We're calling the police!\n";
		}
...
}




Ohhh ok. That makes sense lol. So in bool Accounts::isValid(int number){ }; I do
1
2
3
4
5
6
7
8
9
10
11
12
for (int i=0; i < 18; i++)
		{
			if (number == account[i])
			{
				cout << "The number is valid.\n";
				found=true;
				break;
			}
		}
		
		if(found==false)
			cout<<"The number is invalid.\n";

and that goes in the class...and LOL on "We're calling the police!"
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
#include<iostream>
using namespace std;

class Accounts
{
	public:
    Accounts();
    bool isValid(int number);
private:
    int accounts[18];
		Accounts::Accounts()
	{
     int account[ 18 ] = {5658845, 4520125, 7895122,8777541, 8451277, 1302850,
					8080152, 4562555, 5552012, 5050552, 7825877, 1250255,
					1005231, 6545231, 3852085, 7576651,7881200, 4581002};
	}

bool Accounts::isValid(int number)
	{
    for (int i=0; i < 18; i++)
		{
			if (number == account[i])
			{
				cout << "The number is valid.\n";
				found=true;
				break;
			}
		}
		
		if(found==false)
			cout<<"The number is invalid.\n";
	}


};

int main()
{
	double number;
	char choice;

	Accounts accts;

	do{
		do{
			cout << "Please enter your account number (7 digits).\n";
			cin >> number;
		}while (number < 1000000 || number > 9999999);
		cout << "Do you want to enter another account number?(y/n)\n";
		cin >> choice;
	} while (choice == 'y' || choice == 'Y');

		if (accts.isValid(number)) {
			cout << "that number is valid\n";
		} else {
			cout << "invalid account number!\n";
		}
return 0;
}



Most of the errors are saying that most of everything is already defined or declared. Here's the errors:

1
2
3
4
5
6
7
8
9
10
1
1>  Charge Account Validation.cpp
1>c:\users\chris\desktop\cop2000 visual c++\project 8\project 8\charge account validation.cpp(18): error C2535: 'Accounts::Accounts(void)' : member function already defined or declared
1>          c:\users\chris\desktop\cop2000 visual c++\project 8\project 8\charge account validation.cpp(13) : see declaration of 'Accounts::Accounts'
1>c:\users\chris\desktop\cop2000 visual c++\project 8\project 8\charge account validation.cpp(25): error C2535: 'bool Accounts::isValid(int)' : member function already defined or declared
1>          c:\users\chris\desktop\cop2000 visual c++\project 8\project 8\charge account validation.cpp(14) : see declaration of 'Accounts::isValid'
1>c:\users\chris\desktop\cop2000 visual c++\project 8\project 8\charge account validation.cpp(28): error C2065: 'account' : undeclared identifier
1>c:\users\chris\desktop\cop2000 visual c++\project 8\project 8\charge account validation.cpp(31): error C2065: 'found' : undeclared identifier
1>c:\users\chris\desktop\cop2000 visual c++\project 8\project 8\charge account validation.cpp(36): error C2065: 'found' : undeclared identifier
1>c:\users\chris\desktop\cop2000 visual c++\project 8\project 8\charge account validation.cpp(59): warning C4244: 'argument' : conversion from 'double' to 'int', possible loss of data
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
How do I fix these errors?
OK!!!! I got the teachers help (not much help but helped a little(she told me what page to go to in the book for the example code but the example code was 4 pages long and was a lot so I didn't even use it but it gave me an idea of what to do)) and looked in the book and got this:

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;
	}
}


I didn't need to do the loop to do program over again. I completely understand part of this now :p I'll keep studying it but thanks everyone for the help!

PS
Before I did this code, I did another code that did the exact same thing, but was much shorter and easier to code. So why would anyone write all this code to code something that can be done much quicker and easier?
Last edited on
So why would anyone write all this code to code something that can be done much quicker and easier?

This was a contrived (and IMO not very good) example to introduce you to classes. The power of classes is that they provide encapsulation and extensibility.

One of the reasons I said this is not a very good example is that your ticket array of winning numbers belongs as a private member of your class. You might want to extend your class to have an AddWinningNumber(int num) or AddWinningNumbers(int nums[], int size), but this is something that makes sense after learning std::vector.

number and correct really don't belong as members of your class. number should be an argument to a function such as isWinngingNumber(int num). correct should be the bool result of Compare. They have no bearing on the collection of winning numbers, which is what Accounts is suppoed to represent.

BTW, the only place you set correct false, is in the Accounts constructor. This will set it false initially, which is fine. But your compare function does not reset correct to false. Therefore if you call Compare a second time with a not winning ticket after calling it with a winning ticket, correct will remain set to true.
Last edited on
Topic archived. No new replies allowed.