Thread: Help in strings

  1. #1
    Cryptanalyst
    Join Date
    Sep 2007
    Posts
    52

    Help in strings

    Hello all. Getting to the point, I need some help in string functions. I was making a program to implement my own string functions, without using string.h
    The program turns out proper and correct, but one little flaw remains and that appears in the execution. For this snippet of code -:
    Code:
     cout << "\nEnter 2 strings - ";
    gets(str1);
    fflush(stdin);
    gets(str2);
    fflush(stdin);
    The computer accepts only one string and not the other. Why is this so? Even if I try
    Code:
    cout << "\nEnter 1 string - ";
    gets(str1);
    fflush(stdin);
    cout << "\nEnter 2nd string - ";
    gets(str2);
    fflush(stdin);
    It doesn't work. Will be grateful if you help ASAP.
    Last edited by SVXX; 12-09-2008 at 01:03 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Maybe your implementation of gets() is wrong. Oh wait, why bother implementing the terribly unsafe gets() in the first place? Have you considered designing and implementing a cleaner version of std::string instead?

    By the way, fflush(stdin) results in undefined behaviour. There are other ways to "flush" the input buffer that do not result in undefined behaviour; see the cprogramming.com FAQs.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Cryptanalyst
    Join Date
    Sep 2007
    Posts
    52
    Hmm..I checked that. Then I suppose this is how I would go about it -:
    Code:
    cout << "\nEnter 2 strings - ";
    int ch;
    char buf[BUFSIZ];
    while((ch = cin.get()) != '\n && ch != EOF);
            {
                gets(str1);
                cout.flush();
                gets(str2);
                cout.flush();
            }
    Edit : Ah yes its working now. Thanks a bunch!
    Last edited by SVXX; 12-09-2008 at 01:27 AM.

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    It does? You read the first character, check it and then read the rest of the line with gets(). Won't you miss a character like that? And why do you fflush cout?

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Let me guess - there is a scanf/cin >> to read a number before this function - so your clearing of the input buffer allows the left-over newline that was in the input buffer to be cleared. This would be the 112th time someone has posted about this since I joined the forum.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    And you are still using gets.
    Get rid of it.
    http://cpwiki.sourceforge.net/Gets
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Cryptanalyst
    Join Date
    Sep 2007
    Posts
    52
    Alright..I've been berated too much for gets(). Let it never haunt thy mind again! xD.
    For the alternative, I used something a little simpler. Please do tell me if this method is safe from buffer overflows. And no, I wasn't entering a number before the string or anything, mat. I was making a program implementing my own functions for string operations, without using anything in string.h.

    Code:
    cout << "\nEnter a string - ";
    cin.ignore(); //Ignores the newline
    cin.getline(str1, 200);

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    std::string str;
    std::getline(std::cin, str);
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM