if you do
Code:
string s = "0";
setOne(s,3);//should result int 1000
note that string is only 1 char long, therefore you need first to lengthen it
then you're passing value as being a copy of the original string. You should use a pointer or a reference to afect the original string
And, you return temp which is a integer conversion of the original string not the changed one.
To finish don't store the number inside a int, because atoi will asume base 10. A 32 bit int will be 32 bits long but only 10 decimal digit long. Therefore if you have a 32bit number representation inside the string atoi will return overflowed number.
Code:
int setOne(string& value, int position)
{
while(position >= value.length())
value = "0"+value;
int length = value.length();
int temp;
//string t = value;
value[length-(position+1)] = '1';
temp = atoi(value.c_str());//not recomended
cout << temp << "\n";
return temp;
}
Should work...
I would do it like
Code:
void set_one(string& s, unsigned int pos){
while(pos >= s.length())
s = "0"+s;
s[s.length()-1-pos]='1';
}