hey guys
I'm trying to practice writing codes with class and objects. I tried to wrote a program where I make 5 instances of the class using a for loop. Also I wanted to use a constructor that would allow the user to initialize the values. The errors are listed after the code. Thanks
Use a cnonstructor that enables the user to enter values. Have the user enter data. Refer to "MULTIREC" And "CONSuser" . Please write an individual program that is your own.
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
|
// Example program
#include <iostream>
#include <string>
using namespace std;
class balance {
private:
string name;
public:
balance (string n, double y)
{
name=n;
salary=y;
}
double salary;
void setName (string n)
{
name=n;
}
string getName ()
{
return name;
}
};
int main ()
{
const int size=5;
string name;
double salary;
cout << "\nPlease enter a name and salary" << endl;
cin >> name >> salary;
balance mb [size] (name, salary);
for (int i=0; i<size; i++)
{
balance mb [i];
}
for (int i=0; i <size; i++)
{
cout << "\nPlease enter your name then your salary" << endl;
cin>> name;
mb[i].setName(name);
cin>>mb[i].salary;
}
for (int i=0; i <size; i++)
{
cout<< "\nYour name is " << endl << mb[i].getName();
cout << "\nYour salary is " << endl << mb[i].salary;
}
}
|
In function 'int main()':
36:36: error: bad array initializer
41:22: error: no matching function for call to 'balance::balance()'
41:22: note: candidates are:
11:5: note: balance::balance(std::string, double)
11:5: note: candidate expects 2 arguments, 0 provided
7:7: note: balance::balance(const balance&)
7:7: note: candidate expects 1 argument, 0 provided
7:7: note: balance::balance(balance&&)
7:7: note: candidate expects 1 argument, 0 provided