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; }
strspn()+strlen(), b)strtoull(), and no C ++ is needed. - 0andriy