Thread: Help of C "Hopefully I will get nice Communication from some Admins"

  1. #16
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I think your definition of "prompting" encompasses two things, and would therefore require two steps:

    (1) Sending output to the user
    (2) Receiving input from the user

    Your original "printf()" took care of #1, so I would leave that in.
    Afterwards, you want to put a command to look for a user input.

    So right after that, you use the "fgets()" function. (it's almost the same declaration as "gets()", but much safer.) This will get a string from the user.

    For the "fgets()", you want to look at the declaration of the function so you know what data to pass to it.

    >> char *fgets(char *s, int size, FILE *stream);

    You pass the address of the string in question to the "get string" function, and it returns the users input to the appropriate array.

    You also have to include the maximum number of the array ("int size" is a limit to the expected characters, so the input buffer doesn't overflow) and the stream (stdin).

  2. #17
    Registered User kitymarine's Avatar
    Join Date
    Jun 2011
    Posts
    18
    Can you verify if I can write the first line to show the name like this:
    gets(name[30]);
    printf("Enter name: ",name);
    scanf("%s",people[i].name);

  3. #18
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    The analog of gets() is puts(), which can sometimes be used in place of printf.

    Instead of guessing how gets and safer_get work, look it up. Your insturctor probably provided some documentation for safer_gets.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #19
    Registered User kitymarine's Avatar
    Join Date
    Jun 2011
    Posts
    18
    He did not give any further notices about safer_gets..Gets and puts I usethem without any explanation about the use or anything in the lecture..this class gets more harder

  5. #20
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by kitymarine View Post
    He did not give any further notices about safer_gets..Gets and puts I usethem without any explanation about the use or anything in the lecture..this class gets more harder
    I don't see any immediate "safer_gets()" references in a quick search. Do you have any specific header files exclusively for your course? (The professors at my old college wrote their own header files for graphical interfaces, so we could plot the values of circuit elements in 'C').

  6. #21
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by kitymarine View Post
    He did not give any further notices about safer_gets..Gets and puts I usethem without any explanation about the use or anything in the lecture..this class gets more harder
    For gets and puts:
    c standard library reference - Google Search
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #22
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Well, regardless of which function you need, you don't quite have the concept yet. You're getting close, though. Want to try to follow through with a more common function? "fgets()". You can apply what you learned tonight to a "safer_gets()" if you ever come across one.

    #1: You want to ask the user for input, let them know it's their turn to act. (printf())
    #2: When the program tells them this, *that* is when it's time to listen. (fgets())

    You have the output part fine.

    Now you just have to write the part that gets the users input. I gave you the declaration of "fgets()" in the last reply, so you have single clear command to use.

    Code:
    char *fgets(char *s, int size, FILE *stream);
    Now go through each argument, one by one, and give the required information.

    "char *s" is a pointer to a string. Put a pointer to the first string in that location (hint: the name of an array without the []'s points to the first location of that array).

    "int size" is the number of characters you expect (30, in this case).

    "FILE *stream" is the stream you're reading from; "stdin", the keyboard, in this case.

    Sorry if I'm being a little indirect here, but I want to make sure you learn - and help show everyone else here how much you learn. (You're doing great, by the way!)

  8. #23
    Registered User kitymarine's Avatar
    Join Date
    Jun 2011
    Posts
    18
    Thank you Matticus ,I will work more on the code and I will get back to you guys..you really helped to clear up some points I appreciate it!

  9. #24
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by kitymarine View Post
    I dont know why the admin closed my thread but it is a polite way of communication..Secondly Instead of jumping and accuse me of dumping my assignment I was out of connection yesterday and I could not post my code...For better communication I hope from the Oe Called Salem to be more formal until he will know what people are posting..and this is my tag
    First off Salem is one of the fairest moderators I've worked with. If you have specific questions ask them... post code snippets when possible...


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #define nmax 10
    
    
    typedef struct Person    <-- typedefs go above main
      { char name[30];
        char adresse[30];
        char city[30];
        char state[30];
        int zip;
        int age;
        char gender; };  
    
    int main(void)   <--- this is the correct form
      {
        struct Person people[nmax];
        int i;
    
        for(i=0;i<nmax;i++)
          { printf("Enter the information for the person number : %d\n ",i+1);
            printf("Enter name: ");
            scanf("%29s",people[i].name);
            printf("Enter street adresse : ");     <-- that is a prompt so stop
            scanf("%29s",people[i].adresse);           fussing over it.
            printf("Enter city : ");
            scanf("%29s",people[i].city);
            printf("Enter state : ");
            scanf("%29s",people[i].state);      <-- string size limits prevent
            printf("Enter zip code : ");            buffer overflows
            scanf("%d",&people[i].zip);
            printf("Enter age : ");
            scanf("%d",&people[i].age);
            printf("Enter gender(M or F) : ");
            scanf("%s",&people[i].gender);
            printf("\n\n"); }
    
    system("PAUSE");
      return 0; }    <-- always return a value

  10. #25
    Registered User kitymarine's Avatar
    Join Date
    Jun 2011
    Posts
    18
    @Common Tater: "Nothing in life is fair or perfect that's why there is human beings and forgiveness" as an answer about Salem..Back to my question...Im tryint to involve the "gets" and the output should prompt after entering consistent numbers

  11. #26
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by kitymarine View Post
    ...Im tryint to involve the "gets" and the output should prompt after entering consistent numbers
    Can you be a little more specific? Show us what you're working with.

  12. #27
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by kitymarine View Post
    @Common Tater: "Nothing in life is fair or perfect that's why there is human beings and forgiveness" as an answer about Salem..Back to my question...Im tryint to involve the "gets" and the output should prompt after entering consistent numbers
    Why would you use gets... it's prone to buffer overflows and is generally frowned upon by C programmers. There are far better ways of doing this... fgets() is one, scanf() is another...

    I don't know what you mean by "prompt after entering consistent numbers"... If you mean it should give you some sort of printout, that's best done with printf().

  13. #28
    Registered User kitymarine's Avatar
    Join Date
    Jun 2011
    Posts
    18
    Matticus try to take a look about the assignment and the output..Try to read the question of the professor I got confused about how many informations of persons he wants me to enter and I need to use the puts so by the end I will get this output from his example:
    The information you entered is:

    Minnie Mouse
    100 Disney Drive
    Orlando, FL 99990
    She is 25 years old.

    Big Bird
    10 Sesame Street
    Funtown, MA 01222
    He is 20 years old.

  14. #29
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Using puts() is not your answer... because of the way you are formatting the text, you're best bet is printf().

    puts() is used primarily for diagnostic printing, during the debug phase of a program. It prints only strings and always adds a newline (\n) at the end, so it's pretty much useless for formatted output like you're showing us.

  15. #30
    Registered User kitymarine's Avatar
    Join Date
    Jun 2011
    Posts
    18
    @CommonTater: Okay Guys ..Let's focus on One Line and try to help to correct it properly..I want to know IF shall use Gets for each line to print whatever needed ..for example how can I make up this line with gets instead of printf andscanf Like I used:"Enter zip code: 99990" and how can I use th puts to get the following output at the end of the code:
    The information you entered is:

    Minnie Mouse
    100 Disney Drive
    Orlando, FL 99990
    She is 25 years old.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 03-31-2009, 04:23 PM
  2. Replies: 46
    Last Post: 08-24-2007, 04:52 PM
  3. Replies: 12
    Last Post: 08-05-2003, 02:16 PM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM