gets and puts does'nt work!!

This is a discussion on gets and puts does'nt work!! within the C Programming forums, part of the General Programming Boards category; I used gets and puts inside of switch and it doesn't work. someone tell me why Code: switch (cmd){ case ...

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    IR, Iran
    Posts
    103

    Unhappy gets and puts does'nt work!!

    I used gets and puts inside of switch and it doesn't work.
    someone tell me why
    Code:
    	switch (cmd){
    		case '1':
    			printf("\nEnter name:");
    			gets(std[t].name);
    			getch();
    			tchr_mod();
    		break;
    		case '2':
    			printf("\nEnter number:");
    			gets(std[t].std_num);
    			getch();
    			tchr_mod();
    		break;
    		case '3':
    			tchr_mod();
    		break;
    		default:
    			printf("\n\aUnknown Command! Try again:");
    			getcmd(&cmd);
    		break;
    	}
    std is a global variable!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    You're using gets(), that's one problem - see the FAQ.

    You're probably using scanf() as well, which doesn't play well with input functions which are not scanf.

    Personally, use fgets(buff, sizeof buff, stdin ); for ALL input, then deal with the input from the string in memory. Mixing input and conversion just gets messy.
    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.

  3. #3
    Registered User
    Join Date
    May 2008
    Location
    IR, Iran
    Posts
    103
    Quote Originally Posted by Salem View Post
    You're using gets(), that's one problem - see the FAQ.

    You're probably using scanf() as well, which doesn't play well with input functions which are not scanf.

    Personally, use fgets(buff, sizeof buff, stdin ); for ALL input, then deal with the input from the string in memory. Mixing input and conversion just gets messy.
    I didn't understand! can you tell me more?

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Read a line with fgets() and parse it yourself, ie with sscanf() or strtol() etc.

    ie,
    Code:
    char buff[256];
    int n;
    
    fgets(buff, sizeof buff, stdin);
    sscanf(buff, "%d", &n);

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351

    Where you might do
    Code:
    scanf( "%d", &myInt );
    I would do
    Code:
    char buff[BUFSIZ];
    fgets( buff, sizeof buff, stdin );
    sscanf ( buff, "%d", &myInt );
    You might think that's hard work, but wait until you've added error checking
    Code:
    int r, c;
    if ( (r=scanf( "%d", &myInt )) == 1 ) {
      // success
      
      // now get rid of the trailing chars up to the newline
      while ( (c=getchar()) != '\n' && c != EOF ) { }
    } else
    if ( r == 0 ) {
      // it wasn't an integer, an error message
      printf( "That wasn't an integer\n" );
      // now get rid of the trailing chars up to the newline
      while ( (c=getchar()) != '\n' && c != EOF ) { }
    } else {
      // r == EOF
      // Now what?  the user has lost all interest in the program - tidy up and bail out
    }
    Compare with
    Code:
    char buff[BUFSIZ];
    if ( fgets( buff, sizeof buff, stdin ) != NULL ) {
      if ( sscanf ( buff, "%d", &myInt ) == 1 ) {
        // success
      } else {
        // it wasn't an integer, an error message
        printf( "That wasn't an integer\n" );
      }
    } else {
      // Now what?  the user has lost all interest in the program - tidy up and bail out
    }
    Oh, and as zacs7 mentions, strtol() is better than sscanf(), since it detects numeric overflows.
    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.

  6. #6
    Registered User
    Join Date
    May 2008
    Location
    IR, Iran
    Posts
    103
    Another question:
    Why gets and puts work perfectly in other places in my code

  7. #7
    Registered User
    Join Date
    May 2008
    Location
    IR, Iran
    Posts
    103
    Let me attach my whole code.
    I still don't understand. because I've just started to write with C.

    Thanks to help!
    Attached Files Attached Files

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    C was built by, and designed to be used by, some very smart professional programmers. Also, it was one of the first computer languages for general use, and never intended as a "high level" language. Mid to low level language, quite suitable for things that required easy access to the machine's hardware, and as much run speed as possible.

    So gets(), is easy to abuse, since it has nothing to stop someone over-running the memory that was intended for gets() to use.

    Scanf() can just quit it's operation, for a variety of reasons, AND it leaves the newline char still in the keyboard buffer, which can always cause problems for the next scanf() - because it may just read the newline char, and skip anything else, altogether.

    Now that a lot of us non-professional programmers have "adopted" C, we have to be careful of it's pitfalls. Not every part of the language is good to be used, or may safely be used, without extra care.

    For my own little puzzle programs, I still use scanf(), sometimes, but I do know how to use it carefully, and these are for versions where I'm the one making the keyboard input. Gets() I won't use at all, because I've just broken that habit. Using fgets(), is just a good habit to pick up, and use all the time.

    With strings, remember that this is NOT a string. "The white dog was barking".
    THIS is a string: "The white dog was barking'\0'".

    Char's are elevated to string status ONLY when they have an end of string marker placed at the end of them. Some C functions will do this for you automatically - but others will not.

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    > Why gets and puts work perfectly in other places in my code
    The techniques used to build a garden shed can't be used to build a sky-scraper.

    Yes you can build a working program with gets, that's not an issue.

    What you cannot do though is build a safe program with gets. Sure it may be safe enough in your own hands, but when the incompetent (or the malicious) use it, then all sorts of stuff you didn't plan for starts to happen.

    C is one of the easiest languages to use to get a program to run. Whereas any other 'safe' language would fail to compile, or produce a run-time fault when you screw up, C will happily continue to trash memory until it touches something it shouldn't do. Never assume that because you've got it to run that your work is finished. Making it run is just the first step.
    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.

  10. #10
    Registered User
    Join Date
    May 2008
    Location
    IR, Iran
    Posts
    103
    I've just entered to the university. and C is one of my course in university! so i use everything I learned from them and this is my final exam project. amd I need help

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    Unless you show us a bit more of the program, like where cmd is declared and how it changes with your input commands, it's hard to way exactly what is going on.
    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.

  12. #12
    Registered User
    Join Date
    May 2008
    Location
    IR, Iran
    Posts
    103
    I used fgets(buff, sizeof buff, stdin ); but I have the same problem.
    It doesn't work either!

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,749
    I used fgets(buff, sizeof buff, stdin ); but I have the same problem.
    It doesn't work either!
    It works for me, therefore you are a liar, at least until you provide proof that it does not work for you. In other words, tell us how does it not work, and show the smallest and simplest (compilable) program that demonstrates the problem.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    We need to see the part of the code that isn't working, along with what exactly "isn't working" about that code.

    A general statement that a function we use all the time, suddenly "isn't working", is not going to lead us to your problem.

    I'm sure he's not a liar, and that kind of language is insulting, anyway. He just doesn't know how to make the code work.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,749
    I'm sure he's not a liar, and that kind of language is insulting, anyway. He just doesn't know how to make the code work.
    Oh, I'm fairly sure he's (or she's) not a liar, but we need the proof to be certain, since I can provide proof that the statement made is false:
    Code:
    #include <stdio.h>
    
    int main()
    {
        char buff[10];
        fgets(buff, sizeof buff, stdin);
        printf("You entered: &#37;s\n", buff);
        return 0;
    }
    By the way, behzad_shabani, look at the code example above. It demonstrates what I mean by "the smallest and simplest compilable code that demonstrates the problem", although in this case it demonstrates a working example instead of a problem.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 4 1234 LastLast
Popular pages Recent additions subscribe to a feed

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