Apr 25, 2011 at 1:55am UTC
My problem is that i'm trying to make a function that uses information stored in an array of structures, but doesn't modify any of it.
I'm getting errors saying:
variable or field `profititem' declared void
cannot convert `product' to `int' in initialization
in line
void profititem(products[i]);
So without writing out the entirety of the program, here's what i have that's relevant:
struct product
{
string name;
int q1;
int q2;
int q3;
int q4;
};
void profititem(product);
int main()
{.....
....
for (i = 0; i < 10; i++)
void profititem(products[i]);
}
void profititem(product products)
{
int total
total = products.q1 + products.q2 + products.q3 + products.q4
cout << total << endl;
}
So, any help?
Apr 25, 2011 at 2:16am UTC
void profititem(products[i]);
This makes no sense where it is. If it is an attempt to call the function profititem
, that void
has no business being there.
Apr 25, 2011 at 2:17am UTC
Alright, so i removed void, now its giving me
conversion from `product*' to non-scalar type `product' requested
what do i fix now
Apr 25, 2011 at 2:22am UTC
What is products
? The implication is that products[i]
is a pointer to an object of type product, not an actual object of type product.
Apr 25, 2011 at 2:26am UTC
use pass by reference
void profititem(product &products);
Apr 25, 2011 at 2:31am UTC
What is products? The implication is that products[i] is a pointer to an object of type product, not an actual object of type product.
----uhhhhh, what do you mean what is it? products is the object of the structure product
use pass by reference
-----i was trying that before and i still got errors
Apr 25, 2011 at 2:33am UTC
uhhhhh, what do you mean what is it?products is the object of the structure product
I don't know what it is because I can't see your code. For all I know, your code says
int products[10];
which would make products an array of ten integers. If it were an array of products, you wouldn't be having this problem.
products is the object of the structure product
If it's an object, why are you treating it like an array of objects with
products[i]
? Is it an array?
Last edited on Apr 25, 2011 at 2:34am UTC
Apr 25, 2011 at 2:43am UTC
Oh boy, i have no idea what you're talking about right now.
This is the beginner forum right?
Apr 25, 2011 at 2:44am UTC
Yes.
At some point in your code, you created something named "products".
For example, in your struct you create an int, named q1, like this:
int q1;
How did you create the thing named "products"?
Last edited on Apr 25, 2011 at 2:46am UTC
Apr 25, 2011 at 2:47am UTC
Well, here's the entire code:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
struct product
{
string name;
int q1;
int q2;
int q3;
int q4;
};
void profititem(product);
int main()
{
int i;
product products[10];
ifstream infile;
infile.open("input.txt");
if (infile.is_open())
{for (short i = 0; i < 10; i++)
{
infile >> products[i].name;
infile >> products[i].q1;
infile >> products[i].q2;
infile >> products[i].q3;
infile >> products[i].q4;
cout << products[i].name << endl;
if (products[i].q1 < 0 && products[i].q1 < -99)
cout << " " << products[i].q1;
else if (products[i].q1 < 0 && products[i].q1 > -99)
cout << " " << products[i].q1;
else if (products[i].q1 > 0 && products[i].q1 < 100)
cout << " " << products[i].q1;
else
cout << " " << products[i].q1;
if (products[i].q1 < 0 && products[i].q1 < -99)
cout << " " << products[i].q1;
else if (products[i].q1 < 0 && products[i].q1 > -99)
cout << " " << products[i].q1;
else if (products[i].q1 > 0 && products[i].q1 < 100)
cout << " " << products[i].q1;
else
cout << " " << products[i].q1;
if (products[i].q1 < 0 && products[i].q1 < -99)
cout << " " << products[i].q1;
else if (products[i].q1 < 0 && products[i].q1 > -99)
cout << " " << products[i].q1;
else if (products[i].q1 > 0 && products[i].q1 < 100)
cout << " " << products[i].q1;
else
cout << " " << products[i].q1;
if (products[i].q1 < 0 && products[i].q1 < -99)
cout << " " << products[i].q1 << endl;
else if (products[i].q1 < 0 && products[i].q1 > -99)
cout << " " << products[i].q1 << endl;
else if (products[i].q1 > 0 && products[i].q1 < 100)
cout << " " << products[i].q1 << endl;
else
cout << " " << products[i].q1 << endl;
for (i = 0; i < 10; i++)
profititem(products[i]);
}
}
void profititem(product products);
{
int total;
total = products.q1 + products.q2 + products.q3 + products.q4;
cout << total << endl;
}
infile.close();
system("PAUSE");
}
}
Apr 25, 2011 at 3:02am UTC
Your function definition,
1 2 3 4 5 6
void profititem(product products);
{
int total;
total = products.q1 + products.q2 + products.q3 + products.q4;
cout << total << endl;
}
has no business living inside your main function like that. It should be moved to after your main function.
Also, remove the semi-colon so it looks like this:
1 2 3 4 5 6
void profititem(product products)
{
int total;
total = products.q1 + products.q2 + products.q3 + products.q4;
cout << total << endl;
}
The following code compiles:
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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
struct product
{
string name;
int q1;
int q2;
int q3;
int q4;
};
void profititem(product);
int main()
{
int i;
product products[10];
ifstream infile;
infile.open("input.txt" );
if (infile.is_open())
{for (short i = 0; i < 10; i++)
{
infile >> products[i].name;
infile >> products[i].q1;
infile >> products[i].q2;
infile >> products[i].q3;
infile >> products[i].q4;
cout << products[i].name << endl;
if (products[i].q1 < 0 && products[i].q1 < -99)
cout << " " << products[i].q1;
else if (products[i].q1 < 0 && products[i].q1 > -99)
cout << " " << products[i].q1;
else if (products[i].q1 > 0 && products[i].q1 < 100)
cout << " " << products[i].q1;
else
cout << " " << products[i].q1;
if (products[i].q1 < 0 && products[i].q1 < -99)
cout << " " << products[i].q1;
else if (products[i].q1 < 0 && products[i].q1 > -99)
cout << " " << products[i].q1;
else if (products[i].q1 > 0 && products[i].q1 < 100)
cout << " " << products[i].q1;
else
cout << " " << products[i].q1;
if (products[i].q1 < 0 && products[i].q1 < -99)
cout << " " << products[i].q1;
else if (products[i].q1 < 0 && products[i].q1 > -99)
cout << " " << products[i].q1;
else if (products[i].q1 > 0 && products[i].q1 < 100)
cout << " " << products[i].q1;
else
cout << " " << products[i].q1;
if (products[i].q1 < 0 && products[i].q1 < -99)
cout << " " << products[i].q1 << endl;
else if (products[i].q1 < 0 && products[i].q1 > -99)
cout << " " << products[i].q1 << endl;
else if (products[i].q1 > 0 && products[i].q1 < 100)
cout << " " << products[i].q1 << endl;
else
cout << " " << products[i].q1 << endl;
for (i = 0; i < 10; i++)
profititem(products[i]);
}
}
infile.close();
}
void profititem(product products)
{
int total;
total = products.q1 + products.q2 + products.q3 + products.q4;
cout << total << endl;
}
Last edited on Apr 25, 2011 at 3:04am UTC
Apr 25, 2011 at 3:04am UTC
Oh, so i was missing another close bracket?
I didn't realize it was inside the main program, thanks.
Apr 25, 2011 at 3:55am UTC
err, wait i see now how it was still inside.
Thanks again.