0

I came from python and I'm trying to learn c++ now, and I'm trying to print something that looks like python's list = [1, 2, 3] in c++. I let user to chose a size of array and insert a numbers inside and after that I would like to cout the result in above fashion, but I'm not able to loop over values in array because the variable pointing to a pointer not the array itself. Could me please somebody explain a workaround?

int main() {


    int i, n;
    cout << "How many values?";
    cin >> i;

    int * foo;
    foo = new (nothrow) int [i];

    if (foo == nullptr) {
        cout << " Could Not allocate so much memory";
    }
    else {
        for (n=0; n<i; n++) {
            cout << "Enter Number: " << endl;
            cin >> foo[n];
        }
    }

    
    cout << "[";
    for (int *n: foo){
        cout << n << ", ";
    }
    cout << "]" << endl;
    return 0;
}

ERROR MSG

error: ‘begin’ was not declared in this scope; did you mean ‘std::begin’?
   31 |     for (int *n: foo){
      |                  ^~~
      |                  std::begin
In file included from /usr/include/c++/9/string:54,
                 from /usr/include/c++/9/bits/locale_classes.h:40,
                 from /usr/include/c++/9/bits/ios_base.h:41,
                 from /usr/include/c++/9/ios:42,
                 from /usr/include/c++/9/ostream:38,
                 from /usr/include/c++/9/iostream:39,
                 from study.cpp:1:
/usr/include/c++/9/bits/range_access.h:105:37: note: ‘std::begin’ declared here
  105 |   template<typename _Tp> const _Tp* begin(const valarray<_Tp>&);
      |                                     ^~~~~
study.cpp:31:18: error: ‘end’ was not declared in this scope; did you mean ‘std::end’?
   31 |     for (int *n: foo){
      |                  ^~~
      |                  std::end
In file included from /usr/include/c++/9/string:54,
                 from /usr/include/c++/9/bits/locale_classes.h:40,
                 from /usr/include/c++/9/bits/ios_base.h:41,
                 from /usr/include/c++/9/ios:42,
                 from /usr/include/c++/9/ostream:38,
                 from /usr/include/c++/9/iostream:39,
                 from study.cpp:1:
/usr/include/c++/9/bits/range_access.h:107:37: note: ‘std::end’ declared here
  107 |   template<typename _Tp> const _Tp* end(const valarray<_Tp>&);
      |  
  • See [Range-based for loop on a dynamic array?](https://stackoverflow.com/questions/15904896/range-based-for-loop-on-a-dynamic-array). – dxiv Aug 06 '20 at 00:27
  • thanks, looks like this fits the most when I would like to use C style arrays, but It's too hard for me to understand that overloading >. – Marcel Kopera Aug 06 '20 at 00:42
  • Just use a regular `for` loop, like you do when filling the array, eg: `for (n=0; n – Remy Lebeau Aug 06 '20 at 00:45

2 Answers2

1

If you use a std::vector instead of a C-style array, then you can iterate through it with a range-based for loop, like so:

#include <vector>
#include <iostream>

int main() {
    int i;
    std::cout << "How many values?";
    std::cin >> i;

    std::vector <int> v;

    for (int n = 0; n < i; n++) {
        std::cout << "Enter number: " << std::endl;
        int x;
        std::cin >> x;
        v.push_back (x);
    }
    
    std::cout << "[";
    for (auto e : v)
        std::cout << e << ", ";
    std::cout << "]" << std::endl;
}
Paul Sanders
  • 24,133
  • 4
  • 26
  • 48
1

new c++11 for loop causes: "error: ‘begin’ was not declared in this scope"

I believe that answer to that question is what you are looking for.

kucar
  • 245
  • 3
  • 13