Thread: gets

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    36

    gets

    I am trying to get an array input using gets, but it is not even showing up when I compile. Thre program just skips over that part and goes from the prompt to the cout. I dont get any error messages. Can someone please tell me whatr I am doing wrong?

    Code:
    void zero(){
            char h[6]={"honda"}, t[7]={"toyota"}, f[5]={"ford"}, m[9]={"mercedes"}, *p, symbol='\0';
            if (insertcount<4){
            cout <<"Please enter the car info \n";
            gets ( originalarray [insertcount]);
            fflush (stdin);
            cout << "orig array "<< originalarray [insertcount]<<endl;
            int e=strlen(originalarray[insertcount]);
            for (int r=0;r<e;r++){
                secondarray[insertcount][r]=originalarray[insertcount][r];
            }
    I am using these libraries:

    #include <cstdio>
    #include <iostream>
    #include <cstring>
    #include <cctype>

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    1. fflush(stdin) is wrong.
    2. gets() is wrong.


    Besides those two points, gets() could be reading a '\n' in the input buffer as the first char, and hence returning an empty string.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    36
    what do you mean, wrong? this is exactly the way i was told to do it, and the way it is in my book.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    If you wish to write safe, portable code, then you were told wrong, and your book is wrong. If you want to write code that only works on one particular system and only under certain conditions, you can use those functions in that manner. Some details:

    1. fflush() was designed to flush the contents of a file buffer to the actual file. Therefore, it was designed to only work on output files, not input files. If you use it on an input file, the result is considered undefined. That means any compiler is allowed to design it to do anything it wants. Some compilers give it the meaning that it will eat up all chars in the input buffer until it reaches '\n'. If you try that on compilers that support it, things might work, but your code becomes unportable. That means you can't move it around from one compiler to another and expect it to work unless that other compiler supports it. It won't necessarily even warn you if it doesn't support it. It might just manifest itself as a bug somewhere with some strange activity.
    2. On the subject of gets(), gets() is unsafe. It allows the user to input more chars into the array than the array can support. This means that at best, your program trashes memory that you own, possibly causing some bizarre behavior. If a person is slightly more malicious, they are able to actually take over a program. This becomes a security issue, especially if you use gets()-type functions or scenarios that allow this type of action in a server/client environment, since you then allow other computers to take over or crash remote computers. The alternative to gets() is fgets(). It takes two extra arguments. The second argument is the size of the buffer, and the third argument is the file to read from. To read from the same source as gets(), specify stdin as the file to read from.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    BTW, it's good to be familiar with those functions, since you'll probably see them in other code, but they are C functions. If you're writing a C++ program, you should use the C++ counter-parts: cin or getline()... as well as cin.ignore()...

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    36
    I can't believe how bad my teacher is...

    Thanks for explaining everything to me. Not only does he not tell us anything like this, he insists we use a combination of C and C++ in every program. Not helpful...

    Thanks again.

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Some university/college courses don't teach portable C or portable C++. They teach specific methods for specific compilers and systems. For whatever reason, the professors feel comfortable with this. This board assumes that you want information on how to write portable, standard-compliant code so changing compilers later on isn't a traumatic experience going through many errors and realize the methods chosen don't exactly work across the board.

    If your professor's goal is to help you get familiar with C functions, it's not entirely a bad thing. I don't fully object to them teaching how various things work, as long as they explain what portable, standard-compliant code really is, and you wind up a better programmer as a result.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > I can't believe how bad my teacher is...
    We can

    We see it all the time on various forums, people posting all sorts of crud from their "teacher". There's an awful lot of pulp out there, so you're wise to seek information from as many sources as possible.
    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.

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by veronicak5678 View Post
    I am using these libraries:

    #include <cstdio>
    #include <iostream>
    #include <cstring>
    #include <cctype>
    Those aren't libraries, they're just header files. The libraries your code uses only come into play in the linking stage of your program's build.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed