Advertisement

strings...damn them

Started by July 05, 2000 03:18 PM
34 comments, last by Joker2000 24 years, 5 months ago
Ok, this issue is REALLY starting to piss me off! I can't seem to grasp the concept of strings. First of all, is the datatype "string" the same as "char *" ? Well, of course I know that they're not the same, but are they similar (i.e. do they do the same thing)? Maybe it would be best to put this in an example. I'm creating a small console game that asks a user to type in the name of their player. I'm going to store this name in the string variable name. However, the name can't be more than 30 characters. Here's what I have so far (this is of course a brief snippet): void create_new_player() { string name; cout << "Enter the name of your player."; cin >> name; if (strlen(name) > 30) // is this correct strlen syntax? cout << "Name too long. else player p1(name, 100, 0,0) // creates p1 using class "player" } However, if I use the code just as I have entered it above, I get errors dealing with the strlen function: Could not find a match for 'std::strlen(std::basic_string,std::allocator >)'. I have tried other syntax structures such as: strlen.name() strlen(&name) strlen.*name() strlen.*name ...etc etc etc... I have also tried declaring "name" as "char *name" but still it doesn't work. Why is this not working? Please help! BTW, I'm using Borland C++Builder 3.0, if it makes a difference. Blake Young Edited by - Joker2000 on 7/5/00 3:20:18 PM Edited by - Joker2000 on 7/5/00 3:21:15 PM
>> First of all, is the datatype "string" the same as "char *" ? <<

Nope. A string is a string, while a char* is a pointer to a char.

Maybe this will help?:

        void create_new_player(){char name[35]printf("Enter the name of your player.");// Hmm, I don't think I've got the arguments right here, anyone care to help me?fgets(stdin, name, 30)// The above line only reads 30 chars.// Btw, p1 will only be visible in this function, is that really how you want it?player p1(name, 100, 0,0)  // creates p1 using class "player"}        


- Muzzafarath

Mad House Software
The Field Marshals

Edited by - Muzzafarath on July 5, 2000 4:32:15 PM
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
Advertisement
"hello" is a pointer to 6 bytes stored somewhere else in your program.
"hello" is NOT an lvalue. You must have an lvalue on the left hand side of an assignment statement.
All of these are legal:

char *c = "hello";

char c = "hello"[2];

char c = *("hello" + 3);

char *c = "hello" + 3;

Think about this:
Take a function like printf ("howdy!);
printf needs a string for the first argument. What that means, is printf needs a pointer to a string for the first argument.

Thus, printf ("hello"); outputs "hello".

char *c = "hello";
printf (c); outputs "hello".

char *c = "hello" + 2;
printf (c); outputs "llo".

Got it?



_______________________________
"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.
Joker:
''string'' is a class, while ''char *'' is a pointer to a builtin type. There are many member functions to use with ''string'' that will make things easy on you. If you are using the STL string class, I believe that there is a method called length() that will return the length of the string.

So, you''d go:

string s;
...
if(s.length() > 2)
....

or whatever. It''s possible that the member is called size(). I can''t quite remember. It''s been a while since I used it (a LONG while).

I hope that helps you out a little bit though.
Ill Shoot a question of my own in here

The piece of code that bishop_pass posted:

char *c = "hello";
printf (c);

Should work right? And I know it would''ve worked on my compiler a few weeks back but now anything like that wont it just crashes the program. When ive asked people about this they say its bad practice to do someting like that because you end up writing the string to a random place in memory, they said it''ll work sometimes other times no.

So should that code work on my compiler each and every time? and is it bad practice assigning string constants to Pointers?

Oh yeah an what would a good programmer use to hold strings

Thanks in Advance....

Sorry for jumpin in on yer post


Hello,


Well, you could use CString (MFC) or STL string. But if wanted to keep the C route you can use.

char szBuffer[255];
strcpy(szBuffer, "Happy String ");

or

char *szBuffer = new char[255];
// Create a pointer to a 255 char array on the heap
strcpy(szBuffer, "Happy String "));

you can print them as:
printf("%s", szBuffer);

oh and for length there is:
int iHappyStringLength = strlen (szBuffer);

Best Regards,

- James






Advertisement
quote: Original post by CoiN

Ill Shoot a question of my own in here

The piece of code that bishop_pass posted:

char *c = "hello";
printf (c);

Should work right?


I use that all the time.. But the C and C++ Language reference always seem to show it being done this way:

char c[] = "hello";

// CHRIS

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
Well Now,

I was taught that a string was a collection
of characters. And a char is a single character.

Now I only know Pascal so this how I would set
up a 30 character max STRING.

VAR
name:STRING[30];

Thats me done.

STVOY

Mega Moh Mine!!
Ok, I''ve gotten the length() function to work properly...so that''s taken care of. However, there are just two more "items" that I need to take care of for this create_new_player function.

1.) I want to make it so that if the user doesn''t enter a name and just presses Enter, instead of line feeding to the next row it will give a message saying, "Must enter a name." Would I have to know the ASCII code for the Enter key and test to see if anything has been written if it is pressed? That sort of thing?

2.) Allow the user to have spaces in the player name. Right now I''m just using plain old cin to get the player name, which as you know, doesn''t get spaces. I tried cin.getline but apparently that doesn''t like strings too much because it won''t let me use it.

Thanks so far for all your help. Man, thank God for Al Gore, the man who invented the Internet! (Or so he says)

Joker

This topic is closed to new replies.

Advertisement