Mongow
Mongow

Reputation: 69

How do this except string.resize?

How to write this code without the resize function? wartosc.resize(10);

Because without use resize in string, nothing happens - doesn't assign values.

string naBinarny(int liczba){

    string wartosc;
    int i=0;

    wartosc.resize(10);

    while (liczba>0) {
        wartosc[i] = ((liczba%2) == 1 ? '1' : '0');

        liczba=liczba/2;

        i++;

    }


    return wartosc;
}

Upvotes: 0

Views: 76

Answers (1)

DNT
DNT

Reputation: 2395

How about this:

string naBinarny(int liczba){

string wartosc="";
int i=0;

    while (liczba>0) {
        wartosc += ((liczba%2) == 1 ? "1" : "0");

        liczba=liczba/2;

        i++;

   }


    return wartosc;
}

Upvotes: 1

Related Questions