dimanche 17 janvier 2021

C switch returns default inside if/else

I have the following problem: i have two switch cases . They work perfectly when separated, but the minute I put them into an if/else the 2. switch always returns the default(error). Ive tried o fix it over an hour but still no progress.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
int main()
{
    int szamok[]={};
    int i, donto;
    srand(time(0));
    for(i = 0; i < 10 ; i++)
    {

            szamok[i] = rand()%(10)+1;
    }
    for(i = 0; i < 10; i ++)
    {
        printf("%d\n", szamok[i]);
    }
    char jel;
    int a, b;
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");
scanf("%d %c %d", &a, &jel, &b);

switch(jel)
{
    case '+': printf("%d + %d = %d",szamok[a-1], szamok[b-1], 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;

}

Aucun commentaire:

Enregistrer un commentaire