using strings with fputs and strcpy

I am trying to learn c++ by making a text game.
At the moment I have a problem.

I am using a string-array to hold my races.

1
2
3
4
5
6
std::string race[5];
race[0] = "God";
race[1] = "Human";
race[2] = "Orc";
race[3] = "Elf";
race[4] = "Dwarf";


And this neither fputs nor strcpy like so I tried this

1
2
3
4
char races[50];
int rX;
cin >> rX;
races = race[rX]


But this didn't work either. Got this error:
incompatible types in assignment of `std::string' to `char[50]'


The thing I wanna do is a array that holds the different races and after that
put it in a different variable that will save the value in a text file.
And to use fputs with a string didn't work.

cannot convert `std::string' to `const char*' for argument `1' to `int fputs(const char*, FILE*)'


Then to use strcpy didn't work either.
no matching function for call to `strcpy(char[50], std::string&)'
candidates are: char* strcpy(char*, const char*)


Anyone know how I can do this?
If you didn't understand what I try to do it's this:
1. Start the program
2. Enter name, age, race
3. Save name, age and race in their own variables.
4. Use fputs to save the variables to a file

Name and age works perfect because they already are char but race is string
so it doesn't wanna work with fputs or strcpy and I have no idea how to go
around this.
You don't need fputs() or strcpy(). The std::string and std::ostream classes handle all that stuff for you.
1
2
3
4
5
6
string s = "Hello world!";

fputs( s.c_str(), stdout );  // output a c-string version of 's'
cout << s << '\n';           // output 's' the C++ way

string t = s;  // comparable to strcpy( t, s ), if s and t were char* 


Hope this helps.
Thanks, this worked fine!
fputs(race[rX].c_str(), sFile);

Do you know any good book?
I have C++ Programming by Stephen Prata and it didn't cover this.
It doesn't cover fputs at all.

I prefer a book in my lap rather then a web page or a PDF document.
Agreed. I've gotten pretty used to online documents though...

The reason your book doesn't mention fputs(), etc is because they are standard C functions, not C++.

For a book: http://www.cplusplus.com/forum/windows/2733/

Hope this helps.
I have reported your spamming this topic.

There is no "advancing" to C++ from C. C is a different language to C++ with different purpose. It's still VERY widely used and will continue to be so. Unless you consider Linus Torvalds someone who needs to "advance to C++"?

Topic archived. No new replies allowed.