dimanche 22 décembre 2019

How do I make an input from a function to work in an if statement in another function

I am trying to make a supermarket program, but from the first function when you select 'l' or 'o' for oranges, I want it to print a receipt based on your choosing and I tried to put after when the individual select that he was to pay in credit but it does not print the receipt, it just goes to middle ();

//program for a supermarket to buy lemon or oranges either by credit, full payment or part payment
#include <stdio.h>
#include <time.h> //This is to include time and time delay in the program
#include <stdlib.h> /*This is to include clear screen*/
int lemon, orange, days, PIN;
char account, pay, choice;
char fruit;
int lemons ();
int oranges();
int both();
int payment();
int Receipt(); //This to print the receipt of the item purchased
int middle();
float prices[2]={4.00, 5.00};
void delay (seconds) {
    int mseconds = 1000*seconds;
    clock_t start_time = clock();
    while (clock() < start_time + mseconds);
}

int main () {
    printf("-------------------------------------------------\n");
    printf("\tWelcome to the APEX Supermarket!\n");
    printf("-----------------------------------------------\n");
    printf("We sell various fruits, but today we are selling lemons and oranges\n");
    printf("What would you like to purchase?\n");
    printf("---------------------------------------------------\n");
    start:
    printf("Enter l for lemon\nEnter o for oranges\nEnter b for both fruits\n");
    scanf("%s", &fruit);
    if(fruit == 'l'||fruit == 'o'||fruit == 'b') {
    if (fruit == 'l') lemons();
    if (fruit == 'o') oranges();
    if (fruit == 'b') both();
}
    else {
        printf("Invalid input!\n");
        goto start;
    }

}
int lemons () {
        printf("It costs $%.2f per lemon\nHow many would you like to purchase?\n", prices[0]);
        scanf("%d", &lemon);
        printf("Your total cost is $%.2f\n", lemon*prices[0]);
        printf("What method of payment will you like to use?\n");
        payment();
    }

int oranges () {
        printf("It costs $%.2f per lemon\nHow many would you like to purchase?\n", prices[1]);
        scanf("%d", &orange);
        printf("Your total cost is $%.2f\n", orange*prices[1]);
        printf("What method of payment will you like to use?\n");
        payment();
}

int both (){
    printf("It costs $%.2f per lemon and $%.2f per orange\nHow many would you like to purchase?\n", prices[0],prices[1]);
    printf("How many oranges will you like to buy:");
    scanf("%d", &orange);
    printf("How many lemons will you like to buy:");
    scanf("%d", &lemon);
    printf("Your total cost is $%.2f\n", (lemon*prices[0])+(orange*prices[1]));
    printf("What method of payment will you like to use?\n");
    payment();
}

int middle () {
        middle:
        printf("Are you maybe interested in other products?\n");
        printf("Enter y for Yes\nEnter n for No\n");
        scanf("%s", &choice);
    if (choice == 'y' || choice == 'n'){
    if (choice == 'y') {
        system("cls");
        main();
    }
    else if (choice == 'n') {
        printf("We hope to see you again!\n");
    }
    }
    else {
        printf("Invalid input\n");
        goto middle;
    }
}

int payment () {
        printf("Enter c to pay on credit\nEnter f to pay in full\nEnter p to make part payment\n");
        scanf("%s", &pay);
        if (pay == 'c'){
        printf("In how many days will you be able to pay the sum?\n");
        scanf("%d", &days);
        printf("We expect our payment within the time frame of %d days\n", days);
        Receipt();
        }
    if (pay == 'f') {
    printf("How would you like to pay?\nEnter p for POS\nEnter c for cash\n");
}
    if (pay == 'p') {
        printf("Please pay us back as soon as possible!\n");
    }   
    middle();
}

int Receipt () { /*this is you chose lemon or oranges from the first functtion, it will print their respective receipts*/
    time_t t;
    time (&t);
    int o=rand()%1000;
        if(fruit == 'o') {
        printf("-----------------------------------------------------------------\n");
        printf("\tAPEX Supermarket\n\tBEVERLY HILLS, HOLLYWOOD\n\t%s\n\tCHECK #%d\n", ctime(&t), o);
        printf("-----------------------------------------------------------------\n");
        printf("Receipt:\n");
        printf("ITEM\tQUANTITY\tPRICE\n");
        printf("ORANGES\t%d\t\t%.2f\n",lemon, lemon*prices[0]);
        printf("-----------------------------------------------------------------\nTOTAL--------------------------$%.2f\n", lemon*prices[0]);
    }
    if(fruit == 'o') {
        printf("-----------------------------------------------------------------\n");
        printf("\tAPEX Supermarket\n\tBEVERLY HILLS, HOLLYWOOD\n\t%s\n\tCHECK #%d\n", ctime(&t), o);
        printf("-----------------------------------------------------------------\n");
        printf("Receipt:\n");
        printf("ITEM\tQUANTITY\tPRICE\n");
        printf("ORANGES\t%d\t\t%.2f\n",orange, orange*prices[1]);
        printf("-----------------------------------------------------------------\nTOTAL--------------------------$%.2f\n", orange*prices[1]);
    }

}

Aucun commentaire:

Enregistrer un commentaire