-1

i need to store number and sum it , but the compiler say the a.num[i] is inaccessible... Here my code

class number{
private:
    int num[12];
public:
    number(){
}
    int totalnumber(){
    int total =0;
        for( int i=0 ; i<12 ; i++){
        total= total + num[i];
    }
        return total;
}
};
int main(){
number a;
int a;
    cout<<"Enter number :";
        cin>>a.num[i];
    while(a.num[i] < 0){
        cout<<"You've entered invalid input."<<endl;
        cout<<"Enter number :"<<endl;
        cin>>a.num[i];
    }
    cout<<"Total amount saved is : RM"<<number.totalnumber()<<endl;
    return 0;
    }

I think the number constructor need to do something, but i cant figure it out:( . Is there any way i can store the array inside a private array as declare in class number?

  • 1
    The entire point of making a value `private` is to prevent this. I assume making it `public` is off the table? – user4581301 Dec 12 '17 at 03:22
  • Just noticed you have two variables `number a;` and `int a;` with the same identifier. This shouldn't compile. You should discard the `int a;` because it doesn't do anything to further your example. – user4581301 Dec 12 '17 at 03:24
  • To fix this, you can either (1) make num public, (2) create a getter function which returns the pointer. – lamandy Dec 12 '17 at 03:25
  • Another option is to overload `operator[]`. That would allow you to `a[i]`. More on that here: [What are the basic rules and idioms for operator overloading?](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading) – user4581301 Dec 12 '17 at 03:27
  • But i wanna try put in private and see how it can access and storing .any recommand solution is welcome... – Jsafrose.Blackula Dec 12 '17 at 03:27
  • 1
    Only `number` and `friend`s of `number` can access `number`s `private` data without doing ill-advised, brittle things. You will need to go `public` or add accessor functions. – user4581301 Dec 12 '17 at 03:31

1 Answers1

0

There are a couple of problems with your code:

First you defined 2 variables with the same name in the "main" function:

number a;
int a;

Then you try to access the variable "i" in "cin>>a.num[i];" but "i" has not be declared yet assigned in the "main" function.

I guess and think what you may try to achieve with your code is to sum a couple of numbers that are entered by the user.

In this case you may want to to use "for" loop to iterate over "i" and store the numbers read into the array in your object of the "number" class.

To store the numbers, you may want to implement a method for your class, like this, because you can't access a member variable like "int num[12]" from the ouside when it is declared as a private member:

void storeNum(int num, int i){
  num[i] = num;
}

Third, you create an object of the class "number" with the name "a", but in the last line of your code, you tried to access the method "totalnumber" but you did not specify the object to invoke the method on, instead to wrote the class name.

And then you forget to

#include <iostream>

which is required by the C++ standard if you want to access library functions, which are defined the in the "iostream" header file. Additionally, you may want to write

using namespace std;

at the top of your file to tell the compiler that you want to import all the identifiers from the std namespace into your current namespace; That is, you wan't to write "cout" instead to "std::cout".

( Edit: To make this clear: You should never never never write "using namespace std" in production code... Never. Unless you're learning the language and trying things out.. Then it's okay. Just to keep this in mind for later... )

For example, the finished code could look like this:

#include <iostream>
using namespace std;

class number{
private:
    int num[12];
public:
    number(){};
    int totalnumber(){
        int total =0;
        for( int i=0 ; i<12 ; i++){
            total= total + num[i];
        }
        return total;
    }
    void storeNum(int number, int i) {
        num[i] = number;
    }

};

int main(){
    number a;
    for(int i = 0; i < 12; i++) {
    cout << "Enter Number:";
    int num = 0;
    cin >> num;
    a.storeNum(num,i);
    }
    cout<<"Total amount saved is : RM"<< a.totalnumber()<<endl;
    return 0;
    }

Hopefully, I could resolve your problem I helped you to understand the C++ language a little bit better.

Edit: I noticed you tried to handle invalid user input in your code. That was a good idea (I assume you know at least another language ;) ). But it's more important to get you syntax correct. You can worry about error handling later...

thundl
  • 1
  • 1