Search code examples
coctal

Only allowing octal numbers


What my code does is:

  • User selects either a conversion from decimal to octal or octal to decimal.
  • User puts in a number
  • User gets result of conversion

Now i want to only allow otcal numbers when converting from octal to decimal and give out a error message if the number isn't octal. How can this be achieved?

#include <stdio.h>
#include <math.h>

int UmrechnungDezimalZuOktal(int dezimalzahl), UmrechnungOktalZuDezimal(int oktalzahl);
int main()
{
    char auswahl;
    int dezimalzahl, oktalzahl, nichtBeendet = 1, c;
    

    while (nichtBeendet) {
            printf("\n\n  ##################  AUSWAHL  ##################\n"
                "  #                                             #\n"
                "  #     1) Konvertierung Dezimal -> Oktal       #\n"
                "  #     2) Konvertierung Oktal -> Dezimal       #\n"
                "  #     3) Abbruch                              #\n"
                "  #                                             #\n"
                "  ###############################################\n\n");

            printf("Auswahl: ");
            scanf("%c", &auswahl);

            switch (auswahl) {
            case '1': {                                                                                      /* Konvertierung Dezimal -> Oktal */
                printf("Geben Sie eine positive, ganze Dezimalzahl ein: ");
                scanf("%d", &dezimalzahl);

                if (dezimalzahl >= 0)
                {
                printf("Die Dezimalzahl %d entspricht der Oktalzahl %d", dezimalzahl, UmrechnungDezimalZuOktal(dezimalzahl));
                }
                else
                {
                printf("Bitte nur positive Zahlen eingeben!");
                }
                break;
            }
            case '2': {                                                                                      /* Konvertierung Oktal-> Dezimal */
                printf("Geben Sie eine positive, ganze Oktalzahl ein: ");
                scanf("%d", &oktalzahl);

                if (oktalzahl >= 0)
                {
                printf("Die Oktalzahl %d entspricht der Dezimalzahl %d", oktalzahl, UmrechnungOktalZuDezimal(oktalzahl));
                 }
                else
                {
                    printf("Bitte nur positive Zahlen eingeben!");
                }
                break;

            }
            case '3': {                                                                                      /* Abbruch */
                nichtBeendet = 0;
                break;
            }
            default: {                                                                                       /* Ungültige Eingabe*/
                printf("Ungueltige Eingabe!");
                break;
            }
            }
            while ((c = getchar()) != '\n' && c != EOF) {};
        }
}

int UmrechnungDezimalZuOktal(int dezimalzahl)                                                               /* Rechnung Dezimal -> Oktal */                                                         
{
    int oktalzahl = 0, i = 1;

    while (dezimalzahl != 0)
    {
        oktalzahl += (dezimalzahl % 8) * i;
        dezimalzahl /= 8;
        i *= 10;
    }

    return oktalzahl;
} 

int UmrechnungOktalZuDezimal(int oktalzahl)                                                                 /* Rechnung Oktal -> Dezimal*/
{
    int dezimalzahl = 0, i = 0;

    while (oktalzahl != 0)
    {
        dezimalzahl += (oktalzahl % 10) * pow(8, i);
        ++i;
        oktalzahl /= 10;
    }

    i = 1;

    return dezimalzahl;
}

Solution

  • I would replace all the scanf and harcoded functions with fgets and strtol:

    enum {oct = 8, dec = 10, hex = 16};
    
    int to_number(int base, bool *match)
    {
        char str[32];
        long num = 0;
    
        *match = false;
        if (fgets(str, sizeof str, stdin))
        {
            char *ptr;
    
            num = strtol(str, &ptr, base);
            *match = (*ptr == '\n'); // match is true if strtol stops scanning
                                     // at the newline left by fgets
        }
        return (int)num;
    }
    
    ...
    
    bool match;
    int num = to_number(oct, &match);
    
    printf("%d %s an octal\n", num, match ? "is" : "is not");
    

    EDIT:

    As pointed out by @klutt in comments, since strtol returns a long, ideally the function should also return long in order to avoid the cast, then, if you really need an int, you can cast the return of the function:

    long to_number(int base, bool *match);
    
    int num = (int)to_number(oct, &match);