-2

I have a program that should translate values ​​from one number system to another, but I have a problem with "_itoa_s" writes that [Error] '_itoa_s' was not declared in this scope I tried to connect libraries <cstdlib> and <stdlib.h> I also tried replacing itoa with "snprintf" but it does not help in the compiler there are even more errors, please help me fix the error, Here is the code:

#include <cstring>
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
setlocale(LC_ALL, "rus");
int in, iz, k, s = 0, p;
char str[255];
cout << "Enter the number system from which you want to translate" << endl;
cin >> iz;
cout << "Enter the number system to which we will translate" << endl;
cin >> in;
cout << "Enter the number" << endl;
cin >> str;
p = strlen(str) - 1;
for (int i = 0; i < str[i]; i++)
{
    switch (str[i])
    {
    case 'a': k = 10; break;
    case 'b': k = 11; break;
    case 'c': k = 12; break;
    case 'd': k = 13; break;
    case 'e': k = 14; break;
    case 'f': k = 15; break;
    case '1': k = 1; break;
    case '2': k = 2; break;
    case '3': k = 3; break;
    case '4': k = 4; break;
    case '5': k = 5; break;
    case '6': k = 6; break;
    case '7': k = 7; break;
    case '8': k = 8; break;
    case '9': k = 9; break;
    case '0': k = 0; break;
    }
    s = s + k * pow(iz, p);
    p--;
}
char result[255];
_itoa_s(s, result, in);
cout << "The result of a translation from a radix  " << iz << " to radix " << in << " = " << result;

return 0;
}
Ramazan
  • 1
  • 2
  • I don't understand why you would need `itoa()` at all? Anyways, the `_itoa_s()` is a compiler specific variant, that's not available from the c++ standard. – πάντα ῥεῖ Sep 28 '20 at 17:49
  • @πάνταῥεῖ ok what can i use instead of itoa? I read that it is not available for conventional compilers and used alternatives among which was "snprintf" but it did not work either – Ramazan Sep 28 '20 at 18:17
  • There are still the versions without the length limitation, you just need to ensure that your buffer is big enough to receive the result. With `sprintf()` you can test that in advance. Though, the c++ standard, and easiest way would be to use a `std::ostringstream` and `std::string' for the result. – πάντα ῥεῖ Sep 28 '20 at 18:21
  • Do you need to use `switch` statement? This could be simplified to using an array. – Thomas Matthews Sep 28 '20 at 18:21
  • You could try, for decimal digits, `char c = '0' + value;` For letter digits, `char c = (value - 10) + 'a';` Note: this assumes ASCII encoding for the letter digits. Sorry EBCDIC fans. – Thomas Matthews Sep 28 '20 at 18:24
  • Don't use `pow`, as its for floating point numbers. You may encounter accuracy issues when converting between integers and floating point numbers (and floating point to integer). Instead, multiply your "sum" variable by the radix in each loop. – Thomas Matthews Sep 28 '20 at 18:27
  • @πάνταῥεῖ I tried to put both std :: ostringstream and std :: string, but both options do not help, errors come out, if you want I can write them using edits – Ramazan Sep 28 '20 at 18:29
  • @Ramazan Recommended read: https://stackoverflow.com/questions/5590381/easiest-way-to-convert-int-to-string-in-c – πάντα ῥεῖ Sep 28 '20 at 18:31
  • You need a `default` case to handle the case of the your program encountering invalid digit characters. – Thomas Matthews Sep 28 '20 at 18:57
  • @ThomasMatthews wait, I'm trying to make the code work with your example))) – Ramazan Sep 28 '20 at 19:03
  • @ThomasMatthews I didn’t understand how to implement this, perhaps because I hadn’t worked with such works before, and perhaps because I do not have enough programming skills – Ramazan Sep 28 '20 at 20:27

1 Answers1

0

Here's an alternative that doesn't involve switch, pow or itoa:

// Notice, the index is the value of the digit.
const std::string digits[] = "0123456789abcdef";
//...
const size_t length = str.len;
// Note: iz is the radix for "input" conversion
int number = 0;
for (unsigned int i = 0; i < length; ++i)
{
  const std::string::size_type position = digits.find(str[i]);
  number = number * iz; // Shift the number by one digit.
  number += position;
}

Notice: error handling, such as invalid digits, is left as an exercise for the OP. Invalid digits are not restricted to characters outside the set, but also digits whose index is greater than the conversion radix.

For ultimate understanding, single step through the code with pen and paper. :-)

You can use the table for converting to the target radix, in (I'd rather use a more descriptive variable name).

std::string translated_number;
while (number > 0)
{
  const unsigned int index = number % output_radix; // Output_radix == 'in'
  const char digit_character = digits[index];
  translated_number.insert(0, 1, digit_character);
  number = number / output_radix;
}
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154