C++ strings...

cyberphant0m

Member
Oct 21, 2003
99
0
0
Im trying to create a class which has a string data member, and in the constructor, the user is asked to input their name and the name is stored in that data member. I tried substituting "string" with char*. That one compiles, but crashes after the user presses enters their name, and presses ENTER... Here is the code I have so far... (Compiled with Visual C++ 6.0)


#include <iostream>
using namespace std;
class Player
{
public:
Player ( ) ;
string GetPlayerName ( ) ;

private:
string name;
};
Player::player ( )
{
cout << "What is your name? " ;
cin.getline(name, 20) ; // <--- line 20
}
string Player::GetPlayerName ( )
{
return name;
}
int main ( )
{
Player theplayer;
cout << theplayer.GetPlayerName(); // <--- line 32
return 0;
}


Here are the error messages I get (the lines where the errors occur, are noted above with comments):

C:\programs\class2\class2.cpp(20) : error C2664: 'class std::basic_istream<char,struct std::char_traits<char> > &__thiscall std::basic_istream<char,struct std::char_traits<char> >::getline(char *,int)' : cannot convert parameter 1 from 'class std::b
asic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'char *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
C:\programs\class2\class2.cpp(32) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no acceptable conversi
on)

I can't see for the life of me why I cant enter keyboard input into a "string" variable, or why I can't pass a string variable to "cout"...
 

Kntx

Platinum Member
Dec 11, 2000
2,270
0
71
replace...

this ---> cin.getline(name, 20) ; // <--- line 20

with something like....

this ---> cin >> name;

and you should be good to go.
 

cyberphant0m

Member
Oct 21, 2003
99
0
0
now i get a new error (the first one is solved, but replaced with this one):

C:\programs\class2\class2.cpp(20) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class std::basic_string<CHAR,STRUCT std::char_traits<char>,class std::allocator<CHAR> >' (or there is no acceptable conversi
on)

Im starting to think my compiler may be "broken" lol
 

Kntx

Platinum Member
Dec 11, 2000
2,270
0
71
#include <iostream>

using namespace std;

class Player
{
public:
Player ( ) ;
string GetPlayerName ( ) ;

private:
string name;
};

Player::player ( )
{
cout << "What is your name? " ;
cin >> name;
}
string Player::GetPlayerName ( )
{
return name;
}

int main ( )
{
Player theplayer;
cout << theplayer.GetPlayerName(); // <--- line 32
return 0;
}

this here compiles under gcc.

you aren't by chance using visual c++ 6.0 are you?
 

Kntx

Platinum Member
Dec 11, 2000
2,270
0
71
if you want it to just work, everywhere in your program you see "string" replace with "char*" and it'll work like a charm.

 

BFG10K

Lifer
Aug 14, 2000
22,709
3,003
126
if you want it to just work, everywhere in your program you see "string" replace with "char*" and it'll work like a charm.
No it won't. Not only will it crash the progran but he'll also lose all of the benefits that std::string provides, of which there are many.
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
the header <string> contains a function for extracting line to std::string

eg:
string blah;
getline(cin, blah);

cin >> blah; would also work but that would stop at white spaces
 

Kntx

Platinum Member
Dec 11, 2000
2,270
0
71
Originally posted by: BFG10K
if you want it to just work, everywhere in your program you see "string" replace with "char*" and it'll work like a charm.
No it won't. Not only will it crash the progran but he'll also lose all of the benefits that std::string provides, of which there are many.

won't crash. try it.
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
It's not nearly as simple as "it will crash" or "it won't crash."

std::string and char* do not serve the exact same purpose, so recommending to simply "replace" one with the other is simplistic and probably not going to work out well in many cases. It may or may not crash, but either way, it's not good advice.
 

BFG10K

Lifer
Aug 14, 2000
22,709
3,003
126
won't crash. try it.
I don't need to because I know it will crash. Not only that but he tried it:

That one compiles, but crashes after the user presses enters their name, and presses ENTER.
If you enter anything other than a NULL byte into the name you will crash the program. Perhaps not immediately but you've still corrupted your application's heap memory so that your program will eventually develop a fault.
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
Originally posted by: Kntx
Originally posted by: BFG10K
if you want it to just work, everywhere in your program you see "string" replace with "char*" and it'll work like a charm.
No it won't. Not only will it crash the progran but he'll also lose all of the benefits that std::string provides, of which there are many.

won't crash. try it.

writing to a dangling pointer? it might work, but more likely it'll crash or caus problems later

try allocating some memory for it