Reputation: 571
So I got asked this on a skills test for an internship, and at the time it completely baffled me. I have some code now that I thought should work, but isn't assigning the correct values to the string.
#include <iostream>
#include <string>
using namespace std;
int main()
{
// declarations
int i = 0, num= 63;
string b="";
while (num != 0)
{
i = num % 10;
b.insert(0,i + 48 + "");
num = num/10;
}
cout << b << endl;
return 0;
}
With "num" being the integer value (I just used 63 as an example)
EDIT:: I incorrectly paraphrased, I could not use any function that did an int to string conversion for me, not that I couldn't use the string library, my mistake.
Upvotes: 4
Views: 4439
Reputation: 477020
Here's some code that doesn't use any libraries and doesn't perform allocations. Instead, the user is expected to provide a buffer that's large enough, and she receives a pointer to the beginning of the result string in return:
char * format(unsigned int n, char * buf, std::size_t len)
{
if (len == 0) return NULL;
char * p = buf + len;
*--buf = 0;
while (n && p > buf)
{
*--buf = '0' + (n % 10);
n /= 10;
}
return n == 0 ? p : NULL;
}
Embedded people might like this especially because the function doesn't do anything outside your control.
Usage:
char buf[100];
char * s = format(12345, buf, sizeof buf);
if (s) { printf("Formatted: %s\n", s); }
Upvotes: 0
Reputation: 10936
Very simple solution: Use string streams. In this case, an output string stream:
#include <iostream>
#include <string>
#include <sstream>
std::string IntToString(int value) {
std::ostringstream ss;
ss << value;
return ss.str();
}
const char* IntToCString(int value) {
std::ostringstream ss;
ss << value;
return ss.str().c_str();
}
Upvotes: 0
Reputation: 153820
It is worth noting that there are other errors with the code. For example, the transformation doesn't work for num == 0
. Here is a better version:
#include <iostream>
#include <algorithm>
#include <string>
#include <cstdlib>
int main(int ac, char* av[])
{
int num(ac == 1? 0: std::atoi(av[1]));
std::string b;
do
{
b.push_back((num % 10) + '0');
num /= 10;
}
while (num != 0);
std::reverse(b.begin(), b.end());
std::cout << b << '\n';
return 0;
}
Upvotes: 0
Reputation: 3335
I would suggest recursive function:
std::string f( int val )
{
if ( val >= 10 )
return f( val / 10 ) + ( char )( val % 10 + '0' ) ;
else
return std::string( 1, '0' + val ) ;
}
Usage:
std::cout << f( 63 ) << std::endl ;
Note that this function should append to your string instead of using very expensive shifting (and putting in the front of the string.)
Upvotes: 0
Reputation: 109119
Change the insert line to
b.insert(0, 1, '0' + i);
This will insert the character obtained from adding i
to '0'
once at index 0 in the string.
Explanation of why your code isn't working:
b.insert(0,i + 48 + "");
the line above is calling
basic_string::insert( size_type index, const CharT* s );
where the pointer is being determined by adding 48 + i
to the address of the ""
string literal, resulting in garbage.
Upvotes: 6