gets function - can't figure it out

This is a discussion on gets function - can't figure it out within the C Programming forums, part of the General Programming Boards category; #include <stdio.h> int main(int argc, char *argv[]) { int loop; char name_array [20][30]; char course_array [20]; for (loop = 0; ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    1

    Question gets function - can't figure it out

    #include <stdio.h>

    int main(int argc, char *argv[])
    {

    int loop;
    char name_array [20][30];
    char course_array [20];

    for (loop = 0; loop < 4; loop++)
    {
    puts("Enter name");
    gets (name_array[loop]);
    puts("Now enter course");
    scanf("%c", &course_array[loop]);
    }

    }

    The above is a code fragment that doesn't work as I want. The gets function will accept a string on the first iteration but after that the program skips gets and goes directly to the puts and scanf. Why is this? IF the puts and scanf are removed the loop works ok and I can complete the array. What is going on here or am I going crazy.

  2. #2
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    seems okay... maybe in your scanf/gets usage you have some sort of memory overlap which rewrites your counter variable... check your memory bounds, i think multiple subscripted arrays are reverse organized per subscript...
    hasafraggin shizigishin oppashigger...

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    Another newbie falls into the scanf tar-pit.

    Is the world full of lame teachers and books?

    Code:
    for (loop = 0; loop < 4; loop++) {
        puts("Enter name");
        gets (name_array[loop]);
        puts("Now enter course");
        scanf("%c", &course_array[loop]);
        while( getchar != '\n' );  // remove excess input chars
    }
    Basic problem is you're mixing scanf with non-scanf input routines, and that generally causes big problems (scanf just loves leaving trailing newlines all over the place).

    The second problem is using gets for reading a line, but more on that later no doubt.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    a ha! well, uhh, i don't use scanf! most of my input is in another graphics mode so i echo the output myself... good call salem...
    hasafraggin shizigishin oppashigger...

  5. #5
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    You should never use gets because there is no protection offered in that function. It is too easy to overwrite a character array.

    You can use scanf as long as you clear the input buffer. Also in order to protect input using scanf you have to know more details so it's better than gets but requires much more knowledge.

    fgets is good because you can control the size of the input so that it does not overwrite your character array.

  6. #6
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    >You should never use gets because there is no protection offered in that function. It is too easy to overwrite a character array.

    right... that's another reason i use the DIY method... troll-king or salem, are their any other things i should know about using standard library functions instead of my own for generic and specialized keyboard input? i generally put the scan code in the high order byte, and the ascii in the low order byte of a word... any clues? thanks...
    hasafraggin shizigishin oppashigger...

  7. #7
    Registered User
    Join Date
    Sep 2001
    Location
    Australia
    Posts
    212
    Doubleanti Would you be interested in sharing any of these?
    Last edited by kwigibo; 10-11-2001 at 03:59 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM

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