Hello. I wrote code to translate from any ss to any (up to 16). But I also need to add a condition so that when filling any number in 2nd cc, it gives an error and does not translate. I wrote a condition, but for some reason it does not want to work normally, I will be grateful if you help me correct and supplement it.

for (i = 0; i < st.length(); i++) if (ss1 < 10 && st[i] >= ss1) { cout << "Error" << endl; system("pause"); return 0; } 

This is for any ss <10. Help please do also for 11..16.

 #include <iostream> #include <string> #include <math.h> using namespace std; int main() { int i, dec, k, p, ss1, ss2; string st, hex; string digits[16] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F" }; cout << "chislo" << endl; cin >> st; cout << "ss1" << endl; cin >> ss1; cout << "ss2" << endl; cin >> ss2; for (i = 0; i < st.length(); i++) if (ss1 < 10 && st[i] >= ss1) { cout << "Error" << endl; system("pause"); return 0; } dec = 0; p = st.length() - 1; for (i = 0; p >= 0; i++, p--) { k = toupper(st[i]); dec += ( (k>='A') ? 10+k-'A' : k-'0') * pow(ss1,p); } do { hex.insert(0, digits[dec % ss2]); dec /= ss2; } while (dec != 0); cout << hex << "\n"; system("pause"); return 0; } 
  • Write the tests first. I do not know what you have written there in the code, but checking the input of a binary number is very simple: a) strspn() + strlen() , b) strtoull() , and no C ++ is needed. - 0andriy

1 answer 1

First, your test st[i] >= ss1 does not work correctly, because it compares a symbol not with a symbol, but with a number ...

I would do this:

 if (toupper(st[i]) >= digits[ss1-1][0]) { cout << "Error" << endl; 

Only this verification relies on the fact that you have input only from numbers and specified letters.

By the way, why do you have digits - an array of strings? where it would be more convenient to work with

 char digins[] = "0123456789ABCDEF"; 

I would rewrite the main loop for converting a string to a decimal number without pow :

 for (i = st.length() - 1; i >= 0; --i) { k = toupper(st[i]); dec = dec*10 + ((k>='A') ? 10+k-'A' : k-'0') ; } 

Yes, and with the conclusion ... In my opinion, it would be easier to make the line inverse, and then expand, say. At least, much more efficiently than fussing with insert .

Update Solution:

 #include <iostream> #include <string> #include <cctype> using namespace std; int main() { string digits = "0123456789abcdefghijklmnopqrstuvwxyz"; unsigned int maxRadix = digits.length(); string value; unsigned int from, to; cout << "Enter number from_radix to_radix: "; cin >> value >> from >> to; if (from < 2 || from > maxRadix || to < 2 || to > maxRadix) { cerr << "Wronf radix(es)\n"; return 1; } for (size_t i = 0; i < value.length(); i++) { size_t pos = digits.find(tolower(value[i])); if (pos == string::npos || pos > from) { cerr << "Wrong symbol in value " << value << endl; return 1; } } unsigned long long number = 0; for (size_t i = 0; i < value.length(); ++i) { char k = tolower(value[i]); number = number*from + ((k>='a') ? k-'a' + 10 : k-'0'); } cout << "Decimal: " << number << endl; string result; while(number) { result.push_back(digits[number%to]); number /= to; } for(int i = 0, j = result.length()-1; i < j; ++i, --j) { char x = result[i]; result[i] = result[j]; result[j] = x; } cout << result << endl; system("pause"); return 0; } 
  • Thanks for the help, but for some reason it does not work) I still tried to do this: if (toupper (st [i])> = digits [ss1-1] [0] + '0') but it still does not work. If ss1 <ss2 always gives Error on your variant and never just gives mine when mine) - Roman
  • Maybe I was wrong somewhere, but I didn’t compile it, so I gave ideas to be critical :) So, look at the finished version of the program in response ... - Harry