Home > other >  Project for my C coding class PETS SPA my question: how to add input from user for phone number
Project for my C coding class PETS SPA my question: how to add input from user for phone number

Time:11-22


#include <stdio.h>

//defined structure
struct option {
    char *string;
    float price;
};

float ask(const char *prompt, size_t n, const struct option options[n]) {
    for(;;) {
        printf("%s\n\n", prompt);
        for(int i = 0; i < n; i  ) {
            printf("[%d] %s - $%.2f\n"
                "-----------------------------------------------------\n",
                i   1,
                options[i].string,
                options[i].price
            );
        }
        printf("\n");
        int o;
        if(scanf("%d", &o) != 1) {
            printf("Option could not be read.\n\n");
            while(getchar() != '\n');
            continue;
        }
        printf("\n");
        for(int i = 0; i < n; i  ) {
            if(o == i   1)
                return options[i].price;
        }
        printf("Option %d was not valid.\n", o);
    }
}

int main(void) {
    for(;;) {
        float service = ask(
            "-------------Welcome-------------\n"
            "**********M.A.C PETS SPA*********\n"
            "     choose from our Specials\n"
            "---------------------------------",
            3,
            (const struct option []) {
                { "CLEAN UP Special includes General shower and haircut", 20 },
                { "THE WORKS Special includes General shower, haircut, ear cleaning, and nail trim", 30
                },
                { "FULL GROOM Special includes Breed appropriate shower, specific haircut, nail trim, ear cleaning, bandana and cologne", 40 }
            }
        );

        float size = ask(
            "What size is your dog?",
            3,
            (const struct option []) {
                { "Small", 0 },
                { "Medium", 5 },
                { "Large", 15 },
            }
        );
        int numPhone;
        char petName;
        printf("what is the name of your pet?\n");
        scanf("%s", &petName);
        printf("what is your phone number so you can pick up your friend later:\n");
        scanf("%d", &numPhone);
        printf("\n \t\t    \22******************************\22    \n");
        printf(" \t\t   **********************Receipt***************\n");
        printf("\t\t\tDogs name: %s \n");
        printf("\t\t\tOwners Number: %d \n");
        printf("Total Price including extra charge for the size is = $%.2f\n\n", service   size);
    }
}


This is the code, what im having trouble in is the chunck of code that is the receipt, i still have to clean it up but i just want to get my desired answer.

I am expecting the receipt to show the owners phone number and their pets name from scanf in the previous lines, i thought it would work fine since its a simple scanf but its not working. I would appriciate the help and thanks!!!!

CodePudding user response:

You need to allocate an array of char for petName large enough to hold the expected answer, eg: char petName[64]

Your printf statements do not specify which variable to print out. They should be like this:

printf("\t\t\tDogs name: %s\n", petName);

printf("\t\t\tOwners Number: %n\n", numPhone);
  • Related