Thread: Need help on a very simple script for college.

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    2

    Need help on a very simple script for college.

    Hi, I just started programming yesterday and I was given a bit of homework to do and I'm puzzled as to how I have gone wrong.

    It is a script that is supposed to ask a user to input their name and address and then display it.

    Here's the script:

    Code:
    #include <stdio.h>
    
    
    void getdata(char *, char *, char *);
    void dispdata(char *, char *, char *);
    
    
    int main(void)
    {
        char name[20];
        char addr1[30], addr2[30], addr3[30];
        
        getdata(name, addr1, addr2, addr3);
        
        dispdata(name, addr1, addr3, addr3);
        
        return(0);
        
    }
    
    
    void getdata(char *n, char *a1, char *a2, char *a3)
    {
         printf("Enter Name: ");
         gets(n);
         printf("Enter Address 1: ");
         gets(a1);
         printf("Enter Address 2: ");
         gets(a2);
         printf("Enter Address 3: ");
         gets(a3);
         
    }
    
    
    void dispdata(char *name, char *addr1, char *addr2, char *addr3)
    {
         printf("\nName and Address details:\n\n");
         printf("\t%s\n" name);
         printf("\t%s\n" addr1);
         printf("\t%s\n" addr2);
         printf("\t%s\n" addr3);
    
    
    }

  2. #2
    Registered User
    Join Date
    Aug 2012
    Posts
    41
    Code:
    void getdata(char *, char *, char *);
    void dispdata(char *, char *, char *);
    These two are wrong as your functions take 4 arguments and here you have given only 3.

    Code:
    void dispdata(char *name, char *addr1, char *addr2, char *addr3)
    {
         printf("\nName and Address details:\n\n");
         printf("\t%s\n" name);
         printf("\t%s\n" addr1);
         printf("\t%s\n" addr2);
         printf("\t%s\n" addr3);
    
    
    }
    In last 4 printf statements you missed ',' after ".
    Correct these two then you wont get any errors.

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    2
    Quote Originally Posted by sana.iitkgp View Post
    Code:
    void getdata(char *, char *, char *);
    void dispdata(char *, char *, char *);
    These two are wrong as your functions take 4 arguments and here you have given only 3.

    Code:
    void dispdata(char *name, char *addr1, char *addr2, char *addr3)
    {
         printf("\nName and Address details:\n\n");
         printf("\t%s\n" name);
         printf("\t%s\n" addr1);
         printf("\t%s\n" addr2);
         printf("\t%s\n" addr3);
    
    
    }
    In last 4 printf statements you missed ',' after ".
    Correct these two then you wont get any errors.

    Thank you for your help, everything is fine now.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    What crappy college is teaching you to use gets(), for the love of Pete?

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by rags_to_riches View Post
    What crappy college is teaching you to use gets(), for the love of Pete?
    rags_to_riches has a point..Try using fgets instead

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Here is an example of using fgets
    Code:
    /* fgets example */
    #include <stdio.h>
    
    int main()
    {
       FILE * pFile;
       char mystring [100];
    
       pFile = fopen ("myfile.txt" , "r");
       if (pFile == NULL) perror ("Error opening file");
       else {
         if ( fgets (mystring , 100 , pFile) != NULL )
           puts (mystring);
         fclose (pFile);
       }
       return 0;
    }
    I copy pasty'ed from here fgets - C++ Reference

    And because i have to rationalize myself for saying you not to use gets,the first thing that comes in my mind is this:
    gets() doesn't allow you to specify the length of the buffer to store the string in.

    Also cp'ed this from here gets(), fgets()

  7. #7
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    That's not a replacement for gets(). Did you read the code before you c/p'd?

    You should use
    Code:
    char buffer[100];
    fgets(buffer, 100, stdin);

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    And you can't forget the need to remove the newline left in the buffer.

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by memcpy View Post
    That's not a replacement for gets(). Did you read the code before you c/p'd?

    You should use
    Code:
    char buffer[100];
    fgets(buffer, 100, stdin);
    Yes i read it.The mystring plays the role of the buffer,but the way you put is more clear

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple shell script question.
    By Avanish Giri in forum Linux Programming
    Replies: 8
    Last Post: 04-19-2012, 02:39 PM
  2. [PAID] Very simple C script - Not working?
    By spadez in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 02-12-2009, 08:00 AM
  3. Simple Script causing error
    By CougarElite in forum C++ Programming
    Replies: 9
    Last Post: 12-22-2004, 10:26 PM
  4. Simple Text Parsing.. Long Time Since College
    By chops11 in forum C Programming
    Replies: 4
    Last Post: 10-07-2004, 04:00 PM
  5. help me with this simple script!
    By ryansutton in forum C++ Programming
    Replies: 2
    Last Post: 09-09-2002, 08:42 PM