C++ Strings

NuclearFusi0n

Diamond Member
Jul 2, 2001
7,028
0
0
#include <string>

void main()
{
string ohnoesBAND;
}

Visual C++ 6 tells me that "string" is an undeclared identifier. What's wrong here?
 

scorp00

Senior member
Mar 21, 2001
994
0
71
i think it's: #include <string.h>

been a while though

you should read the book instead of asking for help on anandtech :)
 

NuclearFusi0n

Diamond Member
Jul 2, 2001
7,028
0
0
Originally posted by: Ameesh
why dont you just use char *'s ? there are a lot of functions that you can use.

i'm doing that already, i'm just curious why strings dont work...
 

NuclearFusi0n

Diamond Member
Jul 2, 2001
7,028
0
0
Originally posted by: scorp00
i think it's: #include <string.h>

been a while though

you should read the book instead of asking for help on anandtech :)

a. string.h gives the same error
b. there is no book ;)
 

NuclearFusi0n

Diamond Member
Jul 2, 2001
7,028
0
0
Ameesh, while you are here, how do i append one character array to another? stupid question? yes. do i know the answer? no :p

edit: nevermind, crackhead unix man page helped me out
 

Ameesh

Lifer
Apr 3, 2001
23,686
1
0
Originally posted by: NuclearFusi0n
Ameesh, while you are here, how do i append one character array to another? stupid question? yes. do i know the answer? no :p

char *strcat(char *restrict, const char *restrict);


it adds one string to another 2nd parameter is a const so you may not change that but the first is the tring that you want to add upon

understand?
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
Ok, I don't use crackhead crap windows compilers :)
But you may be having a namespace problem.

Try:
std::string ohnoesBAND;

or add:

using namespace std;

above the function declaration.

And why would you ever use char * if you have the string class!
 

Ameesh

Lifer
Apr 3, 2001
23,686
1
0
Originally posted by: ergeorge


And why would you ever use char * if you have the string class!


because if he needs to use some of those c style functions somewhere he should just stick with char *'s everywhere.


it doesnt really matter to me which one he uses he should just be consistent all the time because if he is not it becomes a real hassle to change strings back and forth to different formats.
 

Dudd

Platinum Member
Aug 3, 2001
2,865
0
0
Originally posted by: NuclearFusi0n
Originally posted by: scorp00
i think it's: #include <string.h>

been a while though

you should read the book instead of asking for help on anandtech :)

a. string.h gives the same error
b. there is no book ;)

Why not just try and download string.h and string.cpp? Maybe it wasn't included with your compiler? I don't really know, I'm taking AP Comp Sci, and we use a file called apstring.cpp for our strings.

 

Armitage

Banned
Feb 23, 2001
8,086
0
0
Originally posted by: Ameesh
Originally posted by: ergeorge


And why would you ever use char * if you have the string class!


because if he needs to use some of those c style functions somewhere he should just stick with char *'s everywhere.


it doesnt really matter to me which one he uses he should just be consistent all the time because if he is not it becomes a real hassle to change strings back and forth to different formats.

I don't think I've found anything I haven't been able to do with string class, but for legacy code that expects char *, it's easy enough to pass it .c_str()
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0
Originally posted by: Ameesh
Originally posted by: NeoSolo
I think Ameesh has the right answer. =)

ive had the right answer this whole thread.

Actually, PrincessGuard had it first, then ergeorge :D

[edit]I'm being pedantic because suggesting using a pointer to char isn't an answer to his asking how to use a string in C++[/edit]
 

manly

Lifer
Jan 25, 2000
13,565
4,230
136
Originally posted by: Descartes
Originally posted by: Ameesh
Originally posted by: NeoSolo
I think Ameesh has the right answer. =)

ive had the right answer this whole thread.

Actually, PrincessGuard had it first, then ergeorge :D

[edit]I'm being pedantic because suggesting using a pointer to char isn't an answer to his asking how to use a string in C++[/edit]
No are you aren't being pedantic because Ameesh's so-called right solution originally was recommending C char arrays in C++ code. Only later did he qualify that advice by saying it gets confusing to mix & match C and C++ strings in the same code. It only took him a few hours to repeat what a few others had already said to merely get the simple C++ code to compile.