[求助]关于getline()与cin.get()一起循环使用的问题!!!

ly_ai001 2011-04-11 11:05:56
#include<iostream>
#include<string>
using namespace std;
int main()
{

while(1)
{
cout<<"Input a string"<<endl;
string b;
getline(cin,b);
cout<<"Input a char"<<endl;
char a;
a=cin.get();
}

return 0;
}
我想实现循环输入一个字符串,一个字符,一个字符串,一个字符……
但是当我循环第一次后,就会忽略掉getline函数了,直接跳过了,不知道为什么,请各位赐教,不胜感激!!
...全文
95 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ly_ai001 2011-04-12
  • 打赏
  • 举报
回复
1楼先到的所以先给他分了,后面的也很给力,不过不好意思哦不要计较这些分哦。。
pengzhixi 2011-04-11
  • 打赏
  • 举报
回复
istream& getline ( istream& is, string& str );
The delimiter character is delim for the first function version, and '\n' (newline character) for the second. The extraction also stops if the end of file is reached in is or if some other error occurs during the input operation.
If the delimiter is found, it is extracted and discarded, i.e. it is not stored and the next input operation will begin after it.

这意味着你第一次输入一个字符串 回车,然后输入字符回车,第一个回车被丢弃,但是第2次输入的那个回车就没有被丢掉,那么对第2次以及以后每次输入的回车都被当成b的结束条件。所以后面的string都没办法输入成功
pcliuguangtao 2011-04-11
  • 打赏
  • 举报
回复
注意:
getline()可以获得一行输入包含空白字符,知道遇到换行符或其他符号,并把换行符从cin流中移除。
如果在getline之前调用了cin.get()并且输入了回车,这时就要cin.ignore(1," ");吃掉一个
qq120848369 2011-04-11
  • 打赏
  • 举报
回复
#include<iostream>
#include<string>
using namespace std;

int main()
{
while(true)
{
cout<<"Input a string"<<endl;

string b;
getline(cin,b);

cout<<"Input a char"<<endl;

char a;
a=cin.get();
cin.get(); //吃掉回车
}

return 0;
}
huer0625 2011-04-11
  • 打赏
  • 举报
回复
补充说一句:从向上面的例子看,当你输入a(回车)的时候,你实际输入的是两个字符,一个是字符'a',另外一个是换行符'\n'。所以第二次之后遇到getline后就都会直接跳过,因为getline默认是以'\n'作为输入的结束符。
huer0625 2011-04-11
  • 打赏
  • 举报
回复
lz, 因为getline默认是以换行符为结束标志而cin.get()只获取一个字符,而当你如输入一个字符并按回车后,其实你输入的是两个字符。如果想要达到你的效果只需要在a后面多添加一个cin.get()语句即可。为了让你更明白这个原理,我把代码修改如下:
[#include<iostream>
#include<string>
using namespace std;
int main()
{

while(1)
{
cout << "Input a string" << endl;
string b;
getline(cin,b);
cout << "Input a char" << endl;
char a, c;
a = cin.get();
c = cin.get(); // 实际上cin.get();就可以,添加变量c是为用gdb调试。

}

return 0;
}
/code]
编译并运行:
[code=C/C++]huer@ubuntu:~/test$ c++ -g -Wall get.cpp -o get
huer@ubuntu:~/test$ ./get
Input a string
hello
Input a char
a
Input a string
hello
Input a char
a
Input a string
^C
huer@ubuntu:~/test$

从运行的结果上看,已经符合你的要求。接下来使用gdb调试,验证前面所说的理由。
huer@ubuntu:~/test$ gdb ./get
GNU gdb (GDB) 7.1-ubuntu
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i486-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/huer/test/get...done.
(gdb) l
1 #include<iostream>
2 #include<string>
3 using namespace std;
4 int main()
5 {
6
7 while(1)
8 {
9 cout << "Input a string" << endl;
10 string b;
(gdb) l
11 getline(cin,b);
12 cout << "Input a char" << endl;
13 char a, c;
14 a = cin.get();
15 c = cin.get();
16
17 }
18
19 return 0;
20 }
(gdb) b 9
Breakpoint 1 at 0x80488ff: file get.cpp, line 9.
(gdb) r
Starting program: /home/huer/test/get

Breakpoint 1, main () at get.cpp:9
9 cout << "Input a string" << endl;
(gdb) n
Input a string
10 string b;
(gdb) n
11 getline(cin,b);
(gdb) n
hello
12 cout << "Input a char" << endl;
(gdb) print b
$1 = {static npos = 4294967295,
_M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, _M_p = 0x804b044 "hello"}}
(gdb) n
Input a char
14 a = cin.get();
(gdb) n
a
15 c = cin.get();
(gdb) print a
$2 = 97 'a'
(gdb) n
7 while(1)
(gdb) print c
$3 = 10 '\n'
(gdb)

64,643

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧