Skip to main content
GameDev.net gamedev.net
🔒 Locked

About appending strings in C++

Started by Leiarchy9 Dec 28, 2003 at 8:47 PM 8 replies 600+ views
Original Post
Leiarchy9
Leiarchy9
#include <cstdio>
#include <cstdlib>


int main( void )
{

     char Buffer[100];
     char* Name= new char[100];
     int number;
     
     
     printf( "Please Input A Number: " );
     scanf( "%s", Name );
     
     
     sprintf( Buffer, "%s", Name );
     printf( "Hi %s!!", Buffer );
     
     
     rewind( stdin );
     rewind( stdout );
     
     delete [] Name;
     
     return(EXIT_SUCCESS);
}
 
The sprintf() function.....is there a C++ equivalent to it? That also handles std::string? -------- "Do you believe in Ghost?"
--------"Do you believe in Ghost?"
SiCrane
SiCrane
The classic way to append std::string objects in C++ is to use opeartor+(). e.g.:

int main() {
std::string a("Moo");
std::string b("cow");

std::string c = a + b;

std::cout << c.c_str() << std::endl;
return 0;
}

std::string also supplies an append() member function.

For sprintf() like functionality, the common method is to use std::stringstream.
Thunder_Hawk
Thunder_Hawk
quote:
Original post by SiCrane

std::cout << c.c_str() << std::endl;




 std::cout << c << std::endl;  

will suffice...cout can handle std::string just like a C-style string.

______________________________________________________________
The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ
MySite
______________________________________________________________
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Forma
The Flood
The Flood
You can also use std::string::append()

To append a std::string or char* :

std::string mystring1, mystring2;
char* sz;

mystring1.append(mystring2);
mystring1.append(sz);


Hope this helps!!



[edited by - The Flood on December 28, 2003 10:21:42 PM]
SiCrane
SiCrane
quote:
Original post by Thunder_Hawk
 std::cout << c << std::endl;   

will suffice...cout can handle std::string just like a C-style string.


Force of habit; there was a bug in one of the early STL implementations that made operator<<(std::ostream &, std::string &) leak memory.
daerid
daerid
The C++ equivalent to scanf() would be either operator >> or std::getline:


#include <iostream>

using namespace std;

int main(int argc,char* argv[])
{
string name;
cout << "Enter Name: " << flush;
// You could use:

// - cin >> name;

// but that would end the name at the

// first whitespace character

getline(cin,name);

cout << "You entered \"" << name << "\" as your name." << endl;
}



daerid | Legends | Garage Games | Spirit | Hapy | Boost | Python | Google
"Doomed to crumble, unless we grow, and strengthen our communication" - Maynard James Keenan, Tool
daerid@gmail.com
Leiarchy9
Leiarchy9
daerid? What is your point?
--------"Do you believe in Ghost?"
Leiarchy9
Leiarchy9
bump

--------
"Do you believe in Ghost?"
--------"Do you believe in Ghost?"
invective
invective
quote:
Original post by Leiarchy9
The sprintf() function.....is there a C++ equivalent to it? That also handles std::string?



The C++ equivalent to sprintf, which incendentally is a potentially horribly evil function because it does not check the size of your buffer and can easily overflow it, is std::ostringstream. std::ostringstream is similar to std::cout, as the both inherit from std::ostream. It is far less evil than sprintf, because it manages its own memory.

see here:
http://www.cplusplus.com/ref/

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.