A problem about array

closed account (oLC9216C)
This is the code, but after i run it, nothing comes out, what's wrong with it?
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
#include <iostream>

using namespace std;
void delete_repeats(char a[],int& sizes);
void get_size(int& sizes);
int main()
{
    int sizes;
    char a[sizes];
    get_size(sizes);
    delete_repeats(a,sizes);
    return 0;
}

void delete_repeats(char a[],int& sizes)
{
    cout << "Enter " << sizes << " characters:\n";
    for (int i = 0; i < sizes; i++)
        cin >> a[i];

}
void get_size(int& sizes)
{
    cout << " Enter how many characters do you want to enter.\n";
    cin >> sizes;
}


The out put screen is


Process returned -1073741571 (0xC00000FD) execution time : 0.040 s
Press any key to continue.


For the main part, I added cout << "1"; after char, and still nothing comes out,
but when I add it after between int and char, it shows up. Does it mean the program stop before char a[sizes]?
closed account (3CXz8vqX)
You're...screwing up your references. It's returning an address, not the value. You'll need to read over the chapter on pointers again.


edit. Shouldn't line 17 be 'after' line 18? You're asking it to call sizes which isn't initialised yet.
Last edited on
Arrays in C++ must be compile time constants. So in the following snippet where have you initialized the size of your array with a constant value?

1
2
    int sizes;  // Must be a constant and must have a value.
    char a[sizes];
closed account (oLC9216C)
Is it possible that the size of the value don't have a constant ?
Yes, you either need to use a vector, or dynamically allocate the memory.
.
Last edited on
So use arrays, but to statically allocate an array the size must be a compile time constant. Just create a large array and make sure the number you user enters for the size is actually equal to or smaller than your array size. If they try to enter a larger number tell them the maximum size and make them re-enter the number until it is correct, or terminate the program.
Topic archived. No new replies allowed.