473,398 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

why doesnt this work?

#ifndef PERSON_H
#define PERSON_H

#include<iostream>
#include<iomanip>
#include<string>

class Person {
protected:
char name[12];
name = jamesbond;

public:
Person();
void Print();
};//close Person
#endif

or this:

#include "person.h"

Person::Person()
{
name = "jamesbond";
cout<<"The student's name is: "<<name;
}

--------------------------------------------------
*** E-mail is shut off ***
--------------------------------------------------
Jul 22 '05 #1
4 1596
Hi,

Either use string (preffered way):

string name( "jamesbond" );

Or

strncpy( name, "jamesbond", sizeof name );
or
char *name="jamesbond";

Regards, Ron AF Greve
]
"Developwebsites" <de*************@aol.com> wrote in message
news:20***************************@mb-m27.aol.com...
#ifndef PERSON_H
#define PERSON_H

#include<iostream>
#include<iomanip>
#include<string>

class Person {
protected:
char name[12];
name = jamesbond;

public:
Person();
void Print();
};//close Person
#endif

or this:

#include "person.h"

Person::Person()
{
name = "jamesbond";
cout<<"The student's name is: "<<name;
}

--------------------------------------------------
*** E-mail is shut off ***
--------------------------------------------------

Jul 22 '05 #2
"Moonlit" <al******@jupiter.universe> wrote...
Either use string (preffered way):

string name( "jamesbond" );
Since 'name' is a member, you cannot initialise it that way.
It has to be done in the constructor member initialisation
list. Nonetheless, using string is A VERY GOOD IDEA(tm).

Or

strncpy( name, "jamesbond", sizeof name );
Why are you copying 'sizeof name' bytes? What if there are
more elements in 'name' than 10? You will try accessing memory
beyond the end of the string literal, which causes undefined
behaviour. If you need to copy, (a) make sure you have enough
elements in 'name' and (b) use 'strcpy', which only copies as
many chars as there are in the second argument, plus the
terminating null character.

strcpy(name, "jamesbond");

Of course, if there is not enough elements in 'name', you end
up overrunning memory, which is also undefined behaviour. So,
all things considered, strings are much better alternative,
aren't they?
or
char *name="jamesbond";
It's much better to NEVER declare a pointer to _non-const_ char
and initialise it with a literal. This nonsense is dragging
into C++ from the old C days, and should be avoided. Use

const char * name = "jamesbond";

And, again, if it's a member, it cannot be initialised that way,
blah blah blah

Just my $0.04...

Regards, Ron AF Greve
]
"Developwebsites" <de*************@aol.com> wrote in message
news:20***************************@mb-m27.aol.com...
#ifndef PERSON_H
#define PERSON_H

#include<iostream>
#include<iomanip>
#include<string>

class Person {
protected:
char name[12];
name = jamesbond;

public:
Person();
void Print();
};//close Person
#endif

or this:

#include "person.h"

Person::Person()
{
name = "jamesbond";
cout<<"The student's name is: "<<name;
}

--------------------------------------------------
*** E-mail is shut off ***
--------------------------------------------------


Jul 22 '05 #3
On Sun, 14 Dec 2003 19:57:52 +0000, Victor Bazarov wrote:

strncpy( name, "jamesbond", sizeof name );


Why are you copying 'sizeof name' bytes? What if there are more
elements in 'name' than 10? You will try accessing memory beyond the
end of the string literal, which causes undefined behaviour. If you
need to copy, (a) make sure you have enough elements in 'name' and (b)
use 'strcpy', which only copies as many chars as there are in the second
argument, plus the terminating null character.


This is wrong. strncpy(dst,src,n) will stop when either a null character
is found in src or when n bytes have been copied, hence there is no
undefined behaviour here (but note that strncpy doesn't add a terminating
null to dst if strlen(src) >= n).
Jul 22 '05 #4
The reason this doesnt work is because you declared name[12] as a char
array, which doesnt have a = operator. as mentioned before, you have 2
options, 1. declare name as a string. 2. use strcpy/strncpy instead of the =
operatior
"Developwebsites" <de*************@aol.com> wrote in message
news:20***************************@mb-m27.aol.com...
#ifndef PERSON_H
#define PERSON_H

#include<iostream>
#include<iomanip>
#include<string>

class Person {
protected:
char name[12];
name = jamesbond;

public:
Person();
void Print();
};//close Person
#endif

or this:

#include "person.h"

Person::Person()
{
name = "jamesbond";
cout<<"The student's name is: "<<name;
}

--------------------------------------------------
*** E-mail is shut off ***
--------------------------------------------------

Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Station Media | last post by:
Hi, here my problem, i use stored procedure in ACCESS database(latest version), and i would like to use this procedure but it doesnt work, do you know why ? Procedure: PARAMETERS MyField Text...
3
by: Clouds | last post by:
Hi ! How do I add the dynamic event handler for a dropdownlist present in the itemtemplate of a datalist !! I am doing it in the itemdatabound event of the datalist but it doesnt work... I am...
8
by: pmud | last post by:
Hi, I am using a compare validator in asp.net application(c# code). This Custom validator is used for comparing a value enterd by the user against the primary key in the SQL database. IF the...
3
by: Bruno Paquette | last post by:
I have installed visual studio .net and now i started to write the first few examples from the 70-315 book by Kalani. It looks like all server side code that i put between <% %> doesnt process...
0
by: Juna | last post by:
I have been working in vs2003, but now started to work in vs2005 but the problem, I have simple web application not website, which work i mean open in browser when we press F5 or run the...
1
Digital Don
by: Digital Don | last post by:
I am writing a program for Peg solitaire... To check for no repetition of previous states I use a Set for storage of Board states.. The pronblem is when I declare the set as type char i.e. set...
3
by: jx2 | last post by:
hi guys i would appriciate your coments on this code - when i ran it for the very first time it doesnt see @last = LAST_INSERT_ID() but when i ran it next time it read it properly i need to know it...
1
by: Dany13 | last post by:
hi all. i using some text box for input value and some localvarible for passing this data to dataset . give instance for correct row of dataset and data in data table . use one gird view for...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.