Sign in to follow this  
DeathLetum

Structure

Recommended Posts

DeathLetum    127
ok was just wondering a few things about this block of code. #include <iostream> using namespace std; struct character { int age; char gender; char *name; }; main() { character zak; zak.age = 22; zak.gender = 'z'; zak.name = new char[80]; // What this mean is that will be able to write a name that has up to 80 letters in it right? and i cant go over 80. cout << "Enter zak's new name: "; cin >> zak.name; cout << "Zak's name: " << zak.name << endl; cout << "Zak's age: " << zak.age << endl; delete zak.name; // Since i used the referance operator on *name this means i will have to deleted zak.name. or i will end up with memory issues. }

Share this post


Link to post
Share on other sites
rip-off    10976
You will be able to store 79 usuable characters, as all routines that take c-style strings require that the final character is '\0' or ( the NUL character, different from NULL ).

I would recommend instead use of std::string, its much easier and has no manual memory handling.

std::string has no practical size limitation, so you can store names of any size in it.


#include <iostream>
#include <string>
using namespace std;


struct character
{
int age;
char gender;
std::string name;
};


main()
{
character zak;

zak.age = 22;
zak.gender = 'z';

cout << "Enter zak's new name: ";
cin >> zak.name;
cout << "Zak's name: " << zak.name << endl;


cout << "Zak's age: " << zak.age << endl;

}


Share this post


Link to post
Share on other sites
Captain P    1092
Yes, definitely go with std::string if you're using C++.

Just a note on your code: you must delete[] what you new[] and delete what you new. :) new[] works a bit different than new, in that there's some additional overhead for the array managing. delete[] takes that into account, but delete simply assumes it's a single object.

So:

char* a = new char[80];
delete [] a;

char* b = new char;
delete b;

Share this post


Link to post
Share on other sites
Zahlman    1682
As well, you do need int main() in C++.

In terms of design, though, you usually want to avoid declaring objects until you know everything that's needed to construct them (in C++, you don't have to put the declarations at the top of their scope, which makes this much easier overall, and possible in more cases), and then initialize them, by making use of a constructor, which in turn uses an initializer list.

With this, plus the std::string tip, and a better type for 'gender' (I can't guess what's inside zak's pants if zak's gender is 'z' :) ) we get code like:


#include <iostream>
#include <string>

struct character {
int age;
enum gender_t { MALE, FEMALE } gender;
std::string name;

character(int age, gender_t gender, const std::string& name) :
age(age), gender(gender), name(name) {}
};

using namespace std;

int main() {
cout << "Enter zak's new name: ";
string zak_name;
cin >> zak_name;

character zak(22, MALE, zak_name);

cout << "Zak's name: " << zak.name << endl; // notice, not zak_name :)
cout << "Zak's age: " << zak.age << endl;
}


And we can get even neater by making the struct capable of "printing itself" with an operator<< overload. :)

Share this post


Link to post
Share on other sites
pulpfist    528
Quote:

delete zak.name; // Since i used the referance operator on *name this means i will have to deleted zak.name. or i will end up with memory issues.

This is not correct.
zak.name in itself is a pointer. If the memory zak.name points to was created by new, you will have to release that memory with delete. If not you must not.

A pointer is no more than a fixed size variable containing a memory address. When the compiler see the pointer it will think "Since this is a pointer, Im gonna treat the contents of it as a memory address to something else".

Share this post


Link to post
Share on other sites
wodinoneeye    1689
You could do the name allocation AFTER you read in the name and dump the initial input into a local var with a size bigger than any likely name value (ie- 256 char). Then you could read its size and allocate exactly the right length string to link into your structure.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this