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

Author Topic: [C++]help with dynamic structure!(solved)  (Read 2196 times)

0 Members and 1 Guest are viewing this topic.

Offline gh0st

  • Sir
  • ***
  • Posts: 575
  • Cookies: 8
  • #DEDSec
    • View Profile
[C++]help with dynamic structure!(solved)
« on: April 09, 2011, 10:25:36 PM »
hello guys well Im on the chapter of compound data and Im making the questions so here we go:

7.Write a code fragment that dynamically allocates a structure of the type described in
Question 6 and then reads a value for the kind member of the structure.

...6.Devise a structure declaration that describes a fish. The structure should include the
kind, the weight in whole ounces, and the length in fractional inches.
..

well I did :

Code: [Select]
#include <iostream>

struct inflatable
{
    char kind[20] = "barracuda";
    float weight = 23;
    double lenght = 21;
};
int main()
{
    inflatable * fish = new inflatable;
    using namespace std;
    cout << (*fish).kind;
    cout << endl;
    cout << fish->weight;
    cout << endl;
    cout << fish->lenght;
    delete fish;
    return 0;
}


its horrible I know  :P

but it is wrong there I need to use something  about "dynamic structures using ´new´" well Ive the teory on the book but that part doesnt give me an example so it makes it harder to underestand so if anyone can help me plz I will be grateful tnx in advance.

an example of dynamic structure from the book:

Code: [Select]
#include <iostream>
struct inflatable // structure template
{
char name[20];
float volume;
double price;
};
int main()
{
using namespace std;
inflatable * ps = new inflatable; // allot memory for structure
cout << “Enter name of inflatable item: “;
cin.get(ps->name, 20); // method 1 for member access
cout << “Enter volume in cubic feet: “;
cin >> (*ps).volume; // method 2 for member access
cout << “Enter price: $”;
cin >> ps->price;
cout << “Name: “ << (*ps).name << endl; // method 2
cout << “Volume: “ << ps->volume << “ cubic feet\n”; // method 1
cout << “Price: $” << ps->price << endl; // method 1
delete ps; // free memory used by structure
return 0;
}


« Last Edit: April 13, 2011, 05:52:31 AM by gh0st »

Offline gh0st

  • Sir
  • ***
  • Posts: 575
  • Cookies: 8
  • #DEDSec
    • View Profile
Re: help with dynamic structure!
« Reply #1 on: April 09, 2011, 11:25:50 PM »
Code: [Select]
#include <iostream>
#include <string.h>

struct fish
{
     char kind[20];
     int weight;
     int lenght;
};
int main()
{
    using namespace std;
    char kind[20] = "barracuda";
    int weight = 23;
    int lenght = 21;
    fish *fish_ptr = new fish;

        strcpy(fish_ptr->kind, kind);
        fish_ptr->weight = weight;
        fish_ptr->lenght = lenght;

    cout << (*fish_ptr).kind;
    cout << endl;
    cout << fish_ptr->weight;
    cout << endl;
    cout << fish_ptr->lenght;
        cout << endl;

    delete fish_ptr;
    system("pause");
    return 0;
}

finally its done thanx  "strcopy" saved us this time!  ;D
thanx to cludthi without him I would never finish this program
« Last Edit: April 09, 2011, 11:26:50 PM by gh0st »

Offline bluechill

  • Cybermancer
  • Royal Highness
  • ****
  • Posts: 682
  • Cookies: 344
  • I am the existence in these walls
    • View Profile
Re: help with dynamic structure!
« Reply #2 on: April 10, 2011, 05:57:27 PM »
I'd recommend instead of using strcpy to use strncpy.  That way you will avoid errors related to overflows if you happen to put in a string which is greater than 20 characters.  So instead of:

Code: [Select]
        strcpy(fish_ptr->kind, kind);

use

Code: [Select]
        strncpy(fish_ptr->kind, kind, 19);
I have dreamed a dream, but now that dream has gone from me.  In its place now exists my own reality, a reality which I have created for myself by myself.

 



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