question on gets() function

This is a discussion on question on gets() function within the C++ Programming forums, part of the General Programming Boards category; I've never used this function before and am not quite sure how it works. It's basically like a cin statement...correct? ...

  1. #1
    Unregistered
    Guest

    question on gets() function

    I've never used this function before and am not quite sure how it works.
    It's basically like a cin statement...correct?

    I've got the following code in one of my programs...

    cout << "Input the sentence... " << endl;
    gets(pointer1);


    When I run my program, it never allows the user to input any data. How do I get it to allow me to actually input my sentence?

    Thanks in advance for any help,
    Dave (bgbfflochp@aol.com)

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I've never used this function before
    Good, don't start using it.

    gets is very buggy, there is a function that fixes the problems of gets called fgets, here's an example that reads a string from the keyboard:
    Code:
    char array[SIZE];
    fgets ( array, sizeof array, stdin );
    However, C++ offers the cin.getline function which should be used instead of C functions to be consistent. The effect is the same.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    12

    Cool

    I am no expert at this but hopefully this will help..

    Code:
    void main()
    {
       #define buffersize 80
         char buffer[buffersize];
        cout<<"Enter any sentence:"<<endl;
        cin.getline(buffer, buffersize);
        cout<<buffer<<endl;
    }
    or if you can use just plain get()

    Code:
    void main()
    {
       char ch;
       int i = 0;
       #define buffersize 80
       char buffer[buffersize] = {'\0'};
       cout<<"Enter any sentence:"<<endl;
       while((ch = cin.get()) != EOF)
       {
          cout.put(ch);
          buffer[i] = ch;
          i++;
       }
       for(int b=0; b != buffersize; b++)
       { cout<<buffer[b];}
       
    }


    Even the greatest of whale is helpless, in the middle of the dessert.
    So help someone who needs help today then some one else will help U tommorrow when U need it.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >void main()
    Congratulations, your program is undefined starting right here
    void main is wrong, main should return an int and nothing else otherwise the result will be undefined (aka illegal).

    >while((ch = cin.get()) != EOF)
    This won't work, you declared ch as a char yet EOF is a value that can't be held by a char. You also fail to check for boundaries in the loop, so the user could enter any amount of characters and the program will happily write it all to memory that you don't own.
    Code:
    #include <iostream>
    #define buffersize 80
    using namespace std;
    
    int main( void )
    {
      int ch;
      int i = 0;
      char buffer[buffersize] = {'\0'};
      cout<<"Enter any sentence:"<<endl;
      while((ch = cin.get()) != EOF){
        if ( i == buffersize ) {
          buffer[i] = '\0';
          break;
        }
        cout.put(ch);
        buffer[i++] = ch;
      }
      cout<<buffer;   
      return EXIT_SUCCESS;
    }
    -Prelude
    Last edited by Prelude; 03-07-2002 at 10:42 AM.
    My best code is written with the delete key.

  5. #5
    Unregistered
    Guest
    Here's another way to do get keypboard input and allow user to terminate at will:

    cout<<"Enter any sentence. enter a tilde ~ to stop:"<<endl;
    while((ch = cin.get()) != '~'){

    That way to don't have to worry about the EOF business.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >That way to don't have to worry about the EOF business.
    Can you be asolutely sure that a user will not need to enter a tilde as part of the input? I can be sure that EOF won't be

    What type of input is used to end the program is up to the programmer, but consider ease of use as well as whether or not that input could by any stretch of the imagination be valid input. If it does end up being input that the user wants to process at some point, then you've got a broken program.

    -Prelude
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    12
    Hey Prelude..
    What do mean by "void main()" is illegal.. void main is perfectly legal to start with. The main does not have to be "int" type. But if you use "int" as the data type, yes you must return a value. But if you use void then you do not have to.. Try it out and see if it compiles, as far as I know it will work. I have used void main() many times (not to say I am good programmer, far from that) sccessfully.
    And about EOF. You can just use ^z in windows or in UNIX I believe ^d (not sure) which will break it out of the loop.

    P.S. If I am wrong, my sincere apolgy and I would stand corrected.

  8. #8
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    It'll compile - that doesn't make it right, softcoder...

    Prelude's right.

  9. #9
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    What do mean by "void main()" is illegal
    Yeah its not right, See here
    Mind you all my course material shows main declared as void(makes me wonder what else they haven't updated). So its no wonder people still use it, I did until I came here.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  10. #10
    Unregistered
    Guest
    Prelude:

    I look forward to reading your posts, even when I don't agree with everything you say. Your knowledge base obviously exceeds mine.

    However, I feel user friendly code takes precedence to flexibility of code. Unfortunately definition/implementation of EOF is system dependent (I have read that it is Control + Z in DOS and Control + D in UNIX). The user will therefore need to know what type of system they have in order to terminate input in the code using:

    while((ch = cin.get()) != EOF)

    At least with a very seldom used character and an upfront statement to user about the restrictions to it's use the user will know what to do. But in the end it is a matter of personal programmer preference.

    I also note a bit of inconsistency in that while you say this in your discussion:

    >while((ch = cin.get()) != EOF)
    This won't work, you declared ch as a char yet EOF is a value that can't be held by a char.

    you use the exact same line:

    while((ch = cin.get()) != EOF)

    in your code. Not that I have never contadicted myself. Au contrair....I've done it more often than I care to mention, and it is one of the reasons I post under unregistered.

  11. #11
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    >while((ch = cin.get()) != EOF)
    This won't work, you declared ch as a char yet EOF is a value that can't be held by a char.

    you use the exact same line:

    while((ch = cin.get()) != EOF)

    in your code.
    I think you'll find prelude declared ch as int which can hold EOF
    int main( void )
    {
    int ch;
    int i = 0;
    char buffer[buffersize] = {'\0'};
    cout<<"Enter any sentence:"<<endl;
    while((ch = cin.get()) != EOF){
    if ( i == buffersize ) {
    buffer[i] = '\0';
    break;
    }
    cout.put(ch);
    buffer[i++] = ch;
    }
    cout<<buffer;
    return EXIT_SUCCESS;
    }
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  12. #12
    Unregistered
    Guest
    Yup, C_Coder is correct. Prelude did declare ch to be an int. My apologies. Eventhough I may hide my "identity" behind unregistered, I admit my mistakes when I see them or when they are pointed out to me. Again, Prelude, my apologies for my error.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. CreateThread() function question
    By chiefmonkey in forum C++ Programming
    Replies: 5
    Last Post: 05-15-2009, 07:52 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 08:10 AM
  4. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 07:55 AM
  5. Replies: 3
    Last Post: 03-04-2005, 01:46 PM

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