1. You could check for name.empty() to see if there are any characters in it.
2. There''s indeed no getline member function of istream that takes a string. However, there''s a global function getline that takes a istream (cin in this case) and a string. Maybe that''ll work.
Erik
strings...damn them
To make sure the string in under 30 characters in length AND include spaces, use:
cin.getline(name, 30);
Happy programming!
- Daniel
cin.getline(name, 30);
Happy programming!
- Daniel
- DanielMy homepage
quote: Original post by CoiN
DOH!!!
Ignore my post i was thinking of somin like:
char *p;
gets(p);
printf ("%s", p);
Thats the type of thing i was reffering too not bishops eg sorry
Messing with memory that hasn't been initiliazed is _not_ good. p might be pointing to NULL or it might be pointing to 0x52532. Who knows? If you change that memory, and some other program has stored something important there, that program will most likely not work since you might have changed something important. But of course, if p is pointing to some free memory address and you change it, nothing bad happens (at least it should, I think ), but it's still really bad to do it.
- Muzzafarath
Mad House Software
The Field Marshals
Edited by - Muzzafarath on July 6, 2000 9:12:24 AM
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
I've seen this so many times on this board that I sometimes doubt myself. But my understanding of declaring a char* is this:
When you declare:
char *p;
you have a single char pointing somewhere in memory. There is of course lots of memory around this single space. Let us venture to diagram 1 (I don't know how well this is going to come out):
There is p. You have no idea what is in any of the xx's. It could be unused memory, memory used by windows (although I think windows protects its memory), memory belonging to other programs, or memory you are using elsewhere in the program. So now say you do:
char *p = "happy";
You would be overwriting whatever was in the xx's, thus losing the potentially important data. There is no law saying you can't do this. You don't have to delete memory you have dynamically allocated either, but I wouldn't suggest it.
So anyway, in my understanding, you are going to want to do the following:
char p[6] = "happy";
or
char *p = new char[6];
strcpy(p, "HAPPY");
(then delete[] p; at some point)
If I'm wrong (it happens more than I like to admit ) ignore this and feel quite free to correct my error.
Happy coding,
-> Briar LoDeran <-
Edited by - BriarLoDeran on July 6, 2000 10:25:11 AM
When you declare:
char *p;
you have a single char pointing somewhere in memory. There is of course lots of memory around this single space. Let us venture to diagram 1 (I don't know how well this is going to come out):
---- ---- ---- ---- ---- / / / / / / / / / / / / / / / / / / / /---- ---- ---- ---- ---- ... and so on... p xx xx xx xx
There is p. You have no idea what is in any of the xx's. It could be unused memory, memory used by windows (although I think windows protects its memory), memory belonging to other programs, or memory you are using elsewhere in the program. So now say you do:
char *p = "happy";
---- ---- ---- ---- ---- / / / / / / / / / / /H / /A / /P / /P / /Y / ---- ---- ---- ---- ---- p xx xx xx xx
You would be overwriting whatever was in the xx's, thus losing the potentially important data. There is no law saying you can't do this. You don't have to delete memory you have dynamically allocated either, but I wouldn't suggest it.
So anyway, in my understanding, you are going to want to do the following:
char p[6] = "happy";
or
char *p = new char[6];
strcpy(p, "HAPPY");
(then delete[] p; at some point)
If I'm wrong (it happens more than I like to admit ) ignore this and feel quite free to correct my error.
Happy coding,
-> Briar LoDeran <-
Edited by - BriarLoDeran on July 6, 2000 10:25:11 AM
The equivalent C code:
char *pstr;
pstr = (char *)malloc(sizeof(how_much_you_want_to_allocate));
pstr = "The string you want";
puts(pstr);
-----------------------------
A wise man once said "A person with half a clue is more dangerous than a person with or without one."
char *pstr;
pstr = (char *)malloc(sizeof(how_much_you_want_to_allocate));
pstr = "The string you want";
puts(pstr);
-----------------------------
A wise man once said "A person with half a clue is more dangerous than a person with or without one."
-----------------------------A wise man once said "A person with half a clue is more dangerous than a person with or without one."The Micro$haft BSOD T-Shirt
quote: Original post by BriarLoDeran
I've seen this so many times on this board that I sometimes doubt myself. But my understanding of declaring a char* is this:
When you declare:
char *p;
you have a single char pointing somewhere in memory. There is of course lots of memory around this single space. Let us venture to diagram 1 (I don't know how well this is going to come out):---- ---- ---- ---- ---- / / / / / / / / / / / / / / / / / / / /---- ---- ---- ---- ---- ... and so on... p xx xx xx xx
There is p. You have no idea what is in any of the xx's. It could be unused memory, memory used by windows (although I think windows protects its memory), memory belonging to other programs, or memory you are using elsewhere in the program. So now say you do:
char *p = "happy";---- ---- ---- ---- ---- / / / / / / / / / / /H / /A / /P / /P / /Y / ---- ---- ---- ---- ---- p xx xx xx xx
You would be overwriting whatever was in the xx's, thus losing the potentially important data. There is no law saying you can't do this. You don't have to delete memory you have dynamically allocated either, but I wouldn't suggest it.
So anyway, in my understanding, you are going to want to do the following:
char p[6] = "happy";
or
char *p = new char[6];
strcpy(p, "HAPPY");
(then delete[] p; at some point)
If I'm wrong (it happens more than I like to admit ) ignore this and feel quite free to correct my error.
Happy coding,
-> Briar LoDeran <-
Edited by - BriarLoDeran on July 6, 2000 10:25:11 AM
Briar,
char *p = "Happy";
"Happy" is a chunk of data in your program somewhere. 6 bytes to be precise. The reason it exists in your program is because at compile time (not runtime) The compiler set aside 6 bytes in your program for it.
Let's look at these 2 statements: The two statements below do what the 1 statement above does.
char *p;
p = "Happy";
The first statement makes a pointer initialized to some random address.
The second statement assigns the address of where the 6 bytes reside to p.
Remember this:! At compile time, all quoted strings evaluate and reduce to a pointer. That is why all of the below are legal:
char *p = "Happy"; // evals to a pointer
char *p = "Happy" + 0; // evals to pointer pointing to "Happy"
char *p = "Happy" + 3; // evals to pointer pointing to "py"
char p = "Happy"[4]; // evals to char 'y'
char p = "*("Happy" + 2); evals to char 'p'
Edited by - bishop_pass on July 6, 2000 12:00:09 PM
_______________________________
"To understand the horse you'll find that you're going to be working on yourself. The horse will give you the answers and he will question you to see if you are sure or not."
- Ray Hunt, in Think Harmony With Horses
ALU - SHRDLU - WORDNET - CYC - SWALE - AM - CD - J.M. - K.S. | CAA - BCHA - AQHA - APHA - R.H. - T.D. | 395 - SPS - GORDIE - SCMA - R.M. - G.R. - V.C. - C.F.
"To understand the horse you'll find that you're going to be working on yourself. The horse will give you the answers and he will question you to see if you are sure or not."
- Ray Hunt, in Think Harmony With Horses
ALU - SHRDLU - WORDNET - CYC - SWALE - AM - CD - J.M. - K.S. | CAA - BCHA - AQHA - APHA - R.H. - T.D. | 395 - SPS - GORDIE - SCMA - R.M. - G.R. - V.C. - C.F.
>> "Happy" is a chunk of data in your program somewhere. 6 bytes to be precise. The reason it exists in your program is because at compile time (not runtime) The compiler set aside 6 bytes in your program for it. <<
Not at runtime? What? Memory is allocated when you run your program, not when you compile it. Right?
- Muzzafarath
Mad House Software
The Field Marshals
Not at runtime? What? Memory is allocated when you run your program, not when you compile it. Right?
- Muzzafarath
Mad House Software
The Field Marshals
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
Muzzafarath,
This is not memory allocation which DOES occur at runtime.
Your way:
Program starts. 6 bytes are allocated. "Happy" which is data somewhere in your program is then copied into the newly allocated memory.
The data "Happy" already existed. Why bother copying it into another 6 bytes?
The way it is really done:
Program starts. "Happy" is data somewhere in your program. End of story.
This is not memory allocation which DOES occur at runtime.
Your way:
Program starts. 6 bytes are allocated. "Happy" which is data somewhere in your program is then copied into the newly allocated memory.
The data "Happy" already existed. Why bother copying it into another 6 bytes?
The way it is really done:
Program starts. "Happy" is data somewhere in your program. End of story.
_______________________________
"To understand the horse you'll find that you're going to be working on yourself. The horse will give you the answers and he will question you to see if you are sure or not."
- Ray Hunt, in Think Harmony With Horses
ALU - SHRDLU - WORDNET - CYC - SWALE - AM - CD - J.M. - K.S. | CAA - BCHA - AQHA - APHA - R.H. - T.D. | 395 - SPS - GORDIE - SCMA - R.M. - G.R. - V.C. - C.F.
"To understand the horse you'll find that you're going to be working on yourself. The horse will give you the answers and he will question you to see if you are sure or not."
- Ray Hunt, in Think Harmony With Horses
ALU - SHRDLU - WORDNET - CYC - SWALE - AM - CD - J.M. - K.S. | CAA - BCHA - AQHA - APHA - R.H. - T.D. | 395 - SPS - GORDIE - SCMA - R.M. - G.R. - V.C. - C.F.
Muzzafarath, "happy" is a constant string that never changes.. (at least not during compile time... it doesn''t depend on any other variables or whatever..) so what the compiler does is it allocates 6 bytes for the # of chars (including NULL at the end)... if you were to convert the following C/C++ code
char* str = "happy";
or
char str[] = "happy";
or even
char str[6] = "happy";
to assembler code, you''d get something like this...
str db "happy",0
which is not saved on the heap (dynamic memory) but instead is saved in your program''s memory... as you can see it is a constant value during compilation time...
btw, working with code like this
char* str;
printf("Enter your name: ");
gets(str);
....
is dangerous, because you haven''t allocated any memory for the string, and str is simply a pointer to a random byte in your computer... and when you try to change the memory at that location, you''ll get messed up.. (you''ll get a GPF under Windows).. DOS might leave you alone for a while, but once it crashes you can feel the crash
Hope this helps!
..-=ViKtOr=-..
char* str = "happy";
or
char str[] = "happy";
or even
char str[6] = "happy";
to assembler code, you''d get something like this...
str db "happy",0
which is not saved on the heap (dynamic memory) but instead is saved in your program''s memory... as you can see it is a constant value during compilation time...
btw, working with code like this
char* str;
printf("Enter your name: ");
gets(str);
....
is dangerous, because you haven''t allocated any memory for the string, and str is simply a pointer to a random byte in your computer... and when you try to change the memory at that location, you''ll get messed up.. (you''ll get a GPF under Windows).. DOS might leave you alone for a while, but once it crashes you can feel the crash
Hope this helps!
..-=ViKtOr=-..
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement