• Advertisement

Archived

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

holding a string without limits

This topic is 5036 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
quote:
Original post by TheOne1
this with the fix or without, because i installed all the visual studio 6 service packs...


go back to the link that oluesyi posted. note that fixing it does not mean just downloading the latest service packs, it involves manually replacing one or more of the library header files.

-me

Share this post


Link to post
Share on other sites
ok, so I manually fixed the code....but nothings changed.

so far out of all the things ive tried ive gotten these errors:

1. the code skips cin, so no input can be entered.
2. when i enter the function, the program terminates
3. when i enter a space, only the first string will show up, but nothing after the space.

am i writing the wrong code, or is it some sort of bug?

this is one of the errors i get alot with the getline function:
C:\Program Files\Microsoft Visual Studio\MyProjects\tester\test.cpp(14) : error C2664: 'class std::basic_istream > &__thiscall std::basic_istream >::getline(char *,int)' : cannot


[edited by - TheOne1 on May 9, 2004 7:52:40 PM]

Share this post


Link to post
Share on other sites
ok, forget finding out how to hold a string, i can figure it out myself.

but, is there any possible way to get rid of the double keypress for cin.getline?

Share this post


Link to post
Share on other sites
quote:
Original post by TheOne1
but, is there any possible way to get rid of the double keypress for cin.getline?
You failed to follow the instructions given on that page. One of the fixes listed (which involves altering the header files supplied with your compiler) corrects that defect.

Share this post


Link to post
Share on other sites

  • Advertisement