basic i/o question

V

vikas

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;

}
 
J

John Ratliff

vikas said:
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
 
V

vikas

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());

}
 
J

Jack Klein

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.
 
J

John Ratliff

vikas said:
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
 
J

John Harrison

vikas said:
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
 
V

vikas

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.
 
M

Marcus Kwok

vikas said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top