Classes and Functions

So I was trying this easy question out and when I executed it I got about 9 errors.
Code:-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include<iostream>
#include <string>

using namespace std;

class item{
char name[50][3];
int code[3];
int price[3];

public:
    void input(int);
    int largest();
    int sum();
    void display(int);
};

void item::input(int i){
cout<<endl<<"Enter the Item name:- ";
cin.getline(name,50,i);
cout<<endl<<"Enter item code:- ";
cin>>code[i];
cout<<endl<<"Enter item price:- ";
cin>>price[i];
}

int item::largest(){
largest = price[0];
for(int j=1;j<3;j++){
    if(largest<price[i])
        largest = price[i];
}
return largest;
}

int item::sum(){
    int sum=0;
for(int p=0;p<3;p++){
    sum+=price[p];
}
return sum;
}

void item::display(int u){
cout<<endl<<"Name:- ";
cout.write(name,50,u);
cout<<endl<<"Price:- "<<price[u];
cout<<endl<<"Item code:- "<<code[u];
}

int main(){
int a, biggest = item.largest, total = item.sum;
cout<<endl<<"Enter data:- ";
for(a=0;a<3;a++){
    item.input(a);
}
cout<<endl<<"Largest  price:- "<<biggest;
cout<<endl<<"Sum:- "<<total;
for(int t=0;t<3;t++)
  cout<<endl<<endl<<"Display:- "<<endl<<item.display(t);
return 0;
}


and the errors that I got were:-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
D:\C++\class employee\main.cpp: In function 'int main()':
D:\C++\class employee\main.cpp:53:22: error: expected primary-expression before '.' token
 int a, biggest = item.largest, total = item.sum;
                      ^
D:\C++\class employee\main.cpp:56:9: error: expected unqualified-id before '.' token
     item.input(a);
         ^
D:\C++\class employee\main.cpp:59:23: error: 'total' was not declared in this scope
 cout<<endl<<"Sum:- "<<total;
                       ^
D:\C++\class employee\main.cpp:61:45: error: expected primary-expression before '.' token
   cout<<endl<<endl<<"Display:- "<<endl<<item.display(t);
                                             ^
Process terminated with status 1 (0 minute(s), 0 second(s))
9 error(s), 0 warning(s) (0 minute(s), 0 second(s))
 


Thanks!
You are trying to pass a multidmensional array to cin.getline, basically you're passing it a pointer to array of pointers.

You are trying to use largest as if it where a variable inside itself while it is a function.

cout.write again try to use a multidimensiona array.

Your calls to item.largest or item.sum again lack the () that specify they are functions.

Also calls like item.input(a); are using the variable type without creating a variable of that type first.
A proper call would be

1
2
item I;
I.input(a);


Switch to Visual Studio, it underline in red all this mistakes while you write them, spot them all without the need to read your code, very begginner friendly ;)
Last edited on
Where did you create an instance of your class named "item"? Remember that a class is an User Defined Type so in order to use members of that class you must first create an instance of that class and then use that instance.

1
2
3
item my_item; // Create an instance of your class.

int biggest = my_item.largest(); // Note that you are calling a member function not accessing a member variable. 


Topic archived. No new replies allowed.