Thread: Urgent Help With C

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    2

    Urgent Help With C

    Hi everyone im new to C and need some help with a project ive been sat by my tutor. i need to use: A menu, Structures, File handling and Dynamic memory allocation to store the name, address, telephone number and email of x amount of people. this is what i have written so far and it isnt working. Any ideas why?




    Code:
     
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include<string.h>
    
    
    int main (void)
    {
    struct database
    {
    char fname[20],lname[20],email[35], address[100], tel_number[15];
    };
    unsigned i,s;
    FILE *datafile;
    struct database input; /*pointer declared*/
    printf("How many people? ");
    scanf("%u",&s);
    if(((struct database *)malloc(s * sizeof(struct database)))==NULL)
     {
     printf("Cannot allocate %u bytes ",s);
     getch();
     return 1;
     }
    
    
    for(i=0;i<s;i++)
     {
     // open the file in write mode
      datafile = fopen ("data","w");
    
    
       if (datafile == NULL)
    
    
         {
          fprintf(stderr, "\nError Writing Data\n\n");
          exit (1);
         }
    
    
          // get details
          printf("\nFirst Name: ");
          gets(input.fname);
          printf("Last Name: ");
          gets (input.lname);
          printf("Telephone Number: +44");
          gets(input.tel_number);
          printf("Email Address: ");
          gets (input.email);
          printf("Home Address: ");
          gets(input.address);
    
    
      // write data in file
      fwrite (&input, sizeof(struct database), 1, datafile);
     }
    
    
    for(i=0;i<s;i++)
    {
      printf ("\nName = %s %s Telephone = +44%s Email Address = %s Home Address = %s",
              input.fname, input.lname,  input.tel_number, input.email, input.address);
    }
    getch();
    return 0;
    }/
    Last edited by JJ57663; 04-26-2019 at 05:42 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,087
    Quite a few things to change:
    • Move the struct database declaration to before the main function definition.
    • I would rename struct database to struct person as it is more a record of a person than a database in itself.
    • Indent your code properly, i.e., pick a fixed number of spaces (typically 2, 4, or 8; I suggest 4) or a tab as a level of indentation, and consistently apply it.
    • Use descriptive variable names, e.g., s is a poor name for "how many people". Better examples include people_num or people_count.
    • You have a comment that says "pointer declared", but you didn't actually declare a pointer.
    • Check the return value of scanf.
    • Be aware that your use of scanf leaves the newline from entering the input in the input buffer where it might be read by a subsequent call to fgets. One way to avoid this is to use fgets with sscanf instead of scanf.
    • You need to assign the return value of malloc to a pointer. Also, there's no need to cast the return value of malloc.
    • You shouldn't be calling fopen in a loop. You should call it before the loop.
    • Since you're using fwrite, open the file with "wb" as the mode instead of "w".
    • If fopen was successful, remember to call fclose at some point.
    • Never use the gets function. The fgets function is an alternative, but be aware that if there's space to store the newline character from entering the input, it will store that newline character. Remember to check the return value of fgets.
    • Your attempt to print the records seems to be printing the same record (i.e, the last one) over and over again. Perhaps you want to read from the file that was recently written to (after closing it) in order to check that it was written correctly? If not, you apparently will have a dynamic array, so you perhaps you could print its content.
    • Remember to free what you malloc.
    • You don't actually need to #include <conio.h> and call getch. They are non-standard. Just run your program from a command prompt window, or from an IDE that will pause the program for you when it ends.
    • Consider breaking up your main function into smaller functions that each does one thing and does it well.
    Last edited by laserlight; 04-26-2019 at 06:13 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Needs urgent help !!!
    By Bilal Zaman in forum C++ Programming
    Replies: 6
    Last Post: 12-26-2012, 06:22 AM
  2. urgent help !
    By Ehsan Sharif in forum C Programming
    Replies: 3
    Last Post: 11-24-2012, 02:58 PM
  3. Urgent help please
    By watsee in forum C Programming
    Replies: 14
    Last Post: 02-09-2010, 12:44 PM
  4. Please help me (urgent)
    By forfor in forum C Programming
    Replies: 3
    Last Post: 12-05-2002, 03:32 PM
  5. in need of urgent HELP!!!!!!!!!
    By Kaz in forum C++ Programming
    Replies: 3
    Last Post: 06-26-2002, 07:23 AM

Tags for this Thread