check out my new C program to capture user input and save it in a txt file please leave ur comments below. Its not finished as yet amma better it soon tho :)

+2 Divulger Michealson · December 20, 2015
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
    FILE *fptr;
    char ans;
    char buff[100];
    char name[200];
    char address[500];
    char telephone[17];
    char sex[23];
    char dateOfBirth[100];
    unsigned char jobDescription[2000000];
    float amountOfMoney,discount,d_price;
    int x,y;
    time_t now = time(0);

for(x = 1; x<=1000;x++){
    printf("\nENTER ALL CUSTOMER INFORMATION BELOW.\n");
    fptr = fopen("C:\Customer's Info.txt", "a+");
    printf("\n\nPlease Enter the Customer's Name: ");
    fflush(stdin);
    scanf(" %[^\n]", &name);
    fprintf(fptr,"Customer Number: %i\n",x);
    fprintf(fptr,"Customer Name: %s\n",name);
    printf("\nPlease Enter Customer's Gender: ");
    fflush(stdin);
    scanf("%[^\n]",&sex);
    fprintf(fptr,"Customer Gender: %s\n",sex);
    printf("\nPlease Enter Customer's Date Of Birth: ");
    fflush(stdin);
    scanf("%[^\n]",&dateOfBirth);
    fprintf(fptr,"Customer D.O.B: %s\n",dateOfBirth);
    printf("\nPlease Enter Customer's Current Address: ");
    fflush(stdin);
    scanf("%[^\n]",&address);
    fprintf(fptr,"Customer Address: %s\n",address);
    printf("\nPlease Enter Customer's Telephone Number: ");
    fflush(stdin);
    scanf("%[^\n]",&telephone);
    fprintf(fptr,"Customer PhoneNumber: %s\n",telephone);
    printf("\nPlease Enter The Customer's Job Description: ");
    fflush(stdin);
    scanf("%[^\n]",jobDescription);
    fprintf(fptr,"Customer Complaint: %s\n",jobDescription);
    printf("\nPlease Enter The Amount The Customer Paid: ");
    fflush(stdin);
    scanf("%f",&amountOfMoney);
    fprintf(fptr,"Money Paid By Customer: $%.2f\n",amountOfMoney);
    printf("\nIs %s Getting Any Discount? ",name);
    fflush(stdin);
    scanf(" %c",&ans);
        if(ans == 'y'|| ans == 'Y'){
            printf("\nEnter the Discount off in percentage (dont use %% just enter the digits): ");
            fflush(stdin);
            scanf("%f",&discount);
            d_price = amountOfMoney - (discount/100.00 * amountOfMoney);
            fprintf(fptr,"Customer Discount: %.2f%% off \nDiscounted Price: $%.2f \n", discount, d_price);
    }else{
        printf("\n");
    };
    strftime(buff,100,"%Y-%m-%d %H:%M:%S.000", localtime(&now));
    fprintf(fptr,"TimeStamp: %s\n\n\n",buff);
    fclose(fptr);

}

    getchar();
    return 0;
}

Post a Reply

Replies

Oldest  Newest  Rating
0 Linguist Llama · December 23, 2015
Additionally, scanf("%[^\n]", ...) probably doesn't do what you want it to do.

This directive can not read empty fields. When a user pressed enter twice (perhaps by accident?) scanf will fail and assign nothing to the destination, hence causing you to rely upon uninitialised data.

In addition, your compiler is probably warning you that you're giving the wrong type of argument. "%[^\n]" tells scanf to read data into the array pointed to by a char *, but the pointer you are giving is (at least, for most of your code) a char (*)[n], where n is the dimension.

This may work for you, but it is technically undefined behaviour. To fix it, you need to realise that if you remove the &, the expression that denotes your array will be converted to a pointer to the first element of your array. In other words, you can remove the & in your array expressions and the warnings will disappear. This explains why scanf("%[^\n]",&telephone); produces a warning, but scanf("%[^\n]",jobDescription); does not.

You should really check the return value of scanf. Something like this, perhaps:

if (scanf("%f", &discount) != 1) {
    puts("ERROR: INVALID ENTRY OR EOF REACHED");
}


2000000 is a little large for an array that's probably unlikely to ever be completely used, isn't it? I know what problem you're trying to solve, but this isn't the way to solve it. You might wish to take a look at the answer I provided on StackOverflow here: http://stackoverflow.com/questions/30271631/dynamically-allocate-user-inputted-string/30272281#30272281
0 Linguist Llama · December 23, 2015
fflush(stdin); is undefined behaviour. See http://c-faq.com/stdio/stdinflush.html for more information and alternatives.
0 Laura Lee · December 21, 2015
Don't use
fflush(stdin);

It results in undefined behaviour. Empty out the streams yourself.

Also refrain from:
scanf

It's better practice to read data as a string and convert it on your own after. Which is why you might have had issues with your streams.
  • 1

C

106,992 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator