Help

In this assignment, you are going to write a program that keeps track of the social security numbers (SSN) of customers of a bank.
Please notice that we are going to assume that SSN is a four-digit number (e.g., 1234. 0123 is not allowed.). When the program runs,
it prompts that the user should enter a 4-digit SSN number to record, or 0 (zero) to exit.
So far I have this:

#include <iostream>
using namespace std;
bool isExist(int ssn, int records[], int numberOfRecords);
int main(){
int ssn = 0;
int records[32];


isExist(ssn, records, 32);

}
bool isExist(int ssn, int records[], int numberOfRecords){

for(int i = 0; i < numberOfRecords; i++){
cout << "Please enter a 4-digit SSN number to record, or enter 0 to finish." << endl;
cin >> ssn;
ssn = records[i];

if((ssn > 999) || (ssn < 10000)){
cout << ssn << " is recorded. " << endl;

}else if(ssn == records[i]){
cout << "This SSN is already in the system. " << endl;

}
}
}

Thanks you.
doesn't anyone use code tags!!!
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
#include <iostream>
using namespace std;
bool isExist(int ssn, int records[], int numberOfRecords);
int main(){
int ssn = 0;
int records[32];


isExist(ssn, records, 32);

}
bool isExist(int ssn, int records[], int numberOfRecords){

for(int i = 0; i < numberOfRecords; i++){
cout << "Please enter a 4-digit SSN number to record, or enter 0 to finish." << endl;
cin >> ssn;
ssn = records[i];

if((ssn > 999) || (ssn < 10000)){
cout << ssn << " is recorded. " << endl;

}else if(ssn == records[i]){
cout << "This SSN is already in the system. " << endl;

}
}
}
Sorry about that, this is my first time posting here.
Tabs are handy too:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;
bool isExist(int ssn, int records[], int numberOfRecords);
int main(){
    int ssn = 0;
    int records[32];
    
    isExist(ssn, records, 32);
}

bool isExist(int ssn, int records[], int numberOfRecords){
    
    for(int i = 0; i < numberOfRecords; i++){
        cout << "Please enter a 4-digit SSN number to record, or enter 0 to finish." << endl;
        cin >> ssn;
        ssn = records[i];
    
        if((ssn > 999) || (ssn < 10000)){
            cout << ssn << " is recorded. " << endl;
        }else if(ssn == records[i]){
            cout << "This SSN is already in the system. " << endl;
        }
    }
}


First off, the function name 'isExist' implies that the purpose of the function is to search the array and determine if the passed value 'ssn' exists in the array. This is a perfectly valid design so far. However, you have the user input buried inside that function, which doesn't make sense (especially since you pass ssn as a value to the function, then immediately overwrite it from 'cin >> snn'.

I'd move the user input to a loop in main(), then call isExist with the value they entered. Then if isExist returns false, add the entered ssn to the array.

There's also a problem with this code:
1
2
3
4
5
        if((ssn > 999) || (ssn < 10000)){
            cout << ssn << " is recorded. " << endl;
        }else if(ssn == records[i]){
            cout << "This SSN is already in the system. " << endl;
        }

the 'ssn > 999 || ssn < 10000 check is fine to determine if the value entered is in the valid range, but it reports the result of having been recorded, which hasn't been checked yet. Furthermore it prevents the check which determines if it has already been recorded from happening.

Also, as currently written, any input that is outside the valid range will report as already being in the system. Line 16 assigns records[i] to ssn, and then line 20 (if the value is outside the valid range) will always evaluate as true, thus reporting the invalid input as already in the system.
Last edited on
Any thoughts on my program? What needs to be corrected?
Move the user input to a loop in main() and call isExist() to determine if the entered value is extant in the array. Since it's fairly simple, I'd do validity checking in the main() loop, and only call isExist() after making sure the value is in the valid range

if isExist() returns false, call another function which would insert the new ssn into the array at the first empty element. return true if successful, false if there are no more empty elements.
Last edited on
Whats the point of "numberOfRecords" in isExist function?
um... you wrote it, why are you asking us?
basically you're using it to know when to stop looping through the array. It's possible (in some compilers) to create an array and access elements of that array beyond the defined limits.

1
2
3
int i[20];

i[40] = 11;

Doing so is a very bad idea, as you're accessing memory that you and the compiler have no idea if and what it is being used for.

Since you only declared the array with 32 elements in your program, you're passing 32 to isExist so that isExist knows when to stop looping through array elements.
The assignment told me to write a function using that specific headers.
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
#include <iostream>
using namespace std;
bool isExist(int ssn, int records[], int numberOfRecords);

int main(){
    int ssn;
    int records[3] = {ssn};

    for(int i = 0; i < 3; i++){
        start:
        cout << "Please enter a 4-digit SSN number to record, or enter 0 to finish." << endl;
        cin >> ssn;
        if((ssn >= 10000) || (ssn <= 999)){
            cout << ssn << " is NOT recorded. " << endl;
            goto start;
    }
        isExist(ssn, records, 3);

    }
}
bool isExist(int ssn, int records[], int numberOfRecords){


        if((ssn > 999) || (ssn < 10000)){
            cout << ssn << " is recorded. " << endl;
            ssn = records[ssn];
            return true;
        }else if(ssn == records[ssn]){
            cout << "This SSN is already in the system. " << endl;
            return false;
        }
}

This is what i got so far.
You're still confusing the check for if a ssn is in a valid range (sss>=10000 || ssn <= 999) with checking if it exists in the array.

isExists no longer needs that check since you did it in main(). isExist needs to loop through the array and check if any element already in it matches the passed ssn.


also line 26 above is confusing (and potentially dangerous).

here's some code for how I'd wrtie isExist
1
2
3
4
5
6
7
8
9
10
11
12
13
bool isExist( int ssn, int records[], int numberOfRecords )
{
     bool exists = false;
     for( int i = 0; i < numbeOfRecors; i++ )
     {
          if( records[i] == ssn )
          {
               exists = true;
               break;
          }
     }
     return exists;
}


here's some pseudocode for how I'd restructure main()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
main()
{
	const int numRecs = 32;
	int ssn;
	int records[numRecs] = 0;

	do
	{
		cout << "enter ssn: ";
		cin >> ssn;
		if( ssn is not valid )
		{
			cout << "Invalid SSN entered." << endl;
			continue;
		}
		else // ssn is valid
		{
			if( ssn exists in array )
				cout << "SSN already in system" << endl;
			else
				addSSN( ssn, numRecs );

	} while( ssn != 0 );
}


addSSN() is a new function which would add the passed ssn at the first empty array element.
Last edited on
For the (sss>=10000 || ssn <= 999) its to check if ssn is not a 4-digits number than it will not be included in the array.

In the isExist function if it is a 4-digit number then it is going to be in the array.
Last edited on
Correct on the first part. but just because the number is in the valid range doesn't mean that it is necessarily already in the array. That's what isExist() checks. all it does is return 'true' if it is already there, and 'false' if it isn't.
Last edited on
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
#include <iostream>
using namespace std;
bool isExist(int ssn, int records[], int numberOfRecords);
void listRecords(int records[], int numberOfRecords);
int main(){
    int ssn;
    const int numberOfRecords = 3;
    int records[numberOfRecords] = {ssn};

    do{
        start:
        cout << "Please enter a 4-digit SSN number to record, or enter 0 to finish." << endl;
        cin >> ssn;

        if((ssn >= 10000) || (ssn <= 999)){
            cout << ssn << " is NOT recorded. " << endl;
            goto start;
        }
        if(isExist(ssn, records, 3)){
                cout << "This SSN is already in the system. " << endl;
            }else if(ssn == 0){
                listRecords(records, 3);
        }

    }while(ssn != 0);

}


bool isExist(int ssn, int records[], int numberOfRecords){


    for(int i = 0; i < numberOfRecords; i++){
        if(records[i] == ssn){
            cout << "This SSN is already in the system. " << endl;
            return true;
        }else if((ssn > 999) || (ssn < 10000)){
            cout << ssn << " is recorded. " << endl;
            ssn = records[i];
            return false;
        }
    }

}

void listRecords(int records[], int numberOfRecords){
    for(int i = 0; i < numberOfRecords; i++){
        cout << "Social Security " << (i+1) << " is " << records[i] << endl;
    }
}

When I put my code like this then it will run forever. How do I fix it and am i in the correct track?
A few other minor concerns. line 17 'goto start;' is bad form. It'll get the job done in this case, but using goto is archaic and can lead new programmers into trouble. Delete the start: label on line 11, and change line 17 to
continue;
Continue forces execution to go to the top of the current loop, which in this case will accomplish exactly what your goto is diong.

Another minor quibble, line 8 initializes the array with {ssn} which will fill all array elements with the value in ssn. However, ssn has not itself been initialized. Initialize the array with {0} instead. This will allow the isExist() and addSSN() to check for 0 as an unused array element. Some compilers might automatically initialize ints to 0, in which case the code will work as is, but others might not. Whenever there's a possibility of ambiguity, eliminate it.


Now the infinite loop you're getting. The instructions say to have the program quit when the user enters 0, but if they do, the validity check rejects it and asks them to enter a new value. Add a check for 0 before the range check at line 15.
1
2
3
if( ssn == 0 ){
	break;
}

as a counterpart to 'continue', 'break' forces the program to exit the current loop, and will thus effectively terminate the program when 0 is entered. Yes, this makes the while( ssn != 0 ) check redundant, but I'd leave it in as it A) documents for someone reading the code what the loop is for, B) provides a second bailout check if modifications later screw up the new check above.
Last edited on
Also, line 15 checks for validity, not if the ssn has been recorded. I'd change the message to something like cout << ssn << " is invalid. Enter a 4 digit number." << endl;


line 37 your checking if the value is in the range again, this has already been done in main()
there's no reason to do any of his here.
1
2
3
4
5
6
7
8
9
bool isExist(int ssn, int records[], int numberOfRecords){
	for(int i = 0; i < numberOfRecords; i++){
		if(records[i] == ssn){
			cout << "This SSN is already in the system. " << endl;
			return true;
		}
	}
	return false;
}
line 22 of your code 'listRecords(records, 3);' should come after the while{} statement. What should be in its place there is a function which will insert the entered ssn into the array at the first unused element.
Last edited on
Thanks Esslercuffi, I got it to run perfectly. Thanks again.
Topic archived. No new replies allowed.