Fill array

Feb 8, 2012 at 8:16pm
At first I wanted the user to input the number of elements in the array, but I couldn't get that so I just made a constant int. I want the user to enter number of scores that will be stored in the array and then input -99 to stop. How can I stop it without the output being like 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
#include <iostream>
using namespace std;

int fillArray(int[], int);

int fillArray(int scores[],int n)
{
	int count=0;
	cout<<"Enter scores"<<endl;

	while(scores[count] !=-99)
	{
	for(count=0;count<n;count++)
	{
	cin>>scores[count];
	if(scores[count]==-99)
		break;
	}

	}

	return n;
}

int main()
{
	const int n=30;
	int scores[n] ;


	fillArray(scores, n);

		for(int count=0;count<n;count++)
	cout<<scores[count]<<"  "<<endl;

		return 0;
}







Enter scores
80
50
76
100
90
66
-99
80
50
76
100
90
66
-99
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
Press any key to continue . . .
[output]


Last edited on Feb 8, 2012 at 8:23pm
Feb 8, 2012 at 8:45pm
At first I wanted the user to input the number of elements in the array

If you want this you can use std::vector instead of an array. You could also allocate the array with new but std::vector is better.

1
2
for(int count=0;count<n;count++)
	cout<<scores[count]<<"  "<<endl;

The problem here is that you print all n elements even if the user has entered fewer than n values. One way to solve this is to check if scores[count] != -99.
1
2
for(int count=0;count<n && scores[count] != -99;count++)
	cout<<scores[count]<<"  "<<endl;
Feb 8, 2012 at 9:22pm
Thanks so much, instead of using the -99 I decided to just use the allocate array which I just searched up on google lol. I haven't learned std::vector though. This is what I got and now I can finish the program Thanks man!

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
#include <iostream>
using namespace std;
int fillArray(int*,int);


int fillArray(int *scores,int n)
{
        cout<<"Enter scores"<<endl;
        for(int count=0;count<n;count++)
        cin>>scores[count];
        
        return n;
}

int main()
{
    int n;
    int *scores;
    
    cout<<"How many scores would you like to type?"<<endl;
    cin>>n;
    
    scores= new int[n];
    
    fillArray(scores,n);
    
    for(int x=0;x<n;x++)
    cout<<scores[x]<<endl;

    
    system("pause");
    
    return 0;
}


How many scores would you like to type?
5
Enter scores
80
90
70
80
90
80
90
70
80
90
Press any key to continue . . .
Feb 8, 2012 at 9:23pm
you can set the return value of FillArray() with the count of number readed:
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
int fillArray(int scores[],int n)
{
	int count=0;
	cout<<"Enter scores"<<endl;

	//while(scores[count] !=-99)  <---- you can use only the for statement
	for(count=0;count<n;count++){
	    cin>>scores[count];
	    if(scores[count]==-99)
                break;
	}

	return count;  //you must return count, not n
}

//the main

int main(){
    const int n=30;
    int scores[n] ;
    fillArray(scores, n);
    int dim=fillArray(scores,n);
    for(int count=0;count<dim;count++)
	cout<<scores[count]<<"  "<<endl;

    return 0;
}
Last edited on Feb 8, 2012 at 9:25pm
Topic archived. No new replies allowed.