Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

dabowles

Problems with Structure Arrays

This topic is 6111 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I am currently learning C++ in ultimate hopes to begin programming games. Right now I am having difficulty initializing a Structure Array and then having a user populate 3 of the variables for the Structure Array. When I attempt to do so, I get the compiler error: D:\C++ Primer Plus Examples\pizzastruct\pizzastruct.cpp(19) : error C2440: ''initializing'' : cannot convert from ''char [20]'' to ''char'' This conversion requires a reinterpret_cast, a C-style cast or function-style cast Here is my code: #include using namespace std; //initialization of structure struct pizza { char brandname[20]; double diameter; double weight; }; int main() { pizza kind[3] = { {kind[0].brandname, kind[0].diameter, kind[0].weight}, {kind[1].brandname, kind[1].diameter, kind[1].weight}, {kind[2].brandname, kind[2].diameter, kind[2].weight} }; cout << "Pizza Brand Name: "; cin.getline(kind[0].brandname, 20); cout << "\nPizza Diameter: "; cin >> kind[0].diameter; cout << "\nPizza Weight: "; cin >> kind[0].weight; cout << "\n\n\n"; cout << "Pizza Brand Name: "; cin.getline(kind[1].brandname, 20); cout << "\nPizza Diameter: "; cin >> kind[1].diameter; cout << "\nPizza Weight: "; cin >> kind[1].weight; cout << "\n\n\n"; cout << "Pizza Brand Name: "; cin.getline(kind[2].brandname, 20); cout << "\nPizza Diameter: "; cin >> kind[2].diameter; cout << "\nPizza Weight: "; cin >> kind[2].weight; cout << "\n\n\n"; //display of struct cout << "Pizza Brand Name: " << kind[0].brandname; cout << "\nPizza Diameter: " << kind[0].diameter; cout << "\nPizza Weight: " << kind[0].weight; cout << "\n\n\n"; cout << "Pizza Brand Name: " << kind[1].brandname; cout << "\nPizza Diameter: " << kind[1].diameter; cout << "\nPizza Weight: " << kind[1].weight; cout << "\n\n\n"; cout << "Pizza Brand Name: " << kind[2].brandname; cout << "\nPizza Diameter: " << kind[2].diameter; cout << "\nPizza Weight: " << kind[2].weight; cout << "\n\n\n"; return 0; } If anyone has the time and the patience to tell me what I have done wrong here, it would be greatly appreciated. All of my code, I think, looks good, but then of course I am a newbie and the compiler error doesn''t tell me too much. Thanks, David Bowles

Share this post


Link to post
Share on other sites
Advertisement
Firstly, the equal operator (=) does not work with character arrays (since they''re actually pointers). To keep it simple: include string.h and use strcpy to put a string into an array. For example:
  
#include <string.h>


/* ... */

char AString[20];
strcpy(AString,"Fish!");

Secondly, why are you doing this:
  
pizza kind[3] = {
{kind[0].brandname, kind[0].diameter, kind[0].weight},
{kind[1].brandname, kind[1].diameter, kind[1].weight},
{kind[2].brandname, kind[2].diameter, kind[2].weight}
};

You''re not actually initializing them to any values. Try doing this instead:
  
pizza kind[3];
strcpy(kind[0].brandname,"Alpha");
kind[0].diameter = 15;
kind[0].weight = 100;
/* Et cetera */

Or (looking more into what you want) simply don''t initialize them at all since you''re going to read in that data from the user anyway. BTW: A for loop would make your input much easier .

[Resist Windows XP''s Invasive Production Activation Technology!]

Share this post


Link to post
Share on other sites
That makes a lot better sense, I was under the impression that structures had to be declared explicitly regardless whether or not you would be initializing them to a value at compile-time or whether a user would be populating those values at run-time. I''ll go with what you suggested and see if that doesn''t fix it. Thanks again for your help.

David

Share this post


Link to post
Share on other sites
This block doesn''t make any sense to me:
  
pizza kind[3] =
{

{kind[0].brandname, kind[0].diameter, kind[0].weight},
{kind[1].brandname, kind[1].diameter, kind[1].weight},
{kind[2].brandname, kind[2].diameter, kind[2].weight}

};


This should do what you need:
struct pizza kind[3];

And it will work. But watch out with using a cin.getline after a "cin >> ..." expression. The getline will take the return from the cin expression as the input and return.

Share this post


Link to post
Share on other sites
Hi,
I know I probably went around my rear to get to my elbow but this is what I came up with:

// pizzastruct.cpp -- Pizza structure that accepts input and displays to screen.

#include
#include
using namespace std;



//initialization of structure
struct pizza
{
char brandname[20];
double diameter;
double weight;
};

int main()
{

char name1[20];
char name2[20];
char name3[20];
double diameter1;
double diameter2;
double diameter3;
double weight1;
double weight2;
double weight3;

cout << "Pizza Brand Name: ";
cin.getline(name1, 20);

cout << "\nPizza Diameter: ";
cin >> diameter1;

cout << "\nPizza Weight: ";
cin >> weight1;
cout << "\n\n\n";

cin.get();
cout << "Pizza Brand Name: ";
cin.getline(name2, 20);

cout << "\nPizza Diameter: ";
cin >> diameter2;

cout << "\nPizza Weight: ";
cin >> weight2;
cout << "\n\n\n";

cin.get();
cout << "Pizza Brand Name: ";
cin.getline(name3, 20);

cout << "\nPizza Diameter: ";
cin >> diameter3;

cout << "\nPizza Weight: ";
cin >> weight3;
cout << "\n\n\n";

pizza kind[3];
strcpy(kind[0].brandname, name1);
kind[0].diameter = diameter1;
kind[0].weight = weight1;
strcpy(kind[1].brandname, name2);
kind[1].diameter = diameter2;
kind[1].weight = weight2;
strcpy(kind[2].brandname, name3);
kind[2].diameter = diameter3;
kind[2].weight = weight3;

//display of struct
cout << "Pizza Brand Name: " << kind[0].brandname;
cout << "\nPizza Diameter: " << kind[0].diameter;
cout << "\nPizza Weight: " << kind[0].weight;
cout << "\n\n\n";
cout << "Pizza Brand Name: " << kind[1].brandname;
cout << "\nPizza Diameter: " << kind[1].diameter;
cout << "\nPizza Weight: " << kind[1].weight;
cout << "\n\n\n";
cout << "Pizza Brand Name: " << kind[2].brandname;
cout << "\nPizza Diameter: " << kind[2].diameter;
cout << "\nPizza Weight: " << kind[2].weight;
cout << "\n\n\n";

return 0;
}


I would use an loop in this program, but this book I am working the exercise in (without a solution to this particular exercise) goes over loops in the next chapter, so to keep things simple, I went the long way. It executes good and does what it should. Minus simplifying it via a loop or any other advanced techniques, is there anything I could do to the program to refine it?

Thanks again,
David

Share this post


Link to post
Share on other sites
Your first version was correct.... just remove the struct initialization (after the = on the "pizza kind[3]" declaration).

I really don''t know why you added all these name1, name2, etc. variables...

Here is a complete explanation for the error message: struct initilization is made member by member. In your struct, you have 22 member: 20 chars and 2 doubles. This line:
pizza kind = {kind[0].brandname, kind[0].diameter, kind [0].weight};
tries to initialize the 3 first chars of the string with a char[20] and 2 doubles and fills the others members with zeroes...
By the way, it''s completely meaningless to initialize a struct with its own (uninitialized) member...

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!