Thread: problem with gets() in dev c++ when used on char arrays

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    25

    problem with gets() in dev c++ when used on char arrays

    i have a problem with gets() on char arrays in dev c++. for ex:- if i declare a structure having members -name,add,phone no etc. as char arrays and when i try to give input for them using gets() the compiler skips the line where gets() i/p is written and goes to the next line. what's the problem??

  2. #2

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    So did you post in the C++ forum because it matches the name of the IDE?
    Or did you post in the C++ forum because you're actually programming in C++, but don't know that gets() is an archaic (and dangerous) function.

    Or is gets() a hint that you're actually trying to program in C?
    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.

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    25
    I was programming in c++. so any other option to read strings from keyboard.any simple functions

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Yes, always use a std::string and getline.

    Eg.
    Code:
    string line;
    getline(cin,line);
    When you have a line, then you can parse it however you want.
    Eg.
    Code:
    istringstream i(line);
    int Number;
    i >> Number;
    If you mix and match methods from C and C++, you will come unstuck at some point.

    If you mix input methods in either language (fgets + scanf in C, or >> and getline in C++), you will come unstuck at some point (as well).

    Stick to ONE general purpose approach to reading the input, then perform all the necessary conversion and validation as separate steps. cin >> var; might be nice and convenient as a one-off, but it can be a trap if you get a decent dialog going with the user.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 08-09-2011, 12:41 PM
  2. Problem with char arrays
    By JOCAAN in forum C Programming
    Replies: 12
    Last Post: 01-03-2011, 10:47 AM
  3. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  4. Storing strings in 2d char arrays problem
    By rainmanddw in forum C++ Programming
    Replies: 5
    Last Post: 10-22-2003, 05:41 PM
  5. Problem with chars and char arrays
    By Zagaberoo in forum C++ Programming
    Replies: 3
    Last Post: 09-27-2003, 08:47 AM