0

When i run this code it doesn't display as i wish to be displayed. Don't mind the "//" it's in norwegian. Anyway when i run this code it works until i print the receipt it prints pizza and kebab too even if i just typed 1 or 2 i don't understand. mat means food and drikke means drinks. When i type 3 on hamburger it says kebab like i'm new to this and please help. On drinks it says sprite 102 on the receipt

#include <stdio.h> //Dette er bibloteker som gjør det mulig å skrive koden, fordi de inneholder mange forskjellige kollksjoner av uttrykk, koder osv.
#include <stdlib.h>
#include <conio.h>


int main() //Inni dette er det alt som skjer i koden.
{
    int pizza; //int er en datatype som står for integer. og da må man gi variabelen en verdi som tall.
    int Kebab;
    int hamburger;
    int cola;
    int fanta;
    int sprite;
    int mat;
    int drikke;
    char spise; //char er en datatype som står for character. her gir du variabelen en verdi på en bokstav.
    char mer = 'y';
    float sum = 0; // her er float en datatype, nesten som int bare at du kan inkludere desimaler.
        
        
        
        
        while(mer == 'y') //while loop er loop som går hvis testUttrykket er sann eller falsk.
        //Hvis den er sann går loopen men hvis den er falsk så stopper loopen.
        
        {
        
            printf("\n\t Welcome to Jenas Fast food\n"); // printf bare printer det som står inni parantesen og "" tegnet.
            printf("\n\nwhat do you want to eat?\n");
            
            printf("\nPizza: (130kr) press 1 \t\t Kebab: (75kr) press 2 \t\t Hamburger: (55kr) press 3 \t No Food: press 0\n\n");
            scanf("%d", &mat); //scanf gjør det mulig å gi verdi til mat variablen som at du kn skrive det.
            //Man må ha med & foran varilen for å gi en verdi til den.
            
            if(mat == 1) //if statement, hvis det inni parantesen er sann så kjører den det som er inni if, hvis den er falsk så hopper den over.
            {
                printf("\n\t\t\t You chose Pizza (120kr)\n");
                pizza = pizza + 1;
                
            }
            
            else if(mat == 2)
            {
                printf("\n\t\t\t You chose Kebab (75kr)\n");
                Kebab = Kebab + 1;
                
            }
            
            else if(mat == 3)
            {
                printf("\n\t\t\t You chose Hamburger (55kr)\n");
                hamburger = hamburger + 1;
            }
            
            else if(mat == 0)
            {
                printf("\n\t\t\t You did not choose any food\n");
                
            }
            
            printf("\nDo you want a drink?\n\n");
            printf("Cola: (22,50kr) press 1 \t fanta: (22,50kr) press 2 \t sprite: (21,50kr) press 3\tNo drink: press 0\n\n");
            scanf("%d", &drikke);
            
            if(drikke == 1)
            {
                printf("\n\t\t\t You chose Cola (22,50kr)\n");
                cola = cola + 1;
            }
            
            else if(drikke == 2)
            {
                printf("\n\t\t\t You chose Fanta (22,50kr)\n");
                fanta = fanta + 1;
            }
            
            else if(drikke == 3)
            {
                printf("\n\t\t\t You chose sprite (21,50kr)\n");
                sprite = sprite + 1;
            }
            
            else if(drikke == 0)
            {
                printf("\n\t\t\t You did not choose any Drinks\n");
                
            }
            
            printf("\n\nDo you want anything more?\tpress y for yes and n for no\n\n");
            scanf(" %c", &mer);
            
        }
        
        printf("\n\nDo you want to eat inside (+0kr) or outside (+5kr)?\t press i for inside and o for outside\n\n");
        scanf(" %c", &spise);
        printf("\nspise = %c", spise);
        
        if(spise == 'i')
        {
            printf("\n\n\tYou are going to eat Inside(+5Kr)");
        }
        
        else if(spise == 'o')
        {
            printf("\n\n\tYou are going to eat Outside");
        }
        
        
        system("cls"); //kommandovinduet fjerner alt tekst og stater på nytt, men koden fortsetter å gå.
        printf("\t\tRECEIPT\n");
        printf("__________________________________________\n");
        
        printf("\n\t\tFOOD\n\n");
        
        if(pizza > 0)
        {
            printf(" Pizza\t\t %d", pizza);
            
        }
        
        else if(Kebab > 0)
        {
            printf(" kebab\t\t %d", Kebab);
            
        }
        
        
        else if(hamburger > 0)
        {
            
            printf(" Hamburger\t\t %d", hamburger);
            
        }
        
        printf("\n\n__________________________________________\n");
        
        printf("\n\t\tDRINKS\n\n");
        
        if(cola > 0)
        {
            printf(" cola\t\t %d", cola);
            
        }
        
        else if(fanta > 0)
        {
            printf(" Fanta\t\t %d", fanta);
            
        }
        
        
        else if(sprite > 0)
        {
            printf(" Sprite\t\t %d", sprite);
            
        }
        
}

  • nothing in this code is C++. Do you compile this as C++ or as C? – 463035818_is_not_an_ai Sep 26 '22 at 11:40
  • `pizza = pizza + 1` is undefined behavior as `pizza` is uninitialized. *"Undefined behavior means anything can happen including but not limited to the program giving your expected output. But never rely on the output of a program that has UB. The program may just crash."* – Jason Sep 26 '22 at 11:40
  • 1
    `pizza = pizza + 1;` and others is `UB`, because `pizza` (and others) are used uninitialized. Your compiler should have warned you – 463035818_is_not_an_ai Sep 26 '22 at 11:41
  • 1
    There's a logical problem in your summary; you only print the number of kebabs if no pizza was ordered, and you only print the number of hamburgers if only hamburgers were ordered, and similarly for the drinks. Think about what the word "else" means. – molbdnilo Sep 26 '22 at 11:44
  • https://en.cppreference.com/w/cpp/language/ub – Jesper Juhl Sep 26 '22 at 11:48
  • Useful debugging habit: just unconditionally printing everything as the summary would have provided some clues about what's going on. – molbdnilo Sep 26 '22 at 12:40

0 Answers0