The question becomes, what you do want to do with them?
Printable View
The question becomes, what you do want to do with them?
I guess i need to do all of the work that was originally done in main through the class functions now?
Well I need to display all of the items with a number in order to select them. Then work with them like I was doing with the built in items in my original code.
You could overload functions that returns the private data so you can display it.
That sounds awesome I would love to make all of that information no longer private although I'm not sure that's what I'm suppose to do.
I need to be able to access the information in the switch statement and others... I guess I do need it to be free data; not sure if that's what my teacher wants though. Oh well, I just want it to work after with your help I was able to make the array of class objects.
Is overloading ok to do?
You don't need to make it public.
Just make member functions that returns the data you need.
Like this?
then would (in main) productlist[0].heldname give me the first held name?! I think I'm getting close!Code:#include <string>
#include <iostream>
class dispenserType
{
public:
int getNoOfItems() const;
int getCost() const;
void makeSale();
dispenserType(int setNoOfItems = 50, int setCost = 50);
void read(std::ifstream& readfrom)
{
readfrom >> name;
readfrom >> numberOfItems;
readfrom >> cost;
}
void return(std::string& heldname int& heldnoi int& heldcost)
{
heldname = name;
heldnoi = numberOfItems;
heldcost = cost;
}
private:
std::string name;
int numberOfItems;
int cost;
};
or should I make a function to return each different variable and be like
std::string returnname()
can I return a string?Code:{
std::string heldname;
heldname = name
return name;
}
productlist[0].returnname
Maybe...
But what if you don't want all 3 of them?
Better create 3 functions, each that returns said part of the data you need.
No, it wouldn't (at least if I'm interpreting this correctly). All you're doing is defining a member function that takes an std::string reference named heldname as argument. You need to pass in a variable of type std::string to the function which will then, upon return of the function, have the information you want.Quote:
then would (in main) productlist[0].heldname give me the first held name?!
Code:#include <string>
#include <iostream>
class dispenserType
{
public:
int getNoOfItems() const;
int getCost() const;
void makeSale();
dispenserType(int setNoOfItems = 50, int setCost = 50);
void read(std::ifstream& readfrom)
{
readfrom >> name;
readfrom >> numberOfItems;
readfrom >> cost;
}
std::string returnname(std::string heldname)
{
heldname = name;
return heldname;
}
int returnnoi(int noi)
{
noi = numberOfItems;
return noi;
}
int returncost (int& heldcost)
{
heldcost = cost;
return cost;
}
private:
std::string name;
int numberOfItems;
int cost;
};
1>c:\users\doug\documents\visual studio 2008\projects\lab5candy\lab5candy\candymachinelab. cpp(66) : error C2664: 'sellProduct' : cannot convert parameter 1 from 'std::string' to 'dispenserType []'Code:while (choice !=99)
{
switch (choice)
{
case 1:
sellProduct (productlist[0].returnname(name), counter);
break;
case 2:
sellProduct (productlist[1].returnname(name), counter);
break;
case 3:
sellProduct (productlist[2].returnname(name), counter);
break;
case 4:
sellProduct (productlist[3].returnname(name), counter);
break;
case 5:
sellProduct (productlist[4].returnname(name), counter);
break;
case 6:
sellProduct (productlist[5].returnname(name), counter);
break;
case 7:
sellProduct (productlist[6].returnname(name), counter);
break;
case 8:
sellProduct (productlist[7].returnname(name), counter);
break;
case 9:
sellProduct (productlist[8].returnname(name), counter);
break;
default:
cout << "Invalid selection." << endl;
}
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
:( lol
Come on. This is obvious.
sellProduct takes a dispenserType array.
You need to think about your implementation. What do you want and where?
And as for this:
You are returning the data, so it does not need to take an argument.Code:std::string returnname(std::string heldname)
{
heldname = name;
return heldname;
}
int returnnoi(int noi)
{
noi = numberOfItems;
return noi;
}
int returncost (int& heldcost)
{
heldcost = cost;
return cost;
}
Code:std::string returnname()
{
return heldname;
}
I want the name lol! I fixed the class functions though. Will keep working on the other stuff thanks again for ur help!
void sellProduct(dispenserType product[], cashRegister& pCounter);
dispenserType productlist[8];
productlist is a dispenserType
Code:#include <string>
#include <iostream>
class dispenserType
{
public:
int getNoOfItems() const;
int getCost() const;
void makeSale();
dispenserType(int setNoOfItems = 50, int setCost = 50);
void read(std::ifstream& readfrom)
{
readfrom >> name;
readfrom >> numberOfItems;
readfrom >> cost;
}
std::string returnname()
{
return name;
}
int returnnoi()
{
return numberOfItems;
}
int returncost()
{
return cost;
}
private:
std::string name;
int numberOfItems;
int cost;
};
Well after I fixed the class functions it says this for every line of the switch statement.
1>c:\users\doug\documents\visual studio 2008\projects\lab5candy\lab5candy\candymachinelab. cpp(62) : error C2660: 'dispenserType::returnname' : function does not take 1 arguments
Have to fun to class though thanks for the help I hope i can figure it out later ;)
Look,
if dispenserType::returnname does not take any arguments, then don't pass any arguments to it.
already tried, if I take it out it still gives a compiler error.
1>c:\users\doug\documents\visual studio 2008\projects\lab5candy\lab5candy\candymachinelab. cpp(62) : error C3867: 'dispenserType::returnname': function call missing argument list; use '&dispenserType::returnname' to create a pointer to memberCode:case 1:
sellProduct (productlist[0].returnname, counter);
break;
As the error says, it's a function, not a variable.
Functions must have () at the end, otherwise it's illegal. If it were a non-class function, you would have taken its address instead.
I am pretty sure that I tried it earlier with the () but I'm not sure will post results when I get a chance to compile again