User Array Size and Sorting User Input

I am making program that allows the user to determine how big the array size will be and then asks the user to make up numbers to fill the array. Every time run the program on Dev C++ it says "program has stopped working"

Can someone please help me with it. Please And Thank You!

Heres 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
//Assignment 19 Program 2
#include <iostream>
using namespace std;
int main()

{
   
  //Declare The Array 
  
  int size; 
  int number [size], counter;
  for (counter=0; counter<size; counter++){
  cout << "Please Enter Size Of Your Array: ";
  cin >> size;
  cout << "" <<endl;
  
  //User Enter Number
  
  cout << "Enter a Number: ";
  cin >>number[counter];
  }

  //Display The Array
  
  cout << endl << endl << "Your Array Looks Like: ";
  for(counter=0; counter<size; counter++){
  cout << endl << number[counter];
  }
        
system ("pause");
return 0;
}
You can't declair an array of unknown size...
It probably crashes because the random value in size variable is negative.
1
2
int size;
int number [size];
how do I fix it?
Computers are too stupid to do things out of order. You have to ask the user -- and get -- the size before you can use it.

The next issue is that C++ disallows you to create an array that way. Either use a std::vector or just make your array really big and ignore any elements more than the user asked for.

Hope this helps.
Registered users can post here. Sign in or register to post.