2020-11-30

How do I fix the input prompt in this C code?

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <memory.h>
#include <ctype.h>
#include <time.h>

struct customer
{
    char name[45];
    char invoice[4];
    char service[2];
    char carNo[16];
    char fee[3];
    bool urgent;
    time_t date;
};

/*********************************************************************************************/
void toFile(char*); // will pass string (char*) to it and it'll write it to record.txt - DONE
void displayMenu(void);      // will display the menu  - DONE
void newInvoice(int invoiceNo);
/*********************************************************************************************/
int main()
{
    char toContinue;
    int invoiceNo =1;
    // format of invoices in txt file
    do
    {
        newInvoice(invoiceNo);
        invoiceNo += 1;
        // asking if user wants to continue to another order or close the program
        printf("Do you want to continue?  (Y or N) :");
        scanf(" %c",&toContinue);
    }
    while (toContinue !='N' && toContinue !='n');
    return 0;
}
/*********************************************************************************************/
void newInvoice(int invoiceNo)
{
    // variable declaration
    char enter,urgentCH;
    int feeINT;
    struct customer *newCus;
    newCus = (struct customer*) malloc ( sizeof(struct customer));
    displayMenu();
    enter = fgetc(stdin);
        sprintf(newCus->invoice, "%d", invoiceNo);
        // customer info being collected
        printf("Customer Name:\n");
        scanf("%[^\n]%*c", newCus->name);
    
        printf("Vehicle Numb :\n");
        scanf("%[^\n]%*c", newCus->carNo);
    
        printf("Service Selection Numb (From Menu):\n");
        scanf("%[^\n]%*c", newCus->service);
    
        printf("Urgent? (Y or N ):\n");
        scanf(" %c",&urgentCH);
        
        if(urgentCH == 'Y' || urgentCH == 'y')
            newCus->urgent = true;
        else
            newCus->urgent = false;
        
        printf("Fee (from menu):");
        scanf("%d",&feeINT);
        sprintf(newCus->fee, "%d", feeINT);
        time(&newCus->date); // system date and time being taken
        
        // writng to file
        toFile("Invoice:\t");
        toFile(newCus->invoice);
        toFile("  customer: ");
        toFile(newCus->name);
        toFile("  vehicle: ");
        toFile(newCus->carNo);
        toFile("  service: ");
        toFile(newCus->service);
        toFile("  type: ");
        if(newCus->urgent)
            toFile("urgent");
        else
            toFile("normal");
        toFile("  fee: ");
        toFile(newCus->fee);
        toFile("  date: ");
        toFile(ctime(&newCus->date));
        // invoice output
        printf("Customer:%s\n  vehicle:%s\n  Service:%s\n  type:%s\n  Fee:%s\n  Date:%s\n",
        newCus->name,newCus->carNo,newCus->service,newCus->urgent?"urgent":"normal",newCus->fee,ctime(&newCus->date));
    free(newCus);
}


/*********************************************************************************************/
void displayMenu()
{
    printf("Numb      Service type                           Time           service fee\n");
    printf("                                         (minutes)      Normal   Urgent\n");
    printf("1       Repair punctured car tyre/piece         10             5        6\n");
    printf("2       Car tyre change /piece                  15           150      160\n");
    printf("3       Mineral Oil Change                      20            80       90\n");
    printf("4       Synthetic Oil Change                    10           130      140\n");
    printf("5       Battery Change                           5           200      210\n");
    printf("6       Head light bulb change /piece            5             6        8\n");
    printf("7       Taillight bulb change /piece             5             6        8\n");
    printf("8       Car Wash                                10            10       12\n");
    printf("------------PRESS ANYTHING TO CONTINUE------------\n");
}
/*********************************************************************************************/
void toFile(char* Str2Write)
{
    // file opened for writing
    FILE *file2write;
    file2write = fopen("file.txt","a");
    // file opened successfully check
    if(file2write == NULL)
        exit(1);
    // writng
    fprintf(file2write,"%s",Str2Write);
    // fprintf(fptr,"\n");
    // closing
    fclose(file2write);
}
/*********************************************************************************************/

My code isn't working properly, there's an issue with the input prompt in the start. The customer name and all other scans print together without giving the option to enter the info needed for the program. I'm a very basic level C programmer and most of the code was written by a friend who I cannot contact at the moment.



from Recent Questions - Stack Overflow https://ift.tt/37h6kvL
https://ift.tt/eA8V8J

No comments:

Post a Comment