基本的i / o问题 [英] basic i/o question
问题描述
如何让getline(cin,somestring)跳过初始空格,
如果我输入一个"长度应该是1.
为什么我需要在getline之后输入两次以显示我得到的内容?
如何过来?
>
示例:
int main(int argc,char * argv [])
{
string s ;
string :: size_type len;
cout<< 输入一些字符串 ;
getline(cin,s);
len = s.length();
cout<< len;
返回0;
}
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写道:
如何让getline(cin,somestring)跳过初始的空格,
如果我输入一个"长度应该是1.
为什么我需要在getline后输入两次以显示我得到的内容?
如何过来?
例如:
int main(int argc,char * argv [])
{
string s;
string :: size_type len;
cout<< 输入一些字符串 ;
getline(cin,s);
len = s.length();
cout<< len;
返回0;
}
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;
}
我没有必要输入两次才能获得一个显示器。
如果你想忽略这个空格,我会创建一个''trim''函数。
我把它关掉了=nofollowhref =http://www.codeproject.com/vcpp/stl/stdstringtrim.asptarget =_ blank> http://www.codeproject.com/vcpp/stl/stdstringtrim.asp 。
void trim2(字符串& 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());
}
现在你可以做类似的事情了:
#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<< 输入一些字符串:" ;;
std :: getline(std :: cin,s);
trim(s);
len = s.length();
std :: cout<< len<< std :: endl;
返回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,
您的建议确实解决了空白问题。但我确实有两次进入
。我不知道为什么?使用上面的确切代码,我确实要输入两次
。
让我写一下
输入一些字符串:John Ratliff
12
所以它需要两次退货:(
i我再次处理下面的代码:
想到附加截图但看不到任何附件标签
这里。
#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<<"输入一些字符串:" ;;
std :: getline(std :: cin,s);
trim(s);
len = s.length();
std :: cout<< len<< std :: endl;
cout<< s;
返回0;
}
static void trim(std :: string& str){
std :: string :: size_type pos = str.find_last_not_of('''');
if(pos!= std :: string :: ()$
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( ));
}
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());
}
2005年10月19日23:07:45 -0700,vikas ; < 6 ********** @ gmail.com>在comp.lang.c中写了
++:
On 19 Oct 2005 23:07:45 -0700, "vikas" <vi**********@gmail.com> wrote
in comp.lang.c++:
感谢John,
你的建议确实解决了空白问题。但我确实有两次进入。我不知道为什么?使用上面的确切代码我确实要输入两次。
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.
确保你的编译器是最新的,并且拥有所有最新的补丁
或服务包,特别是如果它是微软的。
-
Jack Klein
主页: http://JK-Technology.Com
常见问题解答
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
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
这篇关于基本的i / o问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!