Thread: Not Working properly

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    1

    Post Not Working properly

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <conio.h>
    
    int main()
    {
        system("cls");
    
        struct contact
        {
         double tel, mob, pin, age;
         char add[300], fname[10], mname[10], lname[10], email[30];
        }
        c;
        FILE *fp;
        int input_choice, sizeofc = sizeof(c), buffwer = 512;
    
        printf("Contact Manager app.");
        printf("\nAdd Contacts. ");
        //More options to be added.
        printf("Your Choice: ");
        scanf("%d", &input_choice);
        switch(input_choice)
        {
          case 1:
          fp = fopen("contact.txt","wb");
          //Taking input.
          printf("Enter Firstname: "); //firstname
          gets(c.fname);
          printf("Enter Middlename: "); //middlename
          gets(c.mname);
          printf("Enter Lastname: "); //lastname
          gets(c.lname);
          printf("Enter Age: "); //age
          scanf("%f", c.age);
          printf("Enter Address: "); //address
          gets(c.add);
          printf("Enter Telephone no.:"); //telephone
          scanf("%f", c.tel);
          printf("Enter Mobile no.: "); //mobile
          scanf("%f", c.mob);
          printf("Enter Pincode: "); //pincode
          scanf("%f", c.pin);
          printf("Enter Email ID: "); //Email ID
          gets(c.email);
          //Printing in the file.
          
          fputs(fp, c.fname);
          fprintf(fp, "\n");
          fputs(fp, c.mname);
          fprintf(fp, "\n"); 
          fputs(fp, c.lname);
          fprintf(fp, "\n");
          fprintf(fp, "%f", c.age);
          fprintf(fp, "\n");
          fputs(fp, c.add);
          fprintf(fp, "\n");
          fprintf(fp, "%f", c.tel);
          fprintf(fp, "\n");
          fprintf(fp, "%f", c.mob);
          fprintf(fp, "\n");
          fprintf(fp, "%f", c.pin);
          fprintf(fp, "\n");
          fprintf(fp, "%f", c.email); 
          //fwrite(512, sizeofc, 1, fp);  
          fflush(fp);
          fclose(fp);
          break;
    
        }
        
        system("pause");
        return 0;
        }
    This Code Isn't Working Properly

  2. #2
    Registered User
    Join Date
    Apr 2011
    Location
    Bangalore
    Posts
    20
    Quote Originally Posted by Tejas Sanap View Post
    This Code Isn't Working Properly
    Give some more input. What's not working properly. What is the purpose. Is there any error........

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
          printf("Enter Age: "); //age
          scanf("%f", c.age);
          printf("Enter Address: "); //address
          gets(c.add);
          printf("Enter Telephone no.:"); //telephone
          scanf("%f", c.tel);
          printf("Enter Mobile no.: "); //mobile
          scanf("%f", c.mob);
          printf("Enter Pincode: "); //pincode
          scanf("%f", c.pin);
    I'll ignore that, and let you know that scanf expects to be receiving pointers, non-pointer-values.


    Quzah.
    Last edited by quzah; 05-10-2011 at 02:55 AM.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Well I won't ignore it myself, NEVER use gets() for any reason.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by claudiu View Post
    Well I won't ignore it myself, NEVER use gets() for any reason.
    Oh, yeah? Then, what's the alternative?
    Devoted my life to programming...

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    The alternative is to use fgets() which luckily has a complete prototype that will help you avoid having your strings blown up.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  7. #7
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by claudiu View Post
    The alternative is to use fgets() which luckily has a complete prototype that will help you avoid having your strings blown up.
    What do you mean? Is there a problem with gets?
    Do you have any links for it, by the way? ( I didn't know about that )
    Devoted my life to programming...

  8. #8
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    I think we tell people to not use gets() at least 100 times a day on this forum where have you been ?

    Here is the FAQ Link:

    Cprogramming.com FAQ > Why gets() is bad / Buffer Overflows
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fopen not working properly?
    By darkmagic in forum C Programming
    Replies: 7
    Last Post: 05-15-2011, 11:42 PM
  2. Fscanf not working properly
    By thefinalhero in forum C Programming
    Replies: 9
    Last Post: 10-05-2009, 10:06 AM
  3. tic tac toe not working properly
    By h_howee in forum Game Programming
    Replies: 2
    Last Post: 01-01-2006, 01:59 AM
  4. HWND not working properly
    By loopshot in forum Windows Programming
    Replies: 1
    Last Post: 11-15-2005, 10:25 AM
  5. why isn't my loop working properly?
    By orion- in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:23 PM