does my code work as I wanted it to?

Hi!
I'm new here.So I was messing around with making a user input array and I think I did.
but I don't know what I did too.It's just that I confused myself with my code :D.
anyway can someone explain to me what I did?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

using namespace std;


int main()
{
int i;
cout<<"array size"<<endl;
cin>>i;
int j[i];
for(int x=0;x<i;x++)
{
    cout<<"array element"<<endl;
    cin>>j[x];
};
cout<<i<<"size of array and element"<<j[0]<<endl;

}


here is an image of it https://ibb.co/bs5MyF

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
#include <iostream>

using namespace std;

int main()
{
    int i;
    cout<<"array size"<<endl;   // Displays "array size" on the screen and
                                    // then a newline.

    cin>>i;     // Waits for the user to type something, possibly an integer
                // number, and then press ENTER. After the user has pressed 
                // ENTER, it tries to insert what the user has typed into
                // the variable i, which is of type 'int'. If the user hasn't
                // typed an integer number, troubles will occur.

    int j[i];   // Creates an array of integer with room for 'i' integer numbers,
                // i.e. the number the user is supposed to have typed previously.
                // Please note: this is forbidden by ISO C++. Since you are using
                // CodeBlocks, you are likely to have MinGw as a compiler.
                // If you set a higher warning level, let's say -Wall -Wextra
                // -pedantic-errors, it would give you an error rather than let
                // you create the executable.

    for(int x=0;x<i;x++)    // Starts a loop which will execute as many times
                            // as the number the user has given.
    {
        cout<<"array element"<<endl;    // Displays "array element" + a newline
                                        // at every cycle of the for-loop

        cin>>j[x];  // Waits for the user to type something, possibly an integer 
                    // number, and then press ENTER. After the user has pressed 
                    // ENTER, it tries to insert what the user has typed into 
                    // the array 'j' at the position 'x' (the position will be 
                    // incremented at every iteration of the loop).
                    // Since the array 'j' is of type 'int', shouldn't the user 
                    // have typed an integer number, troubles will occur.
    };
    // Displays just once, in the same line:
    //   - the value of the variable 'i' (i.e. the user input);
    //   - "size of array and element"
    //   - the value of the integer at position 0 of the array 'j'
    //   - a newline
    cout<<i<<"size of array and element"<<j[0]<<endl;

}

This is not C++: the number of elements in the array must be constant known at compile-time.
1
2
3
int i;
// ...
int j[i];

http://coliru.stacked-crooked.com/a/3ac3973c91c4506e

Compile with a C++ compiler; if the GNU compiler is being used,
specify both -std=c++14 and -pedantic-errors

Instead do something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

int main()
{
    const int MAX_SIZE = 1024 ; // compile-time constant
    int array[MAX_SIZE] = { 0 } ; // initialise to all zeroes
    
    int logical_size ;
    std::cout << "aray size (max " << MAX_SIZE << "): " ;
    std::cin >> logical_size ;
    if( logical_size > MAX_SIZE ) logical_size = MAX_SIZE ;
    
    std::cout << "enter " << logical_size << " integers\n" ;
    for( int i = 0 ; i < logical_size ; ++i ) std::cin >> array[i] ;
    
    // ...
}

I see thank you for the answers but what does const int max size do?
but what does const int max size do?
const int max size it’s simply illegal C++ syntax.
const int MAX_SIZE, as JLBorges wrote, defines a variable of type ‘int’ and qualifies it as constant, i.e. it can’t be modified once initialized.

May I be so bold to ask you if you are attending some course, trying to put together some code or... what else? Just to understand which type of help you are looking for. To be totally honest, it’s the first time I come across a post where someone claims they can’t understand their own code.

C++ doesn’t allow an array to be creted by a value which is not constant (unless you use “new” or “malloc()” or other workarounds). Unfortunately, some compiler, if not correctly ‘set’, doesn’t follow this rule and let the programmer write code which is not (entirely) legal.

You know what a compiler is, by the way, do you?

The rest of the JLBorges' code just propose this simple solution to that problem: create an array which is likely to be *way* bigger than the one you reckon you need, and later, if the user asks for even a higher number of “rows”, downsize their megalomaniac requests to the level you previously chose.

Last edited on
it turns my code into machine code.
and I have no expirience or whatsoever with a programing language.
and the reason for my question is because i wanted to make an array which size is defined by the user
Last edited on
and the reason for my question is because i wanted to make an array which size is defined by the user

Right! My mother has always told me not to trust strangers too.

Anyway, I'd suggest:
a) in CodeBlocks, open Settings --> Compiler --> Compiler settings --> Compiler flags.
Search for options like (in order of preference):
[-std=c++17]
[-std=c++14]
[-std=c++11]
[-std=gnu++17]
[-std=gnu++14]
[-std=gnu++11]
[-std=c++0x]
You need to tick *just one* of these - no more than one.
If, after having chosen one, your programs don't compile anymore, go back to the options and choose another one.

b) learn about "new".
The following code is an example of usage of operator new.
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
#include <iostream>
#include <limits>

int main()
{
    std::cout << "Please decide array dimension (you'll be required to insert "
                 "an element for each 'row')\n"
                 "How many elements do you want to store? ";
    int size;
    std::cin >> size;
    
    int* myarray = new int[size]; // Creating an array in the heap

    for(int i=0; i<size; i++)
    {
        std::cout << "Please add a value for position " << i << ": ";
        std::cin >> myarray[i];
    }
    
    std::cout << "\nYou inserted: ";
    for(int i=0; i<size; i++)
    {
        std::cout << myarray[i] << "; ";
    }
    std::cout << std::endl;
    
    delete[] myarray; // arrays created by new[] are usually explicitly deleted

    // This part does not relate to your question.
    // It just displays "Press ENTER to continue..." and causes the program
    // to stop until the user presses ENTER.
    std::cin.ignore(1);
    std::cout << "\nPress ENTER to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    return 0;
}

Last edited on
> i wanted to make an array which size is defined by the user

In C++, for such an array, we use a vector. https://cal-linux.com/tutorials/vectors.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <vector>

int main()
{
    // in C++, std::size_t is the unsigned integer type used for
    // specifying the size of arrays and for indexing into arrays.
    std::size_t sz ;
    std::cout << "enter the size of the array: " ;
    std::cin >> sz ;

    // create a vector containing 'sz' elements
    std::vector<int> my_array(sz) ;


    std::cout << "enter " << my_array.size() << " integers\n" ;

    // range-based loop: for each element in the vector
    // http://www.stroustrup.com/C++11FAQ.html#for
    for( int& element : my_array ) std::cin >> element ;

    // ...
}
in CodeBlocks, open Settings --> Compiler --> Compiler settings --> Compiler flags.
Search for options like (in order of preference):
[-std=c++17]


wow, hadn't realized C::B now has a C++17 compiler, gotta download
Ops! I didn’t mean I’m sure there’s already a -std=c++17 option available for the MinGw edition that cames with CodeBlocks. My preferred IDE is Qt Creator, which on Windows installs its own MinGw version, so I’m not reliable informed about CodeBloks.
As a stand-alone compiler on my W7 machine I currently use Lavavej’s package (https://nuwen.net/mingw.html), but the best option I was able to set is -std=gnu++14.
(On my Linux machine I can use -std=c++17 without any problem, instead.)
Sorry if I caused you to waste your time!
Topic archived. No new replies allowed.