无法使用cin读取字符串

时间:2018-01-04 05:06:32

标签: string c++11 cin

对于这段简单的代码:

#include<iostream>
#include<string>
using std::cin;
using std::cout;
using std::string;

bool are_uniq_chars(string const&);

int main(){
string s;
cout << "Enter string: ";
cin >> s;
auto flag = are_uniq_chars(s);

我在gdb调试时得到以下非法值:

Thread 2 hit Breakpoint 1, main () at main.cpp:10
10  string s;
(gdb) n
11  cout << "Enter string: ";
(gdb) n
12  cin >> s;
(gdb) n
Enter string: vinod
13  auto flag = are_uniq_chars(s);
(gdb) p s
$1 = {<std::__1::__basic_string_common<true>> = {<No data fields>}, 
  __r_ = {<std::__1::__libcpp_compressed_pair_imp<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::__rep, std::__1::allocator<char>, 2>> = {<std::__1::allocator<char>> = {<No data fields>}, 
      __first_ = {{__l = {__cap_ = 110429756552714, __size_ = 0, 
            __data_ = 0x0}, __s = {{__size_ = 10 '\n', __lx = 10 '\n'}, 
            __data_ = "vinod", '\000' <repeats 17 times>}, __r = {__words = {
              110429756552714, 0, 0}}}}}, <No data fields>}, 
  static npos = 18446744073709551615}

不确定有什么问题?该代码正在macOS High Sierra 10.13.2上运行。

任何帮助都将不胜感激。

TIA

维诺德

1 个答案:

答案 0 :(得分:0)

猜测此输出是正常的,因为程序在修复另一个错误后开始按预期工作。