C++ converting an int to a string - string

I know this is extremely basic but I'm very new to C++ and can't seem to find an answer. I'm simply trying to convert a few integers to strings. This method works:
int a = 10;
stringstream ss;
ss << a;
string str = ss.str();
but when I need to convert the second and third ones like this:
int b = 13;
stringstream ss;
ss << a;
string str2 = ss.str();
int c = 15;
stringstream ss;
ss << b;
string str3 = ss.str();
I get this error:
'std::stringstream ss' previously declared here
Do I need to somehow close stringstream? I've noticed that if I put them far away from each other in my code the compiler doesn't mind but that doesn't seem like something I should do. Does anyone have suggestions?

You're trying to redeclare the same stringstream with the same name. You can modify your code like this in order to work :
int b = 13;
stringstream ss2;
ss2 << a;
string str2 = ss2.str();
Or like this if you don't want to redeclare it :
int b = 13;
ss.str(""); // empty the stringstream
ss.clear();
ss << a;
string str2 = ss.str();
You can also use , which is much quicker :
int c = 42;
std::string s = std::to_string(c);

The stringstream is a variable just like any other. To make the idiom work twice without change, put it in braces:
int a = 13;
string str2;
{
stringstream ss;
ss << a;
str2 = ss.str();
} // ss ceases to exist at the closing brace
int b = 15;
string str3;
{
stringstream ss; // now we can make a new ss
ss << b;
str3 = ss.str();
}
Since C++11, you can do
std::string str4 = std::to_string( b );
or (with c of arbitrary type)
std::string str5 = ( std::stringstream() << c ).str();
There are other solutions, of course.

The particular error you are getting is telling you that you can't declare two variables with the same name (ss in your case) within the same scope.
If you wanted to create a new stringstream you could call it something else.
But there are other ways to convert an int to a string.. you could use std::to_string().

try
#include <string>
int a = 10;
std::string s = std::to_string(a);

Make sure you declare:
using namespace std;
Then insert the code:
int a = 10;
ostringstream ssa;
ssa << a;
string str = ssa.str();
int b = 13;
ostringstream ssb;
ssb << b;
string str2 = ssb.str();
int c = 15;
ostringstream ssc;
ssc << c;
string str3 = ssc.str();

One of the simplest way i can think of doing it is:
string str = string(itoa(num));

Related

how I delete char in string?

There is a problem using the erase function.
when I input aaabbbbcc, only aaa is output.
But I want to print aaacc.
What is the problem with this code?
int main() {
int i = 3;
string a, temp_a;
cin >> a;
a.erase(i, i + 3);
printf("%s", a.c_str());
}

assign an int(eg. 10) to a string member in a struct in c++, why it compile success?

I'm practicing <thinking in c++ > for chapter5, ex01:
Write a struct called Lib that contains three string objects a, b, and c.
In main( ) create a Lib object called x and assign to x.a, x.b, and x.c.
Print out the values.
in the beginning, I'm trying:
// ex02.cpp
#include <iostream>
#include <string>
using namespace std;
struct Lib {
string a;
string b;
string c;
};
int main(){
Lib x;
x.a = 1; // here I forgot the string object, and incorrectly assign the wrong value to x.a
x.b = 2;
x.c = 3;
cout << x.a << " " << x.b << " " << x.c << endl;
return 0;
}
and it can compile successfully, but the run result seems only two blank spaces:
[root#VM-0-2-centos ch05]# g++ ex02.cpp
[root#VM-0-2-centos ch05]# ./a.out
[root#VM-0-2-centos ch05]#
at this time I find the wrong assignment. but why it should not give a compile error?
when I modify the assignment to the follows:
x.a = "hello";
x.b = "world";
x.c = "welcome";
it compiles success, and give the right run result:
[root#VM-0-2-centos ch05]# g++ ex02.cpp
[root#VM-0-2-centos ch05]# ./a.out
hello world welcome
[root#VM-0-2-centos ch05]#
my question is why x.a = 1 can compile success?
and when I try:
string test = 1;
it will compile error:
error: invalid conversion from ‘int’ to ‘const char*’ [-fpermissive]
U need to verify my statement by yourself. To see string code.
First, When u declare Lib x, the member of x (a, b, c) will call string constructor. So when assign value to the member of x (x.a = 1), it will call "=operation".
However, string test = 1, it will call constructor.
the most difference is caller. the type of parameter of string constructor is "const char*", but "=operation" can get other type of parameter. So, x.a = 1 at compile is passing.
Note, "int" will cast to a certain type by default.
Thx, #Muqali He, you give me the hint and direction.
I try to understand the string class from here: https://www.cplusplus.com/reference/string/string/operator=/
And I understand a bit more.
for c++98, when I use "=", there is three overload member function:
when I try:
string t2;
t2 = "test";
it's OK. and I try:
string t3;
t3 = 256;
it will get a warning:
warning: overflow in implicit constant conversion
at this time, 256 will be recognized as a char. when you output this value, it will transform into its ASCII code:
string t4;
t4 = 100;
cout << "t4= " << t4 << endl;
the ouput is
t4 = d
if I try:
string t5 = 100; // it will compile error
because there's no corresponding constructor

No suitable constructor exists to convert from "char" to "std::string"

I'm new to coding in C and C++, and I have a program with
an issue. When I (try) to run it, it gives me this error:
"No suitable constructor exists to convert from "char" to "std::string".
I'm not sure what it means. My code is an example of a simple
substitution cipher covered in the book "Cracking Codes with Python" by Al Sweigart.
I just want to replicate it in C++. Here's my code:
#include <iostream> // for communicating with user
#include <string>
using namespace std;
string symbols = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // all symbols
string encrypt(string msg, string key, string mode) {
string ca = symbols;
string cb = key;
string translated;
if (mode == "decrypt") {
ca, cb = cb, ca;
}
int index = 0;
for (index = 0; index < msg.length(); index++) {
cout << "index is " << index << endl;
int sindex = ca.find(msg[index]); // it works here
cout << "sindex is " << sindex << endl;
string cl = cb[sindex]; // the problem
translated += cl;
}
return translated;
}
int main() {
string msg = "";
string key = "";
string mode = "";
string ciphertext = ""; // our variables
cout << "Enter message: (no spaces please)\n";
cin >> msg;
cout << "Enter key (or \"none\" for using default):\n";
cin >> key;
if (key == "none") {
key = "QWERTYUIOPASDFGHJKLZXCVBNM";
}
cout << "Enter mode: (\"encrypt\" or \"decrypt\")\n";
cin >> mode;
ciphertext = encrypt(msg, key, mode);
cout << "The ciphertext is\n" << ciphertext;
}
For some reason it works with msg on line 17 but not with cb on line 19, even though
they're both std::string. The actual error is on line 19 with string cl = cb[sindex];.
Not even sure what's wrong. It works on line 17 int sindex = ca.find(/*The thing here*/msg[index]);.
(Maybe my Visual Studio 2019 has gone nuts.) If I replace cb with msg it still gives me the
same error. Maybe line 17 is a lucky line? Who knows? But please help, I'm so
confused!

C++/cli: How to convert String containing Bytes as characters to double

I have problem converting String^ containing 8 bytes as characters (as ascii) to double.
I want to take those 8 characters and convert them binary to double.
What would you recommend to do this conversion in C++/cli?
I was trying to use Marshal::Copy, Double::TryParse, etc.
Maybe I use wrong specifications of parameters, but I really lost my last hopes.
There must be something easy to do this conversion.
Thanks.
Well, the bad news is that the System.String class uses only Unicode encoding internally.
So if you give it bytes it will map them to its internal encoding hiding the original value.
The good news is that you can play with the System.Text.Encoding class to retrieve 8bits values corresponding to the unicode characters.
Here is a sample :
#include <iostream>
using namespace System;
using namespace System::Text;
int main()
{
int n = 123456;
double d = 123.456;
std::cout << n << std::endl;
std::cout << d << std::endl;
char* n_as_bytes = (char*)&n;
char* d_as_bytes = (char*)&d;
String^ n_as_string = gcnew String(n_as_bytes, 0, sizeof(n));
String^ d_as_string = gcnew String(d_as_bytes, 0, sizeof(d));
Encoding^ ascii = Encoding::GetEncoding("iso-8859-1");
array<Byte>^ n_as_array = ascii->GetBytes(n_as_string);
array<Byte>^ d_as_array = ascii->GetBytes(d_as_string);
cli::pin_ptr<unsigned char> pin_ptr_n = &n_as_array[0];
cli::pin_ptr<unsigned char> pin_ptr_d = &d_as_array[0];
unsigned char* ptr_n = pin_ptr_n;
unsigned char* ptr_d = pin_ptr_d;
int n_out = *(int*)ptr_n;
double d_out = *(double*)ptr_d;
std::cout << n_out << std::endl;
std::cout << d_out << std::endl;
return 0;
}
This should give you :
123456
123.456
123456
123.456
Not sure it is completely safe, but trying it in your context should be a good start to ensure it is viable. :)

stick integer to string and char*

How can I add an integer variable to a string and char* variable? for example:
int a = 5;
string St1 = "Book", St2;
char *Ch1 = "Note", Ch2;
St2 = St1 + a --> Book5
Ch2 = Ch1 + a --> Note5
Thanks
The C++ way of doing this is:
std::stringstream temp;
temp << St1 << a;
std::string St2 = temp.str();
You can also do the same thing with Ch1:
std::stringstream temp;
temp << Ch1 << a;
char* Ch2 = new char[temp.str().length() + 1];
strcpy(Ch2, temp.str().c_str());
for char* you need to create another variable that is long enough for both, for instance. You can 'fix' the length of the output string to remove the chance of overrunning the end of the string. If you do that, be careful to make this large enough to hold the whole number, otherwise you might find that book+50 and book+502 both come out as book+50 (truncation).
Here's how to manually calculate the amount of memory required. This is most efficient but error-prone.
int a = 5;
char* ch1 = "Book";
int intVarSize = 11; // assumes 32-bit integer, in decimal, with possible leading -
int newStringLen = strlen(ch1) + intVarSize + 1; // 1 for the null terminator
char* ch2 = malloc(newStringLen);
if (ch2 == 0) { exit 1; }
snprintf(ch2, intVarSize, "%s%i", ch1, a);
ch2 now contains the combined text.
Alternatively, and slightly less tricky and also prettier (but less efficient) you can also do a 'trial run' of printf to get the required length:
int a = 5;
char* ch1 = "Book";
// do a trial run of snprintf with max length set to zero - this returns the number of bytes printed, but does not include the one byte null terminator (so add 1)
int newStringLen = 1 + snprintf(0, 0, "%s%i", ch1, a);
char* ch2 = malloc(newStringLen);
if (ch2 == 0) { exit 1; }
// do the actual printf with real parameters.
snprintf(ch2, newStringLen, "%s%i", ch1, a);
if your platform includes asprintf, then this is a lot easier, since asprintf automatically allocates the correct amount of memory for your new string.
int a = 5;
char* ch1 = "Book";
char* ch2;
asprintf(ch2, "%s%i", ch1, a);
ch2 now contains the combined text.
c++ is much less fiddly, but I'll leave that to others to describe.
You need to create another string large enough to hold the original string followed by the number (i.e. append the character corresponding to each digit of the number to this new string).
Try this out:
char *tmp = new char [ stelen(original) ];
itoa(integer,intString,10);
output = strcat(tmp,intString);
//use output string
delete [] tmp;

Resources