Thread: The first in a set of data to be inputted is always skipped...

  1. #1
    Registered User
    Join Date
    Apr 2012

    The first in a set of data to be inputted is always skipped...

    Ok so in the fuction service_data() the first input from the user is always skipped (gets(detail.type)) and the same thing happens in client_information() with the first input call (gets(data.firstname)). What is wrong here??

    //*************************************Service Cost*************************************************
    void service_data(void)
          Client detail;
          int hwcost=0;
          char hw;
          int swcost=0;
          char sw;
          int tcost=0;
          int labourcost=0;
          printf("*****************************Service Details********************************\n");
          printf("Please enter whether PC is a laptop or Desktop\n");
          printf("Please enter the manufacturer\n");
          printf("Please enter the PC's model number\n");
          printf("\t\t\t------Service Cost\n");
          printf("Enter the hardware installed if any and separate each type by forward slash'/'\n");
          printf("eg. CPU: Intel Core i7 2.6Ghz/ MOBO: ASUS RAMPAGE Z68LX/ RAM: 2X Corsair Vengance 8GB");
          printf("Please enter the total cost of hardware\n");
          scanf("%d", &hwcost);
          printf("Enter the software installed if any and separate each type by forward slash'/'\n");
          printf("eg. AV: bitdefender Internet Security 2012/ Office tools: Microsoft Office Student/ OS: Windows 8 home premium\n");
          printf("Please enter the total cost of software\n");
          printf("Please enter the labour cost\n");
          tcost= hwcost + swcost + labourcost;
          printf("The total service cost is %d", tcost);
          fprintf(fp,"Record created at %12s\n", __TIME__);
          fprintf(fp,"The requested day was : %s", __DATE__);
          fprintf(fp,"The PC type is : %s",detail.type);  //writes to file
          fprintf(fp,"\nPC manufactorer is: %s",detail.brand);   //writes to file
          fprintf(fp,"\nPC model number is: %d",detail.model_no);   //writes to file
          fprintf(fp,"\nThe client owner is ID: %d", detail.ID);
          fprintf(fp,"Hardware installed in system include: %c\n",hw);
          fprintf(fp,"The hardware cost is: %d\n",hwcost);  //writes to file
          fprintf(fp,"Software installed in system include: %c\n",sw);
          fprintf(fp,"The software cost is: %d\n",swcost);   //writes to file
          fprintf(fp,"The labour cost is: %d\n",labourcost);   //writes to file
          fprintf(fp,"The overall cost is: %d\n", tcost);
          fclose(fp);                              //close file
    //******************************Customer Information******************************
    void client_information(void)
        {                                  // Starting point of the function                 
            Client data;                                                  // declaration of character
            system("cls");                     // Clears the screen
            printf("\n\n\n\t\t************************ Customer Record *******************\n\n");//printf statement
            printf(" Enter client's First Name\n\n");          //printf statement
            gets(data.firstname);                             // gets the input
            printf("Enter client's Last Name\n\n");         // printf statement
            gets(data.surname);                           //gets the input
            printf("Enter client's Phone Number\n\n");        // printf statement
            gets(data.Contact_No);                       //gets input
            printf("Enter client's Address\n\n");             // printf statement
            gets(data.address);                              //gets input
            printf("Enter customer ID Number\n\n");           // printf statement
            gets(data.ID);                         //gets input
            fp1=fopen(customer,"a+");                                 // open file
            fprintf(fp1,"First Name is: %s\n",data.firstname);      // writes to file
            fprintf(fp1,"Last Name is: %s\n",data.surname);       // writes to file
            fprintf(fp1,"Client's Phone Number is:- %s\n",data.Contact_No); // writes to file
            fprintf(fp1,"Address is: %s\n",data.address);       // writes to file
            fprintf(fp1,"ID number is:%d\n",data.ID);         // writes to file
            fclose(fp1);                                            // close file
            system("PAUSE");                                 //Pause the screen
            system("cls");                                        //Clears the screen
    Last edited by Linxs; 04-19-2012 at 10:58 AM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Long Beach, CA
    Don't use gets. Read this: FAQ > Why gets() is bad / Buffer Overflows -

    Replace them all with calls to fgets. Remember, you will have to strip the newline at the end, example here: FAQ > Get a line of text from the user/keyboard (C) -

    The problem is likely due to you mixing calls to scanf and fgets (note, I say fgets because you WILL change all those calls to gets). When you use scanf to read a number, it leaves the newline in the input buffer, i.e. it reads the digits you typed, but leaves the <enter> key that you pressed. fgets reads that and thinks you input an empty line, just an <enter>. It's usually a bad idea to mix input methods (scanf, getchar and fgets).

    Just read every line with fgets, and use sscanf to parse the numbers out of them.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > What is wrong here??
    All sorts of things.

    1. Excessive use of global variables

    2. Excessive use of pointless comments (duh!)
    > printf("\nFILE INVAILD\n"); //printf statement

    3. Using gets() at all -> see Gets - cpwiki

    4. The indentation is a mess -> see Indentation - cpwiki

    5. printf("%s\t\t\n", __DATE__); // Display date
    This displays the date the program was compiled - not the current date.

    6. menu();
    menu() should NOT call menu() recursively. Use a normal loop.
    Actually, there's quite a lot of recursive calling going on. Eventually, such programs eat up all available stack and crash.

    7. Opening files, then prompting for filenames.
    fp = fopen(Services,"r");
    fp1 = fopen(customer,"r");

    8. Mixing input styles.
    You need to know that mixing scanf() with other input methods - particularly those that deal with a whole line at a time, is a problem.
    If you have say "%d" as a conversion and type in say "123\n", then the \n will be left for the next input function. Not an issue if the next input is scanf, but a real PITA if it's gets/fgets.

    That's enough for now...
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making sure only one data type is inputted
    By Kinley Adams in forum C++ Programming
    Replies: 5
    Last Post: 03-16-2012, 09:41 PM
  2. gets() skipped!
    By ncode in forum C Programming
    Replies: 14
    Last Post: 02-06-2012, 05:01 PM
  3. Scanf() skipped
    By new-b in forum C Programming
    Replies: 9
    Last Post: 07-18-2009, 01:34 AM
  4. Why is this statement being skipped?
    By TalonStriker in forum C Programming
    Replies: 22
    Last Post: 11-12-2007, 05:45 AM
  5. Replies: 0
    Last Post: 10-22-2001, 12:02 AM