C- Code won't be executed, can someone help me?

Ok, so I’m a computer science student (so I’m still learning) and I’m trying to program a drink maschine thingy. The thing is, it only executes the first if- block. I can’t find my mistake and would really appreciate some help, so that I can fix the mistake and know what I did wrong.

I made some comments in the code, so it’s better understandable for non German speakers, since the main text (which is mostly unimportant) is in german.

Here is the complete code for reverence

#include <stdlib.h>
#include <stdio.h>
void Fkt(int);

int main(void) {
    int x;   /*Not important in this part of code */
 
    char G;  /*The drink you choose will be G*/
    float p; /* p = price */
    
       
       printf("*** Getraenke Automat *** \n*W) Wasser    (0.5 Euro)* \n*B) Bionade     (1 Euro)*\n*O) Orangensaft (2 Euro)*\n***********************\n\nBitte ein Getränk auswaeheln ");  /*Basically just asking what drink you want with the prices */
    scanf("%c", &G);  /* User picks drink*/
       
    if ( G == 119 || 87) {    /*This if block works perfectly fine */
        printf("\n Bitte Zahlen Sie 0.5 Euro ");
        scanf("%f", &p);
        if ( p == 0.5) { /*If you pay the right amount of money */
            printf("\n Entnehmen Sie Ihr Getränk \n");
            }
        else { /*For the wrong amount of money*/
            printf("\nGeben Sie den richtigen Betrag ein!\n");
        }
    }
    
   else if ( G == 66 || 98) {  /* This one is stated as "Will never be executed */
        printf("\n Bitte Zahlen Sie 1 Euro ");
        scanf("%f", &p);
        if ( p == 1) {
            printf("\n Entnehmen Sie Ihr Getränk \n");
            }
        else {
            printf("\nGeben Sie den richtigen Betrag ein!\n");
        }
    }
    
        
    return 0;
}

void Fkt(int x){
    printf("\n------------\n");
    
}
    

I tried to give every drink a different pice (Lemonade 1 Euro, Water 0.5 Cent) but no matter what input is given it always only gives me the price of the first drink.
I don’t know if I made a small silly error or just did it completly wrong.

So here is my problem area:

 if ( G == 119 || 87) {    /*This if block works perfectly fine */
        printf("\n Bitte Zahlen Sie 0.5 Euro ");
        scanf("%f", &p);
        if ( p == 0.5) { /*If you pay the right amount of money */
            printf("\n Entnehmen Sie Ihr Getränk \n");
            }
        else { /*For the wrong amount of money*/
            printf("\nGeben Sie den richtigen Betrag ein!\n");
        }
    }
    
   else if ( G == 66 || 98) {  /* This one is stated as "Will never be executed */
        printf("\n Bitte Zahlen Sie 1 Euro ");
        scanf("%f", &p);
        if ( p == 1) {
            printf("\n Entnehmen Sie Ihr Getränk \n");
            }
        else {
            printf("\nGeben Sie den richtigen Betrag ein!\n");
        }

>Solution :

I think you might want statements like if ( G == 66 || G == 98) instead of what you have. I don’t think the code as written does what you think it does.

When you do G == 66 || 98 it will evaluate G == 66 (which will evaluate to true or false) and then apply that value to 98 via the || operator, which I think will always return true (|| is usually applied to boolean values). It will not check whether G is 66 or 98…

Leave a Reply