Help with Arrays

Hello everyone this is my first post and I'm kinda new at this so please forgive me if I have a hard time with understanding the language we use for some of the terms. Anyways I am working on this project and running into a bunch of problems with the output. The way Arrays are suppose to be setup is that it has a limit of a thousand values. The User is suppose to input any positive number he wants and then it suppose to take those numbers and reverse the numbers via the way they were inputted. Just for your reference I am using VS.

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
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#define cls system("cls")
int i;

using namespace std;

int main()
{
	const int SIZE = 1000;
	int count = 0;
	int myArr[SIZE];

	for (i = 0; i < SIZE; i++) {
		printf("Enter a value for the array (-1 to quit): ");
		scanf_s("%i", &myArr[i]);
		if (myArr[i] == -1) {
			break; 
		}
	} 
	reverse(myArr, myArr + SIZE);
	cout << "Your numbers reversed are: ";
	for (i = 0; i < SIZE; i++) {
		printf("%i. %i\n", i + 1, myArr[i]);
		if (myArr[i] == -1) {
			break; 
	} 
	
}
	system("pause");
	return 0;
}


This is my output.
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
Enter a value for the array (-1 to quit): 1
Enter a value for the array (-1 to quit): 2
Enter a value for the array (-1 to quit): 3
Enter a value for the array (-1 to quit): -1
Your numbers reversed are: 1. -858993460
2. -858993460
3. -858993460
4. -858993460
5. -858993460
6. -858993460
7. -858993460
8. -858993460
9. -858993460
10. -858993460
11. -858993460
12. -858993460
13. -858993460
14. -858993460
15. -858993460
16. -858993460
17. -858993460
18. -858993460
19. -858993460
20. -858993460
21. -858993460
22. -858993460
23. -858993460
24. -858993460
25. -858993460
26. -858993460
27. -858993460
28. -858993460
29. -858993460
30. -858993460
31. -858993460
32. -858993460
33. -858993460
34. -858993460
35. -858993460
36. -858993460
37. -858993460
38. -858993460
39. -858993460
40. -858993460
41. -858993460
42. -858993460
43. -858993460
44. -858993460
45. -858993460
46. -858993460
47. -858993460
48. -858993460
49. -858993460
50. -858993460
You have an array that has room for 1000 integers.

The user enters N integers. Since your loop counts the terminating -1, the array will have at most 999 "real" values. Most likely the N is much smaller than 1000.

You then call std::reverse with all 1000 elements of the array. Surely you want to reverse (and later show) only the first N values?

You actually do have the real count of values. It is not the SIZE.


Unrelated, why do you use two different I/O systems? Use either C's <cstdio> or C++'s <iostream>.


Why is the int i; a global variable? It does not have to be.
Topic archived. No new replies allowed.