void FunctionChar_Line() { int digit, k = 0, i=0; cin >> digit; int a = digit; while (a > 0) { a = a / 10; k++; } vector <char> string (k+1); while (i != k) { while (digit > 0) { int mod = digit % 10; digit = digit / 10; string[i] = mod; i++; } } i = 0; while (string[i] != '\0') { cout << string[i]; i++; } } 

Implementing the conversion of numbers to strings does not work. I think the problem is with the wrong array creation. Tell me how best to create an array for the string.

    2 answers 2

    Try to replace

     string[i] = mod; 

    on

     string[i] = '0'+mod; 

    Yes, and the cycle must be deployed ...

    If string is not suitable, and it is the vector<char> that is desperately needed, here’s an option:

     void FunctionChar_Line() { int digit; cin >> digit; vector<char> s; while (digit > 0) { s.push_back('0' + digit%10); digit /= 10; } for(int i = s.size()-1; i >=0; --i) { cout << s[i]; } } 
    • hmm .. worked .. Thanks a lot. cycle deploy =) - putniy
    • See addition in the answer. Well, if the answer suits you - mark as accepted ... - Harry

    I would do this:

     void FunctionChar_Line() { unsigned int digit; cin >> digit; sting s; while (digit > 0) { s = ('0' + digit%10) + s; digit /= 10; } cout << s; }