basic i/o question

Discussion in 'C++' started by vikas, Oct 20, 2005.

  1. vikas

    vikas Guest

    how can I make getline(cin, somestring) make skip initial white spaces,
    say if i enter " a" length should be 1.
    why do i need to hit enter twice after getline to display what i got?
    how to over come that?

    example:

    int main(int argc, char* argv[])
    {
    string s;
    string::size_type len;

    cout << "enter some string" ;
    getline(cin, s);
    len = s.length();
    cout << len;

    return 0;

    }
     
    vikas, Oct 20, 2005
    #1
    1. Advertising

  2. vikas

    John Ratliff Guest

    vikas wrote:
    > how can I make getline(cin, somestring) make skip initial white spaces,
    > say if i enter " a" length should be 1.
    > why do i need to hit enter twice after getline to display what i got?
    > how to over come that?
    >
    > example:
    >
    > int main(int argc, char* argv[])
    > {
    > string s;
    > string::size_type len;
    >
    > cout << "enter some string" ;
    > getline(cin, s);
    > len = s.length();
    > cout << len;
    >
    > return 0;
    >
    > }
    >


    I don't have to hit enter twice to get a display.

    If you want to ignore the whitespace, I would create a 'trim' function.
    I took this one off http://www.codeproject.com/vcpp/stl/stdstringtrim.asp.

    void trim2(string& str)
    {
    string::size_type pos = str.find_last_not_of(' ');
    if(pos != string::npos) {
    str.erase(pos + 1);
    pos = str.find_first_not_of(' ');
    if(pos != string::npos) str.erase(0, pos);
    }
    else str.erase(str.begin(), str.end());
    }

    Now you can do something like:

    #include <iostream>

    static void trim(std::string &str) {
    std::string::size_type pos = str.find_last_not_of(' ');

    if (pos != std::string::npos) {
    str.erase(pos + 1);
    pos = str.find_first_not_of(' ');

    if (pos != std::string::npos) {
    str.erase(0, pos);
    }
    } else {
    str.erase(str.begin(), str.end());
    }
    }

    int main(int, char **) {
    std::string s;
    std::string::size_type len;

    std::cout << "enter some string: ";
    std::getline(std::cin, s);
    trim(s);
    len = s.length();
    std::cout << len << std::endl;

    return 0;
    }

    --John Ratliff
     
    John Ratliff, Oct 20, 2005
    #2
    1. Advertising

  3. vikas

    vikas Guest

    Thanks John,
    your suggestion does solve the white space problem. But i do have
    to enter twice. I don't know why? using the exact above code i do have
    to enter twice.

    let me just write it

    enter some string: John Ratliff

    12

    so it does need two returns :(

    i am again coping the code below:
    thought of attaching screenshot but can't see any attachment tab out
    here.

    #include <iostream>
    #include <string>
    #include "str.hpp"
    using namespace std;

    int main(int argc, char* argv[]) {
    std::string s;
    std::string::size_type len;

    std::cout << "enter some string: ";
    std::getline(std::cin, s);
    trim(s);
    len = s.length();
    std::cout << len << std::endl;

    cout << s;

    return 0;

    }

    static void trim(std::string &str) {
    std::string::size_type pos = str.find_last_not_of(' ');

    if (pos != std::string::npos) {
    str.erase(pos + 1);
    pos = str.find_first_not_of(' ');

    if (pos != std::string::npos) {
    str.erase(0, pos);
    }
    } else {
    str.erase(str.begin(), str.end());
    }
    }

    void trim2(string& str)
    {
    string::size_type pos = str.find_last_not_of(' ');
    if(pos != string::npos) {
    str.erase(pos + 1);
    pos = str.find_first_not_of(' ');
    if(pos != string::npos) str.erase(0, pos);
    }
    else str.erase(str.begin(), str.end());

    }
     
    vikas, Oct 20, 2005
    #3
  4. vikas

    Jack Klein Guest

    On 19 Oct 2005 23:07:45 -0700, "vikas" <> wrote
    in comp.lang.c++:

    > Thanks John,
    > your suggestion does solve the white space problem. But i do have
    > to enter twice. I don't know why? using the exact above code i do have
    > to enter twice.


    Make sure your compiler is up-to-date, and has all the latest patches
    or service packs, especially if it is Microsoft's.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Oct 20, 2005
    #4
  5. vikas

    John Ratliff Guest

    vikas wrote:
    > Thanks John,
    > your suggestion does solve the white space problem. But i do have
    > to enter twice. I don't know why? using the exact above code i do have
    > to enter twice.
    >
    > let me just write it
    >
    > enter some string: John Ratliff
    >
    > 12
    >
    > so it does need two returns :(


    Can't think of why that would be. What platform do you use? Windows,
    Unix, Mac?

    The two-return seems like a platform-dependent thing. I tested under
    Windows XP/msys with g++ 3.4.2.

    --John Ratliff
     
    John Ratliff, Oct 20, 2005
    #5
  6. vikas wrote:
    > Thanks John,
    > your suggestion does solve the white space problem. But i do have
    > to enter twice. I don't know why? using the exact above code i do have
    > to enter twice.
    >


    This is a known issue with old microsoft compilers. But we are talking
    really old.

    john
     
    John Harrison, Oct 20, 2005
    #6
  7. vikas wrote:
    >
    > Thanks John,
    > your suggestion does solve the white space problem. But i do have
    > to enter twice. I don't know why? using the exact above code i do have
    > to enter twice.


    Any chance you are using VC++ 6.0?
    If yes: this is a known bug.
    http://www.dinkumware.com/vc_fixes.html

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Oct 20, 2005
    #7
  8. vikas

    vikas Guest

    I am using windows xp. doing development in vc++ 6.0
    --vikas
     
    vikas, Oct 20, 2005
    #8
  9. vikas wrote:
    > I am using windows xp. doing development in vc++ 6.0
    > --vikas
    >


    Right, make sure you have that latest service pack. Early versions of
    this compiler had this problem. Alternatively fix this problem and
    others yourself as described here http://www.dinkumware.com/vc_fixes.html

    john
     
    John Harrison, Oct 20, 2005
    #9
  10. vikas

    vikas Guest

    you are right karl.
    I guess vc++ 6.0 is quite stable and relaible, except for some bugs
    like this.
    correct me if i am wrong.
     
    vikas, Oct 20, 2005
    #10
  11. vikas

    Marcus Kwok Guest

    vikas <> wrote:
    > you are right karl.
    > I guess vc++ 6.0 is quite stable and relaible, except for some bugs
    > like this.
    > correct me if i am wrong.


    Actually, the VC++ 6.0 compiler leaves much to be desired, especially in
    terms of standards compliance. The VC++ .NET 2003 compiler is much
    better in this regard.

    --
    Marcus Kwok
     
    Marcus Kwok, Oct 20, 2005
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stefan Duenser

    Basic shifting question

    Stefan Duenser, Dec 7, 2004, in forum: VHDL
    Replies:
    4
    Views:
    457
    Stefan Duenser
    Dec 8, 2004
  2. Replies:
    1
    Views:
    594
    Eric Smith
    Jul 8, 2005
  3. Engineer
    Replies:
    6
    Views:
    672
    Jeremy Bowers
    May 1, 2005
  4. Replies:
    0
    Views:
    473
  5. Ark Khasin

    A basic question question about volatile use

    Ark Khasin, Jul 31, 2008, in forum: C Programming
    Replies:
    12
    Views:
    373
    Ark Khasin
    Jul 31, 2008
Loading...

Share This Page