User Inputs 10 Integers into Array

Hi there! I am currently taking my first programming class and we have reached Arrays. I have followed all the tutorials here however I have not come across any tutorials on Arrays with user input being passed to a function. I have worked on this code for at least 2 hours trying to figure out why it gives me the results it does.

Here is my code:

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

// Program to store 10 integers array

#include <iostream>
#include <stdio.h>

using namespace std;

int getArray(int x[]);
int reverseArray(int x[], int);

int main ()
{
  int a[5];

 	int e = getArray(a);
	int s = reverseArray(a,5);

  return 0;
}

int getArray(int x[])
{
	int userInput;
	
	for (int count = 0; count < 5; count ++)
	{
		cout << "Please enter 5 numbers: ";
		cin >> userInput;
		
		userInput = x[5];

	}
	
}

int reverseArray(int a[], int i )
{
	int n = 5;
        cout << "The reverse order: " << endl;
       
        for(int i=n-1; i>=0; i--)
        {
            cout << a[i] << ' ';
        }
}



Everything complies (which I am really happy about!), HOWEVER, my output is this:

Please enter 5 numbers: 1
Please enter 5 numbers: 2
Please enter 5 numbers: 3
Please enter 5 numbers: 4
Please enter 5 numbers: 5
The reverse order:
8797504 4200912 4201006 2686760 4200912

Where are these giant numbers coming from? Any help is greatly appreciated! Thank you very much for your time!
First, your getArray() function has no return type. Why are you assigning a return from this function to int e? Same with the reverseArray(). I'm not sure what compiler you are using, but I'm pretty sure (without throwing it in mine) that it shouldn't even compile. Hmm, it does compile, but with a ton of warnings that should have told you about just that thing. Anyway, moving on.

Now, in your getarray() function you are not assigning the input to the array. You are assigning the value in element [5] of the array (which, to be honest, isn't even IN the array you declared) to the variable userInput. Assignment works from right to left. IE, the value on the right is placed on into the variable on the left. Also, array elements start at 0. So, if you have an array of size N, the elements are held in array[0] to array[n-1]. Array[5] is actually 1 beyond the end of the array and so spits out gibberish since the data in that location is undefined.

To input to an array (or output from one) it's typical to use a for loop like you did, but you use the counter variable in the array itself to iterate over each element of the array, like this:

1
2
3
4
5
for (int i = 0; i < 5; i++)                                  // assuming size is a variable holding         
{                                                             //the size   of the array
    std::cout << "Enter a number:";
    std::cin   >> array[i];
}


Here you don't need the userInput variable for an intermediary, you can input directly to the array. Note though that if you change the array size you will need to change function itself. A better solution would be to change the function to accept the array size as an additional parameter.

One final note: Don't take this the wrong way, we all had to start somewhere, but you made a lot of fundamental errors in this program so I'd re-read or find some new articles on variables, assignment and declaration, functions, and the like. :)
Last edited on
Thank you so much for your help! I changed the code just like you said and will study that section more. My program is now working 100%. I am studying the second paragraph to try and figure out what I am doing wrong there. Thanks for your help!
Topic archived. No new replies allowed.