Task

Given a number in binary, octal, hexadecimal. Convert to decimal number. Write a program that reads the base number and the sequence of digits of the original number. The sequence of numbers is read into an array of type char. After conversion to decimal, output a sequence of decimal digits. Each transformation is implemented as a separate function.

Input data "Base number" "a sequence of numbers according to the basis of" A sequence of digits not more than 6, without a sign, if you enter more than 6 digits, process the first 6. Maximum number of digits 10.

Output

The first line duplicates the input. The second line contains a decimal representation of the result. If the basis number is entered incorrectly, then in the second line output: Base is wrong

If the initial number is set incorrectly, in the second line output: Number is wrong

Here is my code:

#include <stdio.h> #include <stdlib.h> int enter(int bazis, char str[10], int error, int result) { int error = 0; char str[10] = {0}; char error_message_1[14] = "Base is wrong"; char error_message_2[16] = "Number is wrong"; printf("Введите базис и последовательность: "); scanf("%d %6s", &bazis, str); for(int i = 0; str[i] != '\0' && i < 6; i++) { } if(bazis != 2 && bazis != 8 && bazis != 16) { error++; } else { if(bazis == 2) { for(int i = 0; i < 6; i++) { if(str[i] != '0' && str[i] != '1') { error = error + 2; break; } } } if(bazis == 8) { for(int i = 0; i < 6; i++) { if(str[i] < '0' || str[i] > '7') { error = error + 2; break; } } } if(bazis == 16) { for(int i = 0; i < 6; i++) { if(str[i] < '0' || str[i] > 'F') { error = error + 2; break; } } } } if(result = 1) { result = error_message_1; } else { result = error_message_2; } system("pause"); return(result); } int main(void) { int enter(); return(0); } 

The problem is that I do not quite understand the principle of working with strings, my program for determining the correctness of the input works only half correctly. If I enter a sequence of 6 or more digits, then everything works, and if it is less than 6, then the program already displays number is wrong. First of all, I would like to understand why? The code seems to be correct. My second question is that I also do not understand very well the principle of how user functions work in C, in which sequence they are executed? How to transfer values ​​from one function to another? And how it would be possible to write this program in a shorter way, because it seems to me that I have too many conditional operators and all this could be implemented as something else. And is my code correct in terms of procedural programming?

  • There is a cunning trick to avoid such problems - to write a constant to the left: 1 = result , then the compiler will immediately indicate an error to you. Your program does not work at all because the entry point is incorrect, and int enter(); is a function declaration, not a call. - VTT
  • And how to do it right? And how to call a function correctly? - David
  • one
    Call the function: int result = enter(); . Feel the difference? - avp
  • Yes, I feel, thank you very much - David

0