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...