0

i used to code in javascript, but my new school force me to learn c++. Im kind new in this language, and here's the problem:

In javascript i can write such a code:

for(let i = 0; i < 10; i++){
    var variable[i] = i+3;
}

for(let j = 0; j < 10; j++){
    console.log(variable[j]);
}

You may say 'Why don't you just write the code into 1 for loop', but that's only example.

And now i'm trying to rewrite above code to cpp:

int n,k,w;
cin>>n>>k;

for(int i = 0; i < n; i++){
    int w[i];
    cin>>w[i];
}

//some code here

for(int i = 0; i < n; i++){
    cout<<w[i];
}

And here's the question. How can i cout all variables w with index i, cause im getting an error [Error] invalid types 'int[int]' for array subscript.

4
  • 2
    int w[i]; is not valid standard C++ (it's supported by some compilers as extension). Also why don't you just declare the array outside of the for loop? Commented Sep 15, 2018 at 10:59
  • For readabillity, avoid for (int i =.... Declare i at top of the function: int i; then for (i = .... Commented Sep 15, 2018 at 12:22
  • @peakpeak that's exactly the opposite of readable and that's exactly why C (and C++ by default) later on got rid of variable declarations limitations that forced declarations at the top of the functions. Commented Sep 15, 2018 at 12:45
  • Fureeish: Exactly. Stay fuzzy. No clue! Commented Sep 15, 2018 at 16:11

2 Answers 2

7

What you probably want is:

#include <iostream>
#include <vector>

int main() {
    std::vector<int> vec;
    int size = 0;

    std::cin >> size;

    for(int i = 0; i < size; i++){
        int number = 0;
        std::cin >> number;
        vec.push_back(number);
    }

    for(int i : vec){
        std::cout << i << " ";
    }
}

std::vector<int> is a class designed to provide an interface to resizable array. The push_back() function appends the vector with given argument.

The last loop, which is called a ranged-based for(), is used to print all elements of the vector. You can replace it with plain old for() loop with indexing, since std::vector supports operator [], but if ranged-based approach is sufficient, it should be preferred.

EDIT: I don't know JavaScript, but I assume (from your example) that variables declared inside loops are visible everywhere. This is not the case in C++. Variables' visibility is dependent on the scope they are declared in. If you want your list/array/vector/any other container to be visible to those two for() loops, you have to declare it outside them - like in my example above.

EDIT2: While you should almost always use std::vector for such tasks, one could argue that they want to disable resizing the container. In this case, we are left with simple dynamic allocation. We reach for <memory> library, since we shouldn't manage it ourselves:

#include <iostream>
#include <memory>

int main() {
    int size = 0;
    std::cin >> size;

    auto arr = std::make_unique<int[]>(size);

    for(int i = 0; i < size; i++){
        int number = 0;
        std::cin >> number;
        arr[i] = number;
    }

    for(int i = 0; i < size; i++){
        std::cout << arr[i] << " ";
    }
}

For auto, either read here or imagine that it's just a magic type that is (almost always) correct. It's like var in Python or JavaScript (but later on its type cannot be changed).

For std::unique_ptr<T[]>, either read here or imagine that it's just a dynamically allocated array that automatically delete[]s itself. If you did not learn about dynamic allocation yet, simply ignore what that means and wait until it's introduced.

Notice that we also got rid of the ranged-based for() loop. Unfortunately, it does not work with plain, dynamically allocated arrays.

Sign up to request clarification or add additional context in comments.

Comments

1

Not all compilers support VLA so stick to the Standards; always specify a constant size for arrays. If you need some dynamically changed size then consider using another type of containers like: std::vector.

Also why you re-declared int w inside the for loop? It is local to for loop and changes to it won't affect the outer one.

You get a compile time error in the second loop that complains that w is not an array. To solve it make int w[] outer and on top of the two loops.

int n, k;
cin >> n >> k;
int w[n];

for(int i = 0; i < n; i++)
    cin >> w[i];

//some code here

for(int i = 0; i < n; i++)
    cout << w[i];

The alternative to VAL use std::vector:

std::vector<int> w;
int n, k;
std::cin >> n;

for(int i(0); i!= n; ++i){
    std::cin >> k;
    w.push_back(k);
}


for(auto i(0); i != w.length(); ++i)
    std::cout << w[i] << ", ";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.