无法获取输入字符串数据

时间:2017-02-08 19:06:46

标签: c++

我想从用户那里获取输入字符串,以避免使用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 个答案:

答案 0 :(得分:0)

在C ++中,常见的字符串输入惯用法涉及std::stringstd::getline

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

string包含搜索方法,例如find