Storing and outputting inputs in arrays

Hello,

I'm working on a program that places inputted 4 digit numbers in an array. If the user inputs a number that is already in the array, or if it's not a 4 digit number, it shouldn't be recorded in the array. When the user inputs 0, the program should then list all the values in the array. This is where I'm having a problem. When I input 0, all that is outputted is the last number that was recorded. I'm unsure as to why this is happening because there was an earlier point when I was working on the program where the output was what I was expecting. Other than that the program runs with no errors. This is my first program working with arrays, so criticism of the program as far as what I want it to do would also be appreciated, if possible.

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

void fillArray(int records[], int ssn, int& numberOfRecords);
void listRecords(int records[], int numberOfRecords); 
bool isExist(int ssn, int records[], int numberOfRecords); 

const int size=32;
int records[size];
int numberOfRecords;
int ssn;
int i;

int main()
{
	cout<<"Enter a 4 digit Social Security Number. Press 0 to exit"<<endl;
	cin>>ssn;

	isExist(ssn, records, numberOfRecords);

	fillArray(records, ssn, numberOfRecords);

	listRecords(records, numberOfRecords);

	if(ssn==0)
		cout<<"Exiting Program."<<endl;

	system("pause");	
}

void fillArray(int records[], int ssn, int& numberOfRecords)
{
	do
	{
	for(i=0;i<size;i++)
	{
		while(ssn>=1000&&ssn<=9999)
		{
			if(isExist(ssn, records, numberOfRecords)==false)
			{
				records[i]=ssn;
				i++;
				numberOfRecords=i;
				cout<<"Number has been recorded"<<endl;
				cin>>ssn;
			}

			if(isExist(ssn, records, numberOfRecords)==true)
			{
				cout<<"This number has already been recorded"<<endl;
				cin>>ssn;
			}
		}
	}

		while((ssn<1000&&ssn>0)||(ssn>9999))
		{
			cin>>ssn;
		}
	}while(ssn!=0);
}

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

bool isExist(int ssn, int records[], int numberOfRecords) 
{
	for(i=0;i<numberOfRecords;++i)
	{
		while(ssn==records[i])
		{
		return true;
		}
		return false;
	}
}
The problem is you are using 'i' for too many things and it's messing up parts of your code when inserting variables into the array.

In your isExist function you are setting i equal to 0 every time (which is referring to the global variable i, as there is no local one there). The problem is that your fillArray function is also using the global variable i and so you are just inserting a number into records[0] every time (hence printing out 1 number at the end).

To solve this change your for loop in your isExist function to use a completely different variable. I didn't test out everything you can do, so I am not sure if that will solve every problem though.
Last edited on
James2250 pointed out the biggest issue, but there are a couple others. One is that isExist only checks the first element in the array. If the first element is equal to ssn then true is returned, if not then false is returned. Every record should be checked. Another is that numberOfRecords is not set to anything useful, but is passed to isExist (line 19) as if it means something. Fixing the first two issues would probably yield the behavior that you want, but you would still have some strange/unnecessary code.

In terms of structure, hopefully this can give you something to think about:
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
#include<iostream>
using namespace std;

void listRecords(int records[], int numberOfRecords); 
bool isExist(int ssn, int records[], int numberOfRecords); 

const int size=32;

int main()
{
    // These variables don't need to be global.
    int records[size];
    int numberOfRecords=0; // This should be set to 0.
    int ssn;

    cout<<"Enter a 4 digit Social Security Number. Press 0 to exit"<<endl;
    cin>>ssn;
    
    // In addition to checking if the user want's to exit, make sure there is
    // room in the array.
    while(ssn!=0&&numberOfRecords<size)
    {
        if(ssn>=1000&&ssn<=9999)
        {
            if(isExist(ssn, records, numberOfRecords)==false)
            {
                records[numberOfRecords]=ssn;
                numberOfRecords++;
                cout<<"Number has been recorded"<<endl;
            }
            else
            {
                cout<<"This number has already been recorded"<<endl;
            }
        }
        
        cin>>ssn;
    }
    
    listRecords(records, numberOfRecords);
    
    cout<<"Exiting Program."<<endl;
    system("pause");
    return 0;
}

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

bool isExist(int ssn, int records[], int numberOfRecords) 
{
    // loop though all records
    for(int i=0;i<numberOfRecords;++i)
    {
        // if ssn has already been recorded
        if(ssn==records[i])
        {
            return true;
        }
    }
    
    // if ssn has not already been recorded
    return false;
}


Edit: I think this is self explanatory as it is mostly just a rearrangement of what you wrote, but if not feel free to ask.
Last edited on
Using 'i' too many times completely slipped my mind, and I feel silly for not realizing I didn't have isExist check the whole array. The program works how I want it now. Thank you both for your critiques!
Topic archived. No new replies allowed.