64,643
社区成员
发帖
与我相关
我的任务
分享
#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;
}
[#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$
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)