Register | Login
Views: 19364387
Main | Memberlist | Active users | ACS | Commons | Calendar | Online users
Ranks | FAQ | Color Chart | Photo album | IRC Chat
11-02-05 12:59 PM
0 user currently in Programming. | 3 guests
Acmlm's Board - I2 Archive - Programming - Strings in C++ | |
Add to favorites | "RSS" Feed | Next newer thread | Next older thread
User Post
Sandy53215
Acmlm (10:55:31 PM): they're having fun for the first time in so long
Level: 47

Posts: 790/948
EXP: 713034
For next: 53169

Since: 03-15-04
From: Milwaukee, Wisconsin (U.S.A)

Since last post: 1 day
Last activity: 4 hours
Posted on 07-23-05 01:58 PM Link | Quote
When using strings in C++ (not Char arrays, but the string class from #include)
how is it possible to input data into them (or have the user input data into them).

a normal cin works unless their are spaces but im sure their is a much more appropriate way of doing it.

gets and cin.getline both fail with horrid error messages.
Ramsus

Octoballoon
Level: 19

Posts: 151/162
EXP: 34651
For next: 1126

Since: 01-24-05
From: United States

Since last post: 39 days
Last activity: 71 days
Posted on 07-23-05 05:55 PM Link | Quote
The string header includes a getline template function that takes an istream reference and a string class reference. So:

using namespace std;

/* ... */
string mystring;
getline(cin, mystring);
cout << mystring << endl;

Takes one line from input, puts it in mystring, then outputs it.

HyperLamer
<||bass> and this was the soloution i thought of that was guarinteed to piss off the greatest amount of people

Sesshomaru
Tamaranian

Level: 118

Posts: 5989/8210
EXP: 18171887
For next: 211027

Since: 03-15-04
From: Canada, w00t!
LOL FAD

Since last post: 2 hours
Last activity: 2 hours
Posted on 07-24-05 10:10 AM Link | Quote
Have fun.

If you want to just put something into it: sprintf(string,"This is the number %d",42);. If you want to get input from a console: gets(string);, IIRC.
neotransotaku

Baby Mario
戻れたら、
誰も気が付く
Level: 87

Posts: 3644/4016
EXP: 6220548
For next: 172226

Since: 03-15-04
From: Outside of Time/Space

Since last post: 11 hours
Last activity: 1 hour
Posted on 07-25-05 11:22 PM Link | Quote
since string is a class... new String("string literal") wouldn't hurt either...
Dish

Spiny
Level: 38

Posts: 456/596
EXP: 355646
For next: 14801

Since: 03-15-04
From: Disch

Since last post: 18 days
Last activity: 18 days
Posted on 07-25-05 11:26 PM Link | Quote
don't confuse this with Java. 'new' without a matching 'delete' will lead to memory leaks.


string myString("string literal");


^ is the proper way to use a class constructor in C++. Alternatively if you want to use heap memory:



string* myString = new string("string literal");

// do stuff with myString here

delete myString; // don't forget this line if using 'new' !!!!
beneficii

Lakitu
Level: 36

Posts: 329/567
EXP: 299656
For next: 8454

Since: 06-27-04
From: Cordova, TN, USA

Since last post: 14 hours
Last activity: 6 hours
Posted on 07-25-05 11:30 PM Link | Quote
Originally posted by Disch
don't confuse this with Java. 'new' without a matching 'delete' will lead to memory leaks.


string myString("string literal");


^ is the proper way to use a class constructor in C++. Alternatively if you want to use heap memory:



string* myString = new string("string literal");

// do stuff with myString here

delete myString; // don't forget this line if using 'new' !!!!



Meh, I don't even know why people bother using the string class; quit being lazy and just use a char array/pointer! It's not that hard! If you want it to be of variable size, then just use malloc.


(edited by beneficii on 07-25-05 02:30 PM)
Dish

Spiny
Level: 38

Posts: 457/596
EXP: 355646
For next: 14801

Since: 03-15-04
From: Disch

Since last post: 18 days
Last activity: 18 days
Posted on 07-25-05 11:32 PM Link | Quote
I prefer to use char arrays as well. Then again I dislike a lot of the C++ additions (I only really use C++ for classes and a handful of other perks).

But meh, to each his own.
beneficii

Lakitu
Level: 36

Posts: 330/567
EXP: 299656
For next: 8454

Since: 06-27-04
From: Cordova, TN, USA

Since last post: 14 hours
Last activity: 6 hours
Posted on 07-25-05 11:34 PM Link | Quote
Classes are pretty useful, but they sometimes seem restrictive (like it seems you can't pass a pointer of a class function--say if you wanted to use a class function as a DlgProc), so I on some projects I may choose not to use them at all.

Plus, for me that may be "to each her own."
Dish

Spiny
Level: 38

Posts: 458/596
EXP: 355646
For next: 14801

Since: 03-15-04
From: Disch

Since last post: 18 days
Last activity: 18 days
Posted on 07-25-05 11:42 PM Link | Quote
You can pass a static member function as a DlgProc. You can't use a non-static member function because they require an object to be called with. That's not a limitation of the class... but more because DlgProcs are expected to be pointers to global functions. If they were designed better, they'd allow you to give a pointer with the DlgProc so that it would be more OOP friendly... but alas it's too late for that.

Anyway, the reason I like classes is so I can avoid having global vars, and I don't have to do 'mystruct->varname' when referencing every variable in an object. Putting my entire program in a big class allows the whole program to have access to all the member vars no matter which .cpp file they're in ... without having to make the vars global (and do the whole 'extern' linking BS).

That's on top of the OOP benefits. I still use various classes I have sitting on my HD for various projects (like a sound output class, a PNG loader, OGG player, DInput wrapper, etc)
ADnova
Newcomer
Level: 5

Posts: 8/9
EXP: 364
For next: 165

Since: 05-04-05

Since last post: 97 days
Last activity: 40 days
Posted on 07-27-05 11:10 AM Link | Quote
One of my profs wrote up a page comparing C and C++ strings:
http://cs.stmarys.ca/~porter/csc/ref/c_cpp_strings.html
Dish

Spiny
Level: 38

Posts: 483/596
EXP: 355646
For next: 14801

Since: 03-15-04
From: Disch

Since last post: 18 days
Last activity: 18 days
Posted on 07-27-05 09:45 PM Link | Quote
I fail to see the difference between 'assigning' and 'copying'. I dislike how he says you can't assign a string to a char array, but says you can assign a string to a std::string object with the '=' operator. All the '=' operator is is like an overloaded version of strcpy -- why not list strcpy on the c-style string side?

Some other things were misleading... such as the concatenation example:

"strcpy(s, strcat(str1, str2)); str = str1 + str2;"

The char array examlpe will actually append str2 to str1, then copy str1 to s (modifying both str1 and s). Whereas the std::string side will only modify str.

Other than that it is pretty informative. One of the little benefits to working with char arrays is the power of having every character at your fingertips without having to work around class member functions. For instance I do the following a LOT:


char fullpath[MAX_PATH] = "C:\\Directory\\";
char* filename;

filename = fullpath + strlen(fullpath);

strcpy(filename,"file.txt");

printf( fullpath ); // will output "C:\Directory\file.txt"
printf( filename ); // will output "file.txt"


Using that I can maintain a buffer which always holds the full path to a file, but only modify 'filename' when I want to switch to another file in the same directory.


Stuff like that is definatly not as easy to do with std::string. However you can't really put that kind of thing in a simple comparison doc.
ADnova
Newcomer
Level: 5

Posts: 9/9
EXP: 364
For next: 165

Since: 05-04-05

Since last post: 97 days
Last activity: 40 days
Posted on 07-28-05 01:10 AM Link | Quote
Originally posted by Disch
I fail to see the difference between 'assigning' and 'copying'. I dislike how he says you can't assign a string to a char array, but says you can assign a string to a std::string object with the '=' operator. All the '=' operator is is like an overloaded version of strcpy -- why not list strcpy on the c-style string side?



Well, from a teaching point of view it makes sense. Telling students with only 1 C++ course under their belts that you can assign to C-strings is not a good idea. To most of them, assignment means use of the '=' operator, not calling strcpy. I think it's important while learning the details of a language to be as precise as possible.


Originally posted by Disch

Some other things were misleading... such as the concatenation example:

"strcpy(s, strcat(str1, str2)); str = str1 + str2;"

The char array examlpe will actually append str2 to str1, then copy str1 to s (modifying both str1 and s). Whereas the std::string side will only modify str.



Good point there, I might send him an email about that.
Dwedit

Shyguy
Level: 17

Posts: 77/92
EXP: 20794
For next: 3949

Since: 04-26-04

Since last post: 5 days
Last activity: 1 day
Posted on 08-11-05 04:10 AM Link | Quote
Um... the string content itself uses heap memory. Making a 'new' string will just stick the pointer and length on the heap, which is pointless unless you really really want objects on the heap.
I never use 'new []' anyway, the vector class takes care of that for me.
Add to favorites | "RSS" Feed | Next newer thread | Next older thread
Acmlm's Board - I2 Archive - Programming - Strings in C++ | |


ABII


AcmlmBoard vl.ol (11-01-05)
© 2000-2005 Acmlm, Emuz, et al



Page rendered in 0.033 seconds.