string testABCD = "abcd";
char a = 'a';
char b = 'b';
char c = 'c';
char d = 'd';
string combined = a+b+c+d;
if (testABCD == combined)
cout << "Works!" << endl;
else
cout << "Doesn't Work" << endl;
char to string in C++
Ok, so say I have bunch of characters and I want to combine them and make it of type string. How can I do that?
How can I make something like this work?
thanks!
// There isn't a single-char constructor for string,// So, cast 'a' to a raw string, and only copy one char from it:string combined = string( static_cast<char*>(&a), 1 ) + b + c + d;// Drawbacks:// You could ignore the static_cast:string combined = string( &a, 1 ) + b + c + d;// But you could accidentally make a string of length 91 (value for 'a') filled with 0x01:// string combined = string( a, 1 ) + b + c + d;
Or:
#include <sstream>// Use a stringstream!stringstream ss;ss << a << b << c << d;// Works with other types as well!// ss << 123 << ' ' << FooBar( "hello" );string combined = ss.str();// Or cut the middleman and use ss.str() directly
How about std::string(1, a) + b + c + d? Or maybe something like this:
char a = 'a';char b = 'b';char c = 'c';char d = 'd';char temp[] = {a, b, c, d, 0};std::string combined(temp);std::cout << combined << std::endl;
thanks fastcall, the second one works. But can you tell me how to empty the ss? Because I have it in a loop and I want string to have different value everytime.
Thanks a lot!
ps. thanks DevFred, but we haven't learned anything like that yet.
string testABCD = "abcd"; stringstream ss; //for loop here to get different chars from input { //user's input for a, b, c, d ss << a << b << c << d; string combined = ss.str(); if (testABCD == combined) { cout << "Works!" << endl; break; } else cout << "Doesn't Work" << endl; }
Thanks a lot!
ps. thanks DevFred, but we haven't learned anything like that yet.
Quote:Original post by musafir2007Usually we treat stringstream as a cheap, discardable, temporary object. Rather than reset it we just make a new one:
But can you tell me how to empty the ss? Because I have it in a loop and I want string to have different value everytime.
for (/*stuff*/){ // create a fresh stringstream on each iteration std::stringstream ss; // use the ss here}
Quote:Original post by musafir2007
thanks DevFred, but we haven't learned anything like that yet.
You mean arrays? OK. But have you looked at the first line of my post?
std::string(1, a) + b + c + d
Or is the "std::" part confusing you? Do you have using namespace std; or something like that at the top of your program?
By the way there is no need to read into four character variables and then combine them into a string. Just read into a string directly.
#include <iostream>#include <string>int main(){ std::cout << "please enter four letters: " << std::endl; std::string input = "...."; std::cin >> input[0] >> input[1] >> input[2] >> input[3]; std::string test = "abcd"; if (input == test) { std::cout << "Works!" << std::endl; } else { std::cout << "Doesn't work." << std::endl; }}
what dmatter said is true for the most part, but if you want to know:
ss.str("");
will clear the stream.
ss.str("");
will clear the stream.
Quote:what dmatter said is true for the most part, but if you want to know:That doesn't clear the flags though (which is one of the reasons it's usually easier just to create a new stream object).
ss.str("");
will clear the stream.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement