need help! problems on strings.. REPLY ASAP

Discussion in 'C++' started by Khiine, Jul 25, 2010.

  1. Khiine

    Khiine New Member

    Joined:
    Jul 13, 2010
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    i can't input a string after entering an integer! why is that?..
     
  2. Khiine

    Khiine New Member

    Joined:
    Jul 13, 2010
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    i crashes.. example when i enter an integer using scanf, then after that, when i try to enter a string using gets it crashes.. NEED HELP ON THIS PLS!
     
  3. Ancient Dragon

    Ancient Dragon New Member

    Joined:
    Jul 23, 2010
    Messages:
    26
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    part time cashier at WalMart
    Location:
    near St Louis, IL, USA
    The reason you can not enter a string after entering an integer is because when you type the digits of the integer then press the Enter key the program leaves the Enter key in the keyboard. You have to remove that '\n' Enter key byte from the keyboard buffer after getting the integer. You did not say whether you are using C or C++ so I can not tell you how to do that. Post some code that shows us how you got the integer.
     
  4. Ancient Dragon

    Ancient Dragon New Member

    Joined:
    Jul 23, 2010
    Messages:
    26
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    part time cashier at WalMart
    Location:
    near St Louis, IL, USA
    >> i try to enter a string using gets
    Oh, I suppose I should have read your post more thoroughly. You are using C language. Then just call getchar() after getting input integer.

    About gets() -- NEVER EVER use it because it will allow you to enter a string larger than the amout of space the input buffer can hold, and that will most likely crash your program. Always use fgets() instead of gets(). But that, too, as a drawback in that it may put the '\n' at the end of the string, which you will want to strip out before using the string for anything.

    An alternative is to call scanf() like this: Assume you want the user to enter 10 characters
    Code:
    char name[11];
    printf("Enter your name (up to 10 characters)\n");
    scanf("%10s", name);
    

    With the above if you enter more than 10 characters scanf() will leave the remaining characters in the keyboard buffer.
     
  5. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    scanf is a really pants way of getting input from the user. Use fgets instead (using stdin as the file descriptor). Then parse out the stuff you want from that string.

    The \n at the end of the fgets result is useful: it tells you whether you got the whole input or not. If you asked for 80 characters and the user entered less than that, you get the input + \n. If they entered more than that you get the first 79 characters of input and a \0, and the remaining input can be retrieved with another call to fgets. Always worth watching out for that cos if you expect the complete input after a call to fgets and they typed too much, this can throw the rest of your program off.
     
  6. LordN3mrod

    LordN3mrod New Member

    Joined:
    Sep 4, 2010
    Messages:
    22
    Likes Received:
    11
    Trophy Points:
    0
    Occupation:
    Software Developer
    Location:
    Yerevan, Armenia
    Since everybody has answered in C, I'll answer in C++.
    If you write
    Code:
    #include <iostream>
    #include <string>
    int main()
    {
       using namespace std;
       int n;
       string s;
       cin >> n;
       cin >> s
    }
    
    then everything is fine, nothing will crash and you will read the next "string" until any spaces or newlines. That is, if the input were

    4
    bubu gaga

    then n would be 4, and s would be "bubu".

    However, if you need to read the whole next line (with a getline) you'll first have to "eat" the rest of the current line. Like this

    Code:
    #include <iostream>
    #include <string>
    int main()
    {
       using namespace std;
       int n;
       string s;
       cin >> n;
       getline(cin, s); //eat the end of the line into s;
       getline(cin, s); //actually read the next line into s;
    }
    
    Best Regards,
    Armen
     
  7. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    if you insist on using scanf then after each scanf add one getchar();
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice