Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > basic i/o question

Reply
Thread Tools

basic i/o question

 
 
vikas
Guest
Posts: n/a
 
      10-20-2005
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;

}

 
Reply With Quote
 
 
 
 
John Ratliff
Guest
Posts: n/a
 
      10-20-2005
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
 
Reply With Quote
 
 
 
 
vikas
Guest
Posts: n/a
 
      10-20-2005
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());

}

 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      10-20-2005
On 19 Oct 2005 23:07:45 -0700, "vikas" <(E-Mail Removed)> 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/~a...FAQ-acllc.html
 
Reply With Quote
 
John Ratliff
Guest
Posts: n/a
 
      10-20-2005
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
 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      10-20-2005
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
 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      10-20-2005
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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
vikas
Guest
Posts: n/a
 
      10-20-2005
I am using windows xp. doing development in vc++ 6.0
--vikas

 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      10-20-2005
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
 
Reply With Quote
 
vikas
Guest
Posts: n/a
 
      10-20-2005
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
TurboTax Basic vs. Taxcut Basic? Sharp Dressed Man Computer Support 1 01-12-2009 12:52 PM
What is the difference between Visual Basic.NET and Visual Basic 6? Jimmy Dean Computer Support 3 07-25-2005 07:05 AM
Re: Python interpreter in Basic or a Python-2-Basic translator. rrr@ronadam.com Python 0 05-02-2005 01:48 PM
Python interpreter in Basic or a Python-2-Basic translator. Engineer Python 6 05-01-2005 10:16 PM
Upgrading Microsoft Visual Basic 6.0 to Microsoft Visual Basic .NET Jaime MCSD 2 09-20-2003 05:16 AM



Advertisments