2017-02-08 16 views
-1

我想从用户那里得到输入字符串,以避免空间我用gets_s它输出这样的错误:无法获取输入字符串数据

enter image description here

char str[] = "", symb; 
    int count = 0; 
    cout << "Please enter String: "; 
    gets_s(str); 
    cout << "Which character you want to find: "; 
    symb = getchar(); 

    for (int i = 0; i < strlen(str); i++) 
    { 
     if (str[i]==symb) 
     { 
      count++; 
     } 
    } 

    cout << "Character: " << symb << " found " << count << " times."; 
+1

'gets_s(str);'是未定义的行为。 –

+0

这是使用字符数组的工件。改用'std :: string'。 –

+5

不要使用char []','getchar'和'gets_s'。使用'std :: string','std :: getline'和'std :: cin'。 –

回答

0

在C++中常见的字符串输入成语涉及std::stringstd::getline

string text; 
cout << "Please enter String: "; 
getline(cin, text); 

string包括搜索的方法,如find

相关问题