Good evening. The task is to enter a number and enter a number system. And get a string containing the decimal notation.

#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { int n; int a; char buffer[20]; printf("Vvedite chislo\n"); scanf("%d",&a); do { printf("Sistema schisleniya \n"); scanf("%d",&n); itoa(a,buffer,10); } while(n>10 || n<0); printf("Chislo = %s\n", buffer); system ("pause"); return 0; } 

Here is the code, but the program does not work correctly. I can not find a mistake. How to fix it?

  • "does not work correctly" - describe the input and the expected / received output - Igor
  • @Igor Enter the number in the n number system and enter the number system itself. Result: number in decimal notation - Serg
  • one
    You write to buffer again and again in a loop, overwriting everything that was already in it at each iteration. - ߊߚߤߘ
  • 2
    Wow - "not quite" correct! - Igor
  • 3
    void main (void) {char st [10]; char buffer [20]; gets (st); itoa (st, buffer, 10); } In Studio, they don't even compile, because the type of the parameter in itoa is wrong. So it’s hard to believe in “not quite correctly working”, it cannot work - Vladimir Martyanov

1 answer 1

It became painful to look at this correspondence in the comments ...

 #include <stdlib.h> #include <stdio.h> #include <string.h> #include <ctype.h> int main() { for(char buf[256];printf("Value: "), gets_s(buf,256);) { // Считывание основания системы счисления unsigned int n = 0; printf("Base: "); scanf("%u",&n); while(getc(stdin) != '\n'); if (n == 0) break; // Преобразование; проверки переполнения нет! unsigned long long val = 0; int ok = 1; for(char*c = buf;*c;++c) { unsigned int dig = 0; if (!isalnum(*c)) { ok = 0; break; } if (isdigit(*c)) dig = *c - '0'; else dig = toupper(*c)-'A'+10; if (dig >= n) { ok = 0; break; } val = val*n + dig; } if (ok) printf("%s_%d = %llu_10\n\n",buf,n,val); else printf("Wrong input\n\n"); } }