string - C++ Question mark output -
i have simple program converts decimal numbers binary numbers. no errors come when run program single question mark. should set of values "00101" trying use function returns string well. here code,
#include <iostream> using namespace std; #include <string> string convert(int num) { string binary; int remainder; }
any ideas? help
there several problems code. first, assigning string binary
using =
sign on line binary = remainder
. meant write binary += remainder
, append remainder string.
the second problem on line. both string::operator=
, string::operator+=
have overloads take char. overloads called, when pass in int. string being set character ascii value 0 or 1, question mark character, not looking for. can use std::to_string
convert int
string. or if need level of control on formatting, can use std::ostringstream
in this answer.
in other words, change binary = remainder;
binary += std::to_string(remainder)
.
third problem: there return statement inside while
loop. function return after 1 iteration of loop, no matter how large num
is. remove return statement, there 1 @ end of convert
function.
Comments
Post a Comment