can anyone explain the gets() function?

This is a discussion on can anyone explain the gets() function? within the C++ Programming forums, part of the General Programming Boards category; ive included stdio.h and this bit of code doesnt want to work, gets(sTicket.sStreetName[iArrayCount]) im trying to get it to store ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    25

    can anyone explain the gets() function?

    ive included stdio.h

    and this bit of code doesnt want to work,

    gets(sTicket.sStreetName[iArrayCount])

    im trying to get it to store a string.

    any ideas?

  2. #2
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    Well, for starters gets only takes in a series of characters up to a "\n" and stores them in a string, I could be wrong, but it kinda looks to me like you're confusing gets with fgets.

    char string[256];
    gets (string)

    fgets takes a specified number of characters from a stream and stores them in a string.

    something like

    fgets( string, sizeof(string), stdin );

    I think
    Last edited by Azuth; 05-16-2002 at 06:45 AM.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    25
    so if i want to store a string that is liable to have a space, and wont quit until someone presses enter, what shall i use, im not quite sure what you are trying to explain to me. i want to store something in the string, sTicket.sStreetName[iArrayCount], no more than about 50 characters really.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >gets(sTicket.sStreetName[iArrayCount])

    We would need to see how sTicket is declared, but this should probably be:
    gets(sTicket.sStreetName)
    or if sTicket is an array:
    gets(sTicket[iArrayCount].sStreetName)

    However I think I have heard that gets() is dangerous to use, because it has no error checking, so it's best to use fgets():

    Assuming a declaration for sStreetName of:
    char sStreetName[50];
    then either:
    fgets(sTicket.sStreetName, 50, stdin);
    or:
    fgets(sTicket[iArrayCount].sStreetName, 50, stdin);

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Or using c++ style streams, getline().

    cin.getline(sTicket[iArrayCount].sStreetName, 50);

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    63
    cin.getline(sTicket[iArrayCount].sStreetName, 50);
    This code could pose as a problem because when you use getline, it automatically accepts the fact that the variable being used is either a character pointer or a character array, and by the looks of what the code is supposed to do it is not a multi-dimensional array, therefore I would use the following:

    cin.getline(sTicket.sStreetName, 50, '\n');

    The 3rd argument is a default argument which doesn't have to be put in, it is by default '\n'.
    tudehopet uses Borland Compiler 5.5
    and Visual C++ 6.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM
  5. Replies: 4
    Last Post: 11-23-2003, 06:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21