-4

Newbie question

C++ array as

class App1{
int end = 3;

public:
int list[end];

void App1::AddInput(){
    for(int i=0; i<end; i++){
        cout << endl << "Enter elements in array: " << endl;
        cin >> n;
        list[i] = n;
    }
}

void App1::display(){
    cout << endl << "Display elements: " <<endl;
    for(int i=0; i<sizeof(list) / sizeof(list[1]); i++){
        cout << i << end;
        cout << list[i];
    }
    cout << endl;
}
}

But this does not display the array contents

user544079
  • 16,109
  • 42
  • 115
  • 171
  • This may be stupid question but you said you're new. Do you have a main module somewhere calling these functions? – gowrath Sep 13 '16 at 05:03
  • `int list[end];` is not legal C++ code. Check the compiler's output messages. – M.M Sep 13 '16 at 05:03
  • `void App1::AddInput()` inside the class definition is also an error – M.M Sep 13 '16 at 05:04
  • Another error: `n` is not declared in `cin >> n;`. – Sergey Sep 13 '16 at 05:06
  • 1
    OP you need to post a [MCVE](http://stackoverflow.com/help/mcve) – M.M Sep 13 '16 at 05:08
  • 1
    Duplicate: [How to iterate through a list of numbers in c++](http://stackoverflow.com/q/14350886/514235) ... and ... Related: [C++ Iterate through Vector using for loop](http://stackoverflow.com/q/12702561/514235) – iammilind Sep 13 '16 at 05:09
  • @gsamaras he has an example, ample answer in JS, clearly no C++ base, thus maybe no idea what the Verifiable part would mean, – Adrian Colomitchi Sep 13 '16 at 05:13
  • Bleh, I didn't vote to mark as duplicate. It's dishonest how the site combines all different votes into 1 message. OP if you want to find out why your code isn't working, edit to include a MCVE. Also it would be good to say exactly what your question is, whether it is "Why isn't my code working?", or (as others have assumed) "Show me some code to do this" – M.M Sep 13 '16 at 05:29

1 Answers1

-1

You have a few errors in your code. I have answered them with in line comments so you can read through it sequentially:

class App1 {

private:
    int end = 3;    // this hsould either be declared in the explicit public section you made, or an explicit private section.

public: // do you even indent bro

    /* You can't declare an array like this in a class because
         * the size end is not known at compile time and the compiler
         * needs to know how many bytes to allocate for it. If this
         * was a constant number, you could do it.
         */
    // int list[3];      // this is fine
    int* list_ptr = new int[end];     // this is better; it uses the heap so the compiler is ok not knowing the value of end at compile time. However, you have to remember to free it when you're done.


    void AddInput(){      // You had App1:AddInput here. The namespace declaration is only needed if you are implementing this method outside the enclosing {} braces of the class definition. In this case, because you are implementing it within `class App1{ implementation }`, you are not to add the namespace before the function
        for(int i=0; i<end; i++){
            cout << endl << "Enter elements in array: " << endl;
            int n;      // need to declare n before you can read into it.
            cin >> n;
            list_ptr[i] = n;
        }
    }

    void display(){
        cout << endl << "Display elements: " <<endl;
        for(int i=0; i< end; i++){    // this won't work the way you want; use the variable end
            cout << "Elem " << i << " is: " << list_ptr[i] << endl;       // youa accidentally wrote << end instead of << endl;
        }
        cout << endl;
    }
};  // need to end class declaration with a semicolon
gowrath
  • 3,136
  • 2
  • 17
  • 32