This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
C++ concatenate string and int
Hi,
In C# I can write like this:
int i = 0;
string text = "out.jpg";
while(true)
{
i++;
Object.write(i+text, stream);
}
But this is not true for C++. the problem is at: i + default.
How could I fix this in C++?
Thanks in advance. Your help is much appreciated!
You could use a stringstream...
std::stringstream ss;
ss << i << text;
Object.write(ss.str(), stream);
default is a keyword in C++. You can't have string default in C++. And I don't see what you are trying to achieve. Please clarify
take a look at stringstreams or boost.format http://www.boost.org/doc/libs/1_38_0/libs/format/doc/format.html
boost::format("%1%%2%") % i % default_;
default is a reserved keyword. Change the variable name to defaultStr or similar, and everything should work fine.
Related
This question already has answers here:
Dynamically allocate user inputted string
(4 answers)
Closed 5 years ago.
I want to get unlimited chars, actually I created char*, I use cin for it, and I want to change it to string.
I don't know the length of the input which user enters, so I made this solution for myself.
Can someone tell me plz, how to get a char* without knowing the size of input and converting to string.
thanks.
Since it is C++. Just use std::string
If you really need to use char* look at this topic
Instead of using a char *, use the standard library.
#include <string>
#include <iostream>
int main()
{
std::string data;
std::getline(std::cin, data);
std::cout << data << '\n';
}
This will read a string of any length (at least, until a newline is entered, which will not be included in data) and then print it out.
You might wish to also check the state of std::cin to test if any errors occurred.
This question already has answers here:
printf with std::string?
(9 answers)
Closed 9 years ago.
So this is my code
string name;
cout <<"\n Enter Your name : \n";
cin >> name;
printf("%s" , name);
and for some weird reasons codeblocks crashes at this
why ?
also how could I fix it ?
thanks
You should compile with all warnings (e.g. g++ -Wall). You'll get a useful warning. You want to use c_str like this
printf("%s", name.c_str());
BTW, why use printfand why do you forget a \n at the end of the printf format string? (or use fflush)
Better code:
cout << name << endl;
If you need to pass your std::string to a function that accepts / uses C-style strings (const char *) for input, use .c_str(). It returns a const char *.
This is what you should do when needing to work with existing libraries, system calls, etc. For your own code, it is usually better to find a more C++ way of doing it.
In this case:
std::cout << name << std::endl;
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Easiest way to convert int to string in C++
Does anyone know how to do that conversion?
I need to concatenate a intptr_t type to a string, and therefore need to convert it.
It's not an int, as It's a 64 bit OS.
Correct me if I'm wrong
Thanks
intptr_t is just a number. Therefore:
Easiest way to convert int to string in C++
...and others
Converting numbers to strings and strings to numbers
Simple:
std::to_string(ip);
Well, simple if you have C++11.
std::stringstream ss;
ss << ip;
ss.str();
(or if you prefer:
ss << std::hex << ip;
)
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Convert double to string C++?
I searched in the page but i haven't found the solution.
I have a method to convert from int to string. But now i need to convert from float/double to string. Because i want to write some data in a file.
Anyone could help me ??
Thanks in advance.
http://www.daniweb.com/software-development/cpp/threads/146718
#include <sstream>
std::string Convert (float number){
std::ostringstream buff;
buff<<number;
return buff.str();
}
You could use sprintf.
You could use stringstream with << operator.
See also Convert double to string C++?, How do I convert a double into a string in C++?, Convert double to string using boost::lexical_cast in C++?, Converting Double to String in C++, etc.
Can you not use the standard (C) function sprintf/fprintf etc?
This can help you : Convert double to string C++?
Is your file written using IOStreams? If so, just do this:
stream << number;
If not, and you really need a string, you can use an ostringstream for this. Boost's lexical_cast wraps the string streams in an easy-to-use fashion.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to parse a string to an int in C++?
How do you convert a C++ string to an int?
Assume you are expecting the string to have actual numbers in it ("1", "345", "38944", for example).
Also, let's assume you don't have boost, and you really want to do it the C++ way, not the crufty old C way.
#include <sstream>
// st is input string
int result;
stringstream(st) >> result;
Use the C++ streams.
std::string plop("123");
std::stringstream str(plop);
int x;
str >> x;
/* Lets not forget to error checking */
if (!str)
{
// The conversion failed.
// Need to do something here.
// Maybe throw an exception
}
PS. This basic principle is how the boost library lexical_cast<> works.
My favorite method is the boost lexical_cast<>
#include <boost/lexical_cast.hpp>
int x = boost::lexical_cast<int>("123");
It provides a method to convert between a string and number formats and back again. Underneath it uses a string stream so anything that can be marshaled into a stream and then un-marshaled from a stream (Take a look at the >> and << operators).
I have used something like the following in C++ code before:
#include <sstream>
int main()
{
char* str = "1234";
std::stringstream s_str( str );
int i;
s_str >> i;
}
C++ FAQ Lite
[39.2] How do I convert a std::string to a number?
https://isocpp.org/wiki/faq/misc-technical-issues#convert-string-to-num
Let me add my vote for boost::lexical_cast
#include <boost/lexical_cast.hpp>
int val = boost::lexical_cast<int>(strval) ;
It throws bad_lexical_cast on error.
Use atoi
Perhaps I am misunderstanding the question, by why exactly would you not want to use atoi? I see no point in reinventing the wheel.
Am I just missing the point here?
in "stdapi.h"
StrToInt
This function tells you the result, and how many characters participated in the conversion.