Strange problem with std::string

This is a discussion on Strange problem with std::string within the C++ Programming forums, part of the General Programming Boards category; Hi, I am having a very strange problem with std::string. Here's the code: Code: char temp[100]; gets(temp); printf("%s\n", temp); string ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    37

    Question Strange problem with std::string

    Hi,

    I am having a very strange problem with std::string. Here's the code:

    Code:
    char temp[100];
    gets(temp);
    printf("%s\n", temp);
    string t(temp);
    I did "using namespace std;", and now
    Code:
    string t(temp)
    is not working. Any ideas? My input was "lee".

    Thanks.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    How is it not working? Post a full program.

    Also note that gets is a function that you shouldn't use, since it is impossible to prevent buffer overruns with it, and that you can get input directly to a std::string much more safely and conveniently.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    37
    Here is a more complete code (all those relevant, even some I don't think might be relevant):

    Code:
       vector<string> name;
       vector<string> problem;
       vector<int> score;
       int n, p;
    
       scanf("%d", &n);
       for(int c = 0; c < n; c++){
          char temp[255];
          gets(temp);
          gets(temp);
          printf("%s\n", temp);
          string t(temp);
          printf("a\n");
          name.push_back(t);
          printf("a\n");
          score.push_back(0);
          printf("a\n");
       }
    I actually also tried directly cin >> t, but not working...

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Your problem appears to be having two gets calls in the loop. The first time through the loop the first gets extracts an unread newline left by scanf, but next times both calls to gets want input from the user.

    With iostream getting this input looks like this:

    Code:
    ...
    #include <limits>
    int main() {
        ...
        cin >> n;
        cin.ignore(numeric_limits<streamsize>::max(), '\n'); //throw away all unread characters
        for (...) {
            string t;
            getline(cin, t);
            ...
        }
    }
    Last edited by anon; 10-03-2009 at 06:26 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    37
    Somehow Dev-C++ isn't having <limits>... What can I do? Can anyone send me the .h and the .o?
    Last edited by jasperleeabc; 10-03-2009 at 07:03 AM.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Upgrade to an IDE that has seen development in the last 10 years.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    37
    Dev-C++ is convenient (and used in OI here in Hong Kong, so I better not change). Does anyone have the .h and .o for <limits> (I guess it isn't copyrighted?)?

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I can't believe it doesn't have this header. However, you can also use a large number or
    Code:
    cin.ignore(cin.rdbuf()->in_avail(), '\n');
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    37
    runtime error... somehow, I can't even execute:

    Code:
    string temp("abc");
    I believe this is happening just after an automatic update from Microsoft on my XP last night... Strange...

  10. #10
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    Quote Originally Posted by jasperleeabc View Post
    Dev-C++ is convenient (and used in OI here in Hong Kong, so I better not change). Does anyone have the .h and .o for <limits> (I guess it isn't copyrighted?)?
    ???

    The other MinGW IDE choice is Code::Blocks. I don't see how Dev-C++ is any more *convenient*. Both IDE's work on the same simple premise: text editor + makefile (and it doesn't get much more convenient/simpler than that).

    But seriously, heed the advice given, and make the switch.
    goto( comeFrom() );

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange problem
    By G4B3 in forum C Programming
    Replies: 6
    Last Post: 05-14-2008, 02:07 PM
  2. Strange problem with classes in header files
    By samGwilliam in forum C++ Programming
    Replies: 2
    Last Post: 02-29-2008, 03:55 AM
  3. Extremely strange "delete" problem
    By dr_jaymahdi in forum C++ Programming
    Replies: 4
    Last Post: 10-21-2007, 09:06 PM
  4. Strange problem
    By ~Kyo~ in forum Game Programming
    Replies: 0
    Last Post: 02-14-2006, 09:35 PM

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