Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

TheOne1

holding a string without limits

This topic is 5328 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am creating a little text rpg, and I need help holding a string (character''s name). I have a global variable: char * characterName and here is the function to set the name: // set characters name void set_CharacterName() { cout << "Enter a name for your new character: "; cin >> characterName; cout << "\n\nYour character''s name is " << characterName << "\n\n"; } i know that cin >> characterName will only hold one character, so I tried cin.get, and cin.getline, but those have character limits. I am trying to avoid character limits, so that I don''t have to set a constant number of characters a characters name can be and also so that there is no wasted space. So how do I hold a string, including spaces? Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
getline(cin,variablename,''x'');

where x is a deliminating character. ie, the character the user presses to end the input. i like to use a period but you could use a ''/n'' so the user presses enter to stop.

Share this post


Link to post
Share on other sites
quote:
Original post by graveyard filla
getline(cin,variablename,'x');

where x is a deliminating character. ie, the character the user presses to end the input. i like to use a period but you could use a '/n' so the user presses enter to stop.


do you mean cin.getline( variablename, 'x' );?

i tried that and the example you gave me but doesnt work.

i also tried string, but it wont take in anything after a space.



[edited by - TheOne1 on May 9, 2004 1:27:32 AM]

Share this post


Link to post
Share on other sites
Some variant of that function will work to take space''d strings.

getline(string&)

Share this post


Link to post
Share on other sites
quote:
Original post by Vlion
Some variant of that function will work to take space''d strings.

getline(string&)


sorry...i dont understand what your saying. can you explain more or show an example? thanks!

Share this post


Link to post
Share on other sites
quote:
Original post by graveyard filla
''/n''
Newline is \n. Note the back - rather than forward - slash.

@TheOne1:
#include <iostream>
#include <string>

int main()
{
using namespace std;
string characterName;

cout << "Enter a name for your new character: ";
getline(cin, characterName); // or cin.getline(characterName);

cout << "\n\nYour character''s name is " << characterName << "\n\n";

return 0;
}
The implementation of the standard library that ships with MSVC 6.0 is broken. One of the flaws is that it makes std::getine require an additional keypress. Get the fixes here.

Share this post


Link to post
Share on other sites
i did this:

string clname;

cout << "enter a name: ";
getline( cin, clname );
cout << "name: " << clname << endl;

it works, but like you said it requires an additional keypress. is this with the fix or without, because i installed all the visual studio 6 service packs...


[edited by - TheOne1 on May 9, 2004 6:50:23 PM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!