EvilZone

Programming and Scripting => C - C++ => : gh0st April 09, 2011, 10:25:36 PM

: [C++]help with dynamic structure!(solved)
: gh0st 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 :

:
#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:

:
#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;
}


: Re: help with dynamic structure!
: gh0st April 09, 2011, 11:25:50 PM
:
#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
: Re: help with dynamic structure!
: bluechill 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:

:
        strcpy(fish_ptr->kind, kind);

use

:
        strncpy(fish_ptr->kind, kind, 19);