• Advertisement

Archived

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

becomming angry with chars, strings, text...

This topic is 5315 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

hello i''m wondering how to store a large amount of text. i''ve tried from chars to strings to whatever and it never works. especially with a class system. #include <iostream.h> #include <conio.h> #include <string.h> class player { public: char name[60]; private: }; void main() { player player2; player2.name = "blah, a really long name that can go on for 60 letters"; cout<

Share this post


Link to post
Share on other sites
Advertisement
You'll have to change the player.name="string" to:

strcpy(player2.name, "blah, a really long name that can go on for 60 letters");

Oh yeah, and here's a good page for C Strings (String.h)
http://www.cplusplus.com/ref/cstring/

[edited by - Joe-Bob on July 3, 2003 7:30:33 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Tiffany Smith
Hi there, at a quick glance i can see that you missed a ''<'' in your ''cout''.


Not quite...Ahem...HTML

Share this post


Link to post
Share on other sites
quote:
Original post by Thunder_Hawk
quote:
Original post by Tiffany Smith
Hi there, at a quick glance i can see that you missed a ''<'' in your ''cout''.


Not quite...Ahem...HTML



Ohh interesting i didnt realize.

Share this post


Link to post
Share on other sites
thanks, but it doesn''t display the name in dos when i compile with the cout.

Share this post


Link to post
Share on other sites
Use proper C++ strings, not character arrays. By the way, it''s <iostream>, not <iostream.h>; search GameDev on the topic and you''ll find lots of reasons why ...

#include <iostream>
#include <conio.h>
// Different file. <string.h> becomes

// <cstring>!

#include <string>

class player
{
public:
std::string name;
private:
};

// main() -must- return an int!

int main()
{
using namespace std;
player player2;
player2.name = "blah, a really long name that can go on for 60 letters - or as many as you want; the std::string will reisze itself";
cout << player2.playername << endl;
getch();
// Don''t need to -explicitly- return; main() will

// implicitly return 0 unless you specify otherwise

}

Share this post


Link to post
Share on other sites
quote:
Original post by Miserable
By the way, it''s <iostream>, not <iostream.h>; search GameDev on the topic and you''ll find lots of reasons why ...
I''ll save everyone the trouble of searching: here''s a good topic on this issue. I have it bookmarked for this purpose.

Share this post


Link to post
Share on other sites
quote:
Original post by Tiffany Smith
Miserable, did you even try to compile that before you posted it?
The only error is player2.playername, which isn''t much of a problem.

Share this post


Link to post
Share on other sites
quote:
Original post by Tiffany Smith
Ohh my gosh its a horrible de ja vous. ....
Miserable, did you even try to compile that before you posted it?

No. Honestly, I didn''t really look at the code; I just quoted the OP''s code and modified the sections dealing with strings ...

Share this post


Link to post
Share on other sites

  • Advertisement