Search code examples
cif-statementswitch-statementreturndefault

C switch returns default inside if/else


I have the following problem: i have two switch statements. They work perfectly when separated, but the minute I put both of them into an if/else. switch always returns the default(error). I am sorry for the difficult wording, now I have copied in the whole program so you can check it, I have tried what you said, but it didn't seem to help. So for an example, when it asks for the number, my input is 2. Then it asks for what character do I want to choose (+,-,/,*), and no matter which I write in, it gives me the default output like I have type a wrong character.

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

void tomb_beolvas(int *szamok);
void tomb_kiir(int *szamok);
void tomb_sorrend(int *szamok);
void sorrend_kiir(int *szamok);
void kalkulator(int *szamok);

int main() {
    int szamok[10];
    tomb_beolvas(szamok);
    tomb_kiir(szamok);
    tomb_sorrend(szamok);
    sorrend_kiir(szamok);
    kalkulator(szamok);
    return 0;
}

void tomb_beolvas(int *szamok) {
    int i;
    srand(time(0));
    for (i = 0; i < 10; i++) {
        szamok[i] = rand() % (10) + 1;
    }
    return;
}

void tomb_kiir(int *szamok) {
    int i;
    for (i = 0; i < 10; i++) {
        printf("%d\n", szamok[i]);
    }
    return;
}

void tomb_sorrend(int *szamok) {
    int i;
    int a, j;
    for (i = 0; i < 10; ++i) {
        for (j = i + 1; j < 10; j++) {
            if (szamok[i] > szamok[j]) {
                a =  szamok[i];
                szamok[i] = szamok[j];
                szamok[j] = a;
            }
        }
    }
    return;
}

void sorrend_kiir(int *szamok) {
    int i;
    for (i = 0; i < 10; i++) {
        printf("\n%d", szamok[i]);
    }
    return;
}

void kalkulator(int *szamok) {
    char jel;
    int a, b, donto;
    printf("\nKerem valassza ki milyen modon szeretne megadni az adatokat:\n"
           " 1.: egyben(peldaul 5. + 8.)\n"
           " 2.: kulon(peldaul + aztan 5. es 8.)\n");
    scanf("%d", &donto);
    if (donto == 1) {
        printf("\nKerem irja be hanyadik szamokat szeretne es koze hogy milyen kalkulaciot szeretne vegezni(pl.: 5 + 8):\n");
        while (scanf("%d %c %d", &a, &jel, &b)) {
            switch (jel) {
              case '+':
                printf("%d", szamok[a-1] + szamok[b-1]);
                break;

              case '-':
                printf("%d", szamok[a-1] - szamok[b-1]);
                break;

              case '*':
                printf("%d", szamok[a-1] * szamok[b-1]);
                break;

              case '/':
                printf("%d", szamok[a-1] / szamok[b-1]);
                break;
            }
        }
    } else
    if (donto == 2) {
        printf("Adj meg egy jelet (+, -, *, /): ");
        scanf("%c", &jel);

        printf("add meg hanyadik szamokkal akarsz szamolni: ");
        scanf("%d %d", &a, &b);

        switch (jel) {
          case '+':
            printf("%d + %d = %d", szamok[a], szamok[b], szamok[a] + szamok[b]);
            break;

          case '-':
            printf("%d - %d = %d", szamok[a], szamok[b], szamok[a] - szamok[b]);
            break;

          case '*':
            printf("%d * %d = %d", szamok[a], szamok[b], szamok[a] * szamok[b]);
            break;

          case '/':
            printf("%d / %d = %d", szamok[a], szamok[b], szamok[a] / szamok[b]);
            break;

          // operator doesn't match any case constant +, -, *, /
          default:
            printf("Error! operator is not correct");
        }
    }
    return;
}

Solution

  • I'm afraid your problem is a classic scanf() issue: instead of scanf("%c", &jel); you should use

    scanf(" %c", &jel);  // notice the initial space before the `%c`
    

    This skips the newline left pending after the scanf("%d", &donto);.