This forum is in archive mode. You will not be able to post new content.

Author Topic: [C++]dynamic structure/variables help!(solved)  (Read 2919 times)

0 Members and 1 Guest are viewing this topic.

Offline gh0st

  • Sir
  • ***
  • Posts: 575
  • Cookies: 8
  • #DEDSec
    • View Profile
[C++]dynamic structure/variables help!(solved)
« on: April 11, 2011, 11:46:30 PM »
well guys Im just doing an exercise I already did it but It doesnt seem to work correctly well basicly are 2 exercises check it out:

5. The CandyBar structure contains three members. The first member holds the brand
name of a candy bar. The second member holds the weight (which may have a fractional
part) of the candy bar, and the third member holds the number of calories (an integer
value) in the candy bar. Write a program that declares such a structure and creates a
CandyBar variable called snack, initializing its members to “Mocha Munch”, 2.3, and
350, respectively. The initialization should be part of the declaration for snack. Finally,
the program should display the contents of the snack variable.

6. The CandyBar structure contains three members, as described in the previus exercise Write a program that creates an array of three CandyBar structures, initializes them to
values of your choice, and then displays the contents of each structure.


so I did:

Code: [Select]
#include <iostream>
#include <string.h>

struct barsnames
{
    char candyname[20];
    float weight;
    int calories;
};
int main()
{
    using namespace std;
    char candyname[20] = "Mocha Munch";
    float weight = 2.3;
    int calories = 350;
   
    barsnames * barsnames_ptr = new barsnames;
    strcpy(barsnames_ptr->candyname, candyname);
    barsnames_ptr->weight, weight;
    barsnames_ptr->calories, calories;
   
    cout << (*barsnames_ptr).candyname;
    cout << endl;
    cout <<  barsnames_ptr->weight;
    cout << endl;
    cout << barsnames_ptr->calories;
    cout << endl;
   
    delete barsnames_ptr;
    system("pause");
    return 0;
}

and I did this to the question 6:

Code: [Select]
#include <iostream>
#include <string.h>

struct barsnames
{
    char candyname[20];
    float weight;
    int calories;
};
int main()
{
    using namespace std;
    char arrayall[2];
    char arrayall[0] = candynameS;
    char arrayall[1] = weightS;
    char arrayall[2] = caloriesS;
   
    barsnames * barsnames_ptr = new barsnames;
    strcpy(barsnames_ptr->candynameS, candyname);
    strcpy(barsnames_ptr->weightS, weight);
    strcpy(barsnames_ptr->caloriesS, calories);
   
    cout << arrayall[3];
    cout << endl;
   
    delete barsnames_ptr;
    system("pause");
    return 0;
}

well at least I get an output on my first source code but at the second one it doesnt even compile can someone help me plz? thanx in advance
« Last Edit: April 13, 2011, 05:50:50 AM by gh0st »

Offline Satan911

  • VIP
  • Knight
  • *
  • Posts: 289
  • Cookies: 25
  • Retired god/admin
    • View Profile
Re: dynamic structure/variables help!
« Reply #1 on: April 12, 2011, 01:10:51 AM »
They want you to create an array of barsnames. That's not what you are doing.

Try:
Code: [Select]
barsnames * barsnames_array_ptr = new barsnames[3];
And then to delete it you'll have to use:
Code: [Select]
delete [] barnames_array_ptr;
Go on from there.. Basically the same as the example but for each item of your array.
Satan911
Evilzone Network Administrator

Offline gh0st

  • Sir
  • ***
  • Posts: 575
  • Cookies: 8
  • #DEDSec
    • View Profile
Re: dynamic structure/variables help!
« Reply #2 on: April 12, 2011, 02:49:18 AM »
well Satan911 first of all thanx for your support this time the answer seems to be something like this:

Code: [Select]
#include <iostream>
struct inflatable
{
char name[20];
float volume;
double price;
};
int main()
{
using namespace std;
inflatable guests[2] = // initializing an array of structs
{
{“Bambi”, 0.5, 21.99}, // first structure in array
{“Godzilla”, 2000, 565.99} // next structure in array
};
cout << “The guests “ << guests[0].name << “ and “ << guests[1].name
<< “\nhave a combined volume of “
<< guests[0].volume + guests[1].volume << “ cubic feet.\n”;
return 0;
}

well I tried to code the answer but I get an error here we go:

Code: [Select]
#include <iostream>

struct barsnames
{
    char candyname[20];
    float weight;
    int calories;
};
int main()
{
    using namespace std;
    barsnames hi[2] =
    {
              {"snack",0},
              {"weight",2.3,0}
              {"calories",350}
};
cout << "CandyName: " << hi[0].candyname << " , weight: " << hi[1].weight << " , calories: " << hi[2].calories;
cout << endl;
    system("pause");
    return 0;
}

look at :

Code: [Select]
{"snack",0},
              {"weight",2.3,0}
              {"calories",350}
I think that I did something wrong there it should look like this:

Code: [Select]
{"snack",here comes something},
              {"2.3",her comes something}
              {"350",here comes something}

well from the book we have got this:

Code: [Select]
inflatable guests[2] = // initializing an array of structs
{
{“Bambi”, 0.5, 21.99}, // first structure in array
{“Godzilla”, 2000, 565.99} // next structure in array
};

Idk what are those numbers but I doubt that are something related with memory store
« Last Edit: April 12, 2011, 05:05:10 AM by gh0st »

Offline gh0st

  • Sir
  • ***
  • Posts: 575
  • Cookies: 8
  • #DEDSec
    • View Profile
Re: dynamic structure/variables help!
« Reply #3 on: April 13, 2011, 12:08:43 AM »
Satan look what Ive found this is probably the answer to my question:

Code: [Select]
#include <iostream>

struct barsnames
{
    char candyname[20];
    float weight;
    int calories;
};
int main()
{
    using namespace std;
    barsnames hi[3] =
    {
              {"snack"},
              {"weight",2.3},
              {"calories",350}
};
cout << "CandyName: " << hi[1].candyname << " , weight: " << hi[2].weight << " , calories: " << hi[3].calories;
cout << endl;
    system("pause");
    return 0;
}

I knew it that part is something related with the memory I know that thats not the answer but at least I get an output but I still dont underestanding how this statement works so If someone could help me plz I would be really grateful

Offline Satan911

  • VIP
  • Knight
  • *
  • Posts: 289
  • Cookies: 25
  • Retired god/admin
    • View Profile
Re: dynamic structure/variables help!
« Reply #4 on: April 13, 2011, 01:56:00 AM »
It's that simple:
Code: [Select]
#include <string>
#include <iostream>

using namespace std;

struct barsnames
{
    string candyname;
    float weight;
    int calories;
};

int main()
{

   barsnames *barsnames_ptr = new barsnames[3];
   
   barsnames_ptr[0].candyname = "Oh Henry";
   barsnames_ptr[0].weight = 2.0;
   barsnames_ptr[0].calories = 200;

   barsnames_ptr[1].candyname = "Caramilk";
   barsnames_ptr[1].weight = 2.5;
   barsnames_ptr[1].calories = 400;

   barsnames_ptr[2].candyname = "Mr.Big";
   barsnames_ptr[2].weight = 3.0;
   barsnames_ptr[2].calories = 500;
   
   for(int i=0; i<3; i++)
   {
      cout  << "Candy" << i+1 << " name: "     << barsnames_ptr[i].candyname << endl
            << "Candy" << i+1 << " weight: "   << barsnames_ptr[i].weight    << endl
            << "Candy" << i+1 << " calories: " << barsnames_ptr[i].calories  << endl << endl;
   }

   delete [] barsnames_ptr;

   return 0;
}

I took the liberty to change the char to a string because I don't see why you would use a char anyway.
« Last Edit: April 13, 2011, 01:58:16 AM by Satan911 »
Satan911
Evilzone Network Administrator

Offline gh0st

  • Sir
  • ***
  • Posts: 575
  • Cookies: 8
  • #DEDSec
    • View Profile
Re: dynamic structure/variables help!
« Reply #5 on: April 13, 2011, 01:59:24 AM »
thank you very much Satan +1 for you

 



Want to be here? Contact Ande, Factionwars or Kulverstukas on the forum or at IRC.