调试c++代码,如何计算字符串中的字符数

rn0zuynd  于 2023-02-06  发布在  其他
关注(0)|答案(1)|浏览(151)

嘿,伙计们,我是一个初学者谁是试图学习C++,我试图做一个实验,要求输出字符串中出现的次数,与测试用例calrifcation在上面的图片.我试图做实验,但我遇到了一个错误消息,我不能弄清楚是什么问题是在exacly.
我试着要求输入字符和输入字符串,并试着做一个for循环,循环输入字符串的每个字母,并计算输入字符和字符串中相同字符匹配的次数。
这是我的代码如下:

#include <iostream>
#include <iomanip>
using namespace std;

int main() {

   string inputVal;
   string inputWord;
   int outputVal =0 ;
   cin >> inputVal;
   cout << inputVal;
   cin >> inputWord;
   cout << inputWord;

   for (int i=0; i <= inputWord.size(); ++i){
    if (inputWord.at(i) == inputVal) {
        ++outputVal;
    }
   }
   if (outputVal ==1) {
    cout << outputVal << " " << inputVal<<endl;
   }
   else {
    cout << outputVal << " " << inputVal<<"'s" <<endl;
   }
}

这是下面的错误消息:

main.cpp: In function ‘int main()’:
main.cpp:16:20: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::__cxx11::basic_string<char>::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
   16 |    for (int i=0; i <= inputWord.size(); ++i){
      |                  ~~^~~~~~~~~~~~~~~~~~~
main.cpp:17:25: error: no match for ‘operator==’ (operand types are ‘__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type’ {aka ‘char’} and ‘std::string’ {aka ‘std::__cxx11::basic_string<char>’})
   17 |     if (inputWord.at(i) == inputVal) {
      |         ~~~~~~~~~~~~~~~ ^~ ~~~~~~~~
      |                     |      |
      |                     |      std::string {aka std::__cxx11::basic_string<char>}
      |                     __gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type {aka char}
In file included from /usr/include/c++/9/iosfwd:40,
                 from /usr/include/c++/9/ios:38,
                 from /usr/include/c++/9/ostream:38,
                 from /usr/include/c++/9/iostream:39,
                 from main.cpp:1:
/usr/include/c++/9/bits/postypes.h:222:5: note: candidate: ‘template<class _StateT> bool std::operator==(const std::fpos<_StateT>&, const std::fpos<_StateT>&)’
  222 |     operator==(const fpos<_StateT>& __lhs, const fpos<_StateT>& __rhs)
      |     ^~~~~~~~
/usr/include/c++/9/bits/postypes.h:222:5: note:   template argument deduction/substitution failed:
main.cpp:17:28: note:   mismatched types ‘const std::fpos<_StateT>’ and ‘__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type’ {aka ‘char’}
   17 |     if (inputWord.at(i) == inputVal) {
      |                            ^~~~~~~~
In file included from /usr/include/c++/9/bits/stl_algobase.h:64,
                 from /usr/include/c++/9/bits/char_traits.h:39,
                 from /usr/include/c++/9/ios:40,
                 from /usr/include/c++/9/ostream:38,
                 from /usr/include/c++/9/iostream:39,
                 from main.cpp:1:
/usr/include/c++/9/bits/stl_pair.h:448:5: note: candidate: ‘template<class _T1, class _T2> constexpr bool std::operator==(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)’
  448 |     operator==(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
      |     ^~~~~~~~
/usr/include/c++/9/bits/stl_pair.h:448:5: note:   template argument deduction/substitution failed:
main.cpp:17:28: note:   mismatched types ‘const std::pair<_T1, _T2>’ and ‘__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type’ {aka ‘char’}
   17 |     if (inputWord.at(i) == inputVal) {
      |                            ^~~~~~~~
In file included from /usr/include/c++/9/bits/stl_algobase.h:67,
                 from /usr/include/c++/9/bits/char_traits.h:39,
                 from /usr/include/c++/9/ios:40,
                 from /usr/include/c++/9/ostream:38,
                 from /usr/include/c++/9/iostream:39,
                 from main.cpp:1:
/usr/include/c++/9/bits/stl_iterator.h:325:5: note: candidate: ‘template<class _Iterator> bool std::operator==(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)’
  325 |     operator==(const reverse_iterator<_Iterator>& __x,
      |     ^~~~~~~~
/usr/include/c++/9/bits/stl_iterator.h:325:5: note:   template argument deduction/substitution failed:
main.cpp:17:28: note:   mismatched types ‘const std::reverse_iterator<_Iterator>’ and ‘__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type’ {aka ‘char’}
   17 |     if (inputWord.at(i) == inputVal) {
      |                            ^~~~~~~~
In file included from /usr/include/c++/9/bits/stl_algobase.h:67,
                 from /usr/include/c++/9/bits/char_traits.h:39,
                 from /usr/include/c++/9/ios:40,
                 from /usr/include/c++/9/ostream:38,
                 from /usr/include/c++/9/iostream:39,
                 from main.cpp:1:
/usr/include/c++/9/bits/stl_iterator.h:363:5: note: candidate: ‘template<class _IteratorL, class _IteratorR> bool std::operator==(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_IteratorR>&)’
  363 |     operator==(const reverse_iterator<_IteratorL>& __x,
      |     ^~~~~~~~
/usr/include/c++/9/bits/stl_iterator.h:363:5: note:   template argument deduction/substitution failed:
main.cpp:17:28: note:   mismatched types ‘const std::reverse_iterator<_Iterator>’ and ‘__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type’ {aka ‘char’}
   17 |     if (inputWord.at(i) == inputVal) {
      |                            ^~~~~~~~
In file included from /usr/include/c++/9/bits/stl_algobase.h:67,
                 from /usr/include/c++/9/bits/char_traits.h:39,
                 from /usr/include/c++/9/ios:40,
                 from /usr/include/c++/9/ostream:38,
                 from /usr/include/c++/9/iostream:39,
                 from main.cpp:1:
/usr/include/c++/9/bits/stl_iterator.h:1136:5: note: candidate: ‘template<class _IteratorL, class _IteratorR> bool std::operator==(const std::move_iterator<_IteratorL>&, const std::move_iterator<_IteratorR>&)’
 1136 |     operator==(const move_iterator<_IteratorL>& __x,
      |     ^~~~~~~~
/usr/include/c++/9/bits/stl_iterator.h:1136:5: note:   template argument deduction/substitution failed:
main.cpp:17:28: note:   mismatched types ‘const std::move_iterator<_IteratorL>’ and ‘__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type’ {aka ‘char’}
   17 |     if (inputWord.at(i) == inputVal) {
      |                            ^~~~~~~~
In file included from /usr/include/c++/9/bits/stl_algobase.h:67,
                 from /usr/include/c++/9/bits/char_traits.h:39,
                 from /usr/include/c++/9/ios:40,
                 from /usr/include/c++/9/ostream:38,
                 from /usr/include/c++/9/iostream:39,
                 from main.cpp:1:
/usr/include/c++/9/bits/stl_iterator.h:1142:5: note: candidate: ‘template<class _Iterator> bool std::operator==(const std::move_iterator<_IteratorL>&, const std::move_iterator<_IteratorL>&)’
 1142 |     operator==(const move_iterator<_Iterator>& __x,
      |     ^~~~~~~~
/usr/include/c++/9/bits/stl_iterator.h:1142:5: note:   template argument deduction/substitution failed:
main.cpp:17:28: note:   mismatched types ‘const std::move_iterator<_IteratorL>’ and ‘__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type’ {aka ‘char’}
   17 |     if (inputWord.at(i) == inputVal) {
      |                            ^~~~~~~~
In file included from /usr/include/c++/9/string:41,
                 from /usr/include/c++/9/bits/locale_classes.h:40,
                 from /usr/include/c++/9/bits/ios_base.h:41,
                 from /usr/include/c++/9/ios:42,
                 from /usr/include/c++/9/ostream:38,
                 from /usr/include/c++/9/iostream:39,
                 from main.cpp:1:
/usr/include/c++/9/bits/allocator.h:167:5: note: candidate: ‘template<class _T1, class _T2> bool std::operator==(const std::allocator<_CharT>&, const std::allocator<_T2>&)’
  167 |     operator==(const allocator<_T1>&, const allocator<_T2>&)
      |     ^~~~~~~~
/usr/include/c++/9/bits/allocator.h:167:5: note:   template argument deduction/substitution failed:
main.cpp:17:28: note:   mismatched types ‘const std::allocator<_CharT>’ and ‘__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type’ {aka ‘char’}
   17 |     if (inputWord.at(i) == inputVal) {
      |                            ^~~~~~~~
In file included from /usr/include/c++/9/string:55,
                 from /usr/include/c++/9/bits/locale_classes.h:40,
                 from /usr/include/c++/9/bits/ios_base.h:41,
                 from /usr/include/c++/9/ios:42,
                 from /usr/include/c++/9/ostream:38,
                 from /usr/include/c++/9/iostream:39,
                 from main.cpp:1:
/usr/include/c++/9/bits/basic_string.h:6144:5: note: candidate: ‘template<class _CharT, class _Traits, class _Alloc> bool std::operator==(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&)’
 6144 |     operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/9/bits/basic_string.h:6144:5: note:   template argument deduction/substitution failed:
main.cpp:17:28: note:   mismatched types ‘const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’ and ‘__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type’ {aka ‘char’}
   17 |     if (inputWord.at(i) == inputVal) {
      |                            ^~~~~~~~
In file included from /usr/include/c++/9/string:55,
                 from /usr/include/c++/9/bits/locale_classes.h:40,
                 from /usr/include/c++/9/bits/ios_base.h:41,
                 from /usr/include/c++/9/ios:42,
                 from /usr/include/c++/9/ostream:38,
                 from /usr/include/c++/9/iostream:39,
                 from main.cpp:1:
/usr/include/c++/9/bits/basic_string.h:6152:5: note: candidate: ‘template<class _CharT> typename __gnu_cxx::__enable_if<std::__is_char<_Tp>::__value, bool>::__type std::operator==(const std::__cxx11::basic_string<_CharT>&, const std::__cxx11::basic_string<_CharT>&)’
 6152 |     operator==(const basic_string<_CharT>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/9/bits/basic_string.h:6152:5: note:   template argument deduction/substitution failed:
main.cpp:17:28: note:   mismatched types ‘const std::__cxx11::basic_string<_CharT>’ and ‘__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type’ {aka ‘char’}
   17 |     if (inputWord.at(i) == inputVal) {
      |                            ^~~~~~~~
In file included from /usr/include/c++/9/string:55,
                 from /usr/include/c++/9/bits/locale_classes.h:40,
                 from /usr/include/c++/9/bits/ios_base.h:41,
                 from /usr/include/c++/9/ios:42,
                 from /usr/include/c++/9/ostream:38,
                 from /usr/include/c++/9/iostream:39,
                 from main.cpp:1:
/usr/include/c++/9/bits/basic_string.h:6166:5: note: candidate: ‘template<class _CharT, class _Traits, class _Alloc> bool std::operator==(const _CharT*, const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&)’
 6166 |     operator==(const _CharT* __lhs,
      |     ^~~~~~~~
/usr/include/c++/9/bits/basic_string.h:6166:5: note:   template argument deduction/substitution failed:
main.cpp:17:28: note:   mismatched types ‘const _CharT*’ and ‘char’
   17 |     if (inputWord.at(i) == inputVal) {
      |                            ^~~~~~~~
In file included from /usr/include/c++/9/string:55,
                 from /usr/include/c++/9/bits/locale_classes.h:40,
                 from /usr/include/c++/9/bits/ios_base.h:41,
                 from /usr/include/c++/9/ios:42,
                 from /usr/include/c++/9/ostream:38,
                 from /usr/include/c++/9/iostream:39,
                 from main.cpp:1:
/usr/include/c++/9/bits/basic_string.h:6178:5: note: candidate: ‘template<class _CharT, class _Traits, class _Alloc> bool std::operator==(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)’
 6178 |     operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/9/bits/basic_string.h:6178:5: note:   template argument deduction/substitution failed:
main.cpp:17:28: note:   mismatched types ‘const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’ and ‘__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type’ {aka ‘char’}
   17 |     if (inputWord.at(i) == inputVal) {
      |                            ^~~~~~~~
In file included from /usr/include/c++/9/bits/ios_base.h:46,
                 from /usr/include/c++/9/ios:42,
                 from /usr/include/c++/9/ostream:38,
                 from /usr/include/c++/9/iostream:39,
                 from main.cpp:1:
/usr/include/c++/9/system_error:292:3: note: candidate: ‘bool std::operator==(const std::error_code&, const std::error_code&)’
  292 |   operator==(const error_code& __lhs, const error_code& __rhs) noexcept
      |   ^~~~~~~~
/usr/include/c++/9/system_error:292:32: note:   no known conversion for argument 1 from ‘__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type’ {aka ‘char’} to ‘const std::error_code&’
  292 |   operator==(const error_code& __lhs, const error_code& __rhs) noexcept
      |              ~~~~~~~~~~~~~~~~~~^~~~~
/usr/include/c++/9/system_error:297:3: note: candidate: ‘bool std::operator==(const std::error_code&, const std::error_condition&)’
  297 |   operator==(const error_code& __lhs, const error_condition& __rhs) noexcept
      |   ^~~~~~~~
/usr/include/c++/9/system_error:297:32: note:   no known conversion for argument 1 from ‘__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type’ {aka ‘char’} to ‘const std::error_code&’
  297 |   operator==(const error_code& __lhs, const error_condition& __rhs) noexcept
      |              ~~~~~~~~~~~~~~~~~~^~~~~
/usr/include/c++/9/system_error:304:3: note: candidate: ‘bool std::operator==(const std::error_condition&, const std::error_code&)’
  304 |   operator==(const error_condition& __lhs, const error_code& __rhs) noexcept
      |   ^~~~~~~~
/usr/include/c++/9/system_error:304:37: note:   no known conversion for argument 1 from ‘__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type’ {aka ‘char’} to ‘const std::error_condition&’
  304 |   operator==(const error_condition& __lhs, const error_code& __rhs) noexcept
      |              ~~~~~~~~~~~~~~~~~~~~~~~^~~~~
/usr/include/c++/9/system_error:311:3: note: candidate: ‘bool std::operator==(const std::error_condition&, const std::error_condition&)’
  311 |   operator==(const error_condition& __lhs,
      |   ^~~~~~~~
/usr/include/c++/9/system_error:311:37: note:   no known conversion for argument 1 from ‘__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type’ {aka ‘char’} to ‘const std::error_condition&’
  311 |   operator==(const error_condition& __lhs,
      |              ~~~~~~~~~~~~~~~~~~~~~~~^~~~~
In file included from /usr/include/c++/9/bits/locale_facets.h:48,
                 from /usr/include/c++/9/bits/basic_ios.h:37,
                 from /usr/include/c++/9/ios:44,
                 from /usr/include/c++/9/ostream:38,
                 from /usr/include/c++/9/iostream:39,
                 from main.cpp:1:
/usr/include/c++/9/bits/streambuf_iterator.h:208:5: note: candidate: ‘template<class _CharT, class _Traits> bool std::operator==(const std::istreambuf_iterator<_CharT, _Traits>&, const std::istreambuf_iterator<_CharT, _Traits>&)’
  208 |     operator==(const istreambuf_iterator<_CharT, _Traits>& __a,
      |     ^~~~~~~~
/usr/include/c++/9/bits/streambuf_iterator.h:208:5: note:   template argument deduction/substitution failed:
main.cpp:17:28: note:   mismatched types ‘const std::istreambuf_iterator<_CharT, _Traits>’ and ‘__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type’ {aka ‘char’}
   17 |     if (inputWord.at(i) == inputVal) {
      |                            ^~~~~~~~
In file included from /usr/include/c++/9/tuple:39,
                 from /usr/include/c++/9/bits/unique_ptr.h:37,
                 from /usr/include/c++/9/bits/locale_conv.h:41,
                 from /usr/include/c++/9/locale:43,
                 from /usr/include/c++/9/iomanip:43,
                 from main.cpp:2:
/usr/include/c++/9/array:252:5: note: candidate: ‘template<class _Tp, long unsigned int _Nm> bool std::operator==(const std::array<_Tp, _Nm>&, const std::array<_Tp, _Nm>&)’
  252 |     operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
      |     ^~~~~~~~
/usr/include/c++/9/array:252:5: note:   template argument deduction/substitution failed:
main.cpp:17:28: note:   mismatched types ‘const std::array<_Tp, _Nm>’ and ‘__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type’ {aka ‘char’}
   17 |     if (inputWord.at(i) == inputVal) {
      |                            ^~~~~~~~
In file included from /usr/include/c++/9/bits/unique_ptr.h:37,
                 from /usr/include/c++/9/bits/locale_conv.h:41,
                 from /usr/include/c++/9/locale:43,
                 from /usr/include/c++/9/iomanip:43,
                 from main.cpp:2:
/usr/include/c++/9/tuple:1419:5: note: candidate: ‘template<class ... _TElements, class ... _UElements> constexpr bool std::operator==(const std::tuple<_Tps ...>&, const std::tuple<_Elements ...>&)’
 1419 |     operator==(const tuple<_TElements...>& __t,
      |     ^~~~~~~~
/usr/include/c++/9/tuple:1419:5: note:   template argument deduction/substitution failed:
main.cpp:17:28: note:   mismatched types ‘const std::tuple<_Tps ...>’ and ‘__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type’ {aka ‘char’}
   17 |     if (inputWord.at(i) == inputVal) {
      |                            ^~~~~~~~
In file included from /usr/include/c++/9/bits/locale_conv.h:41,
                 from /usr/include/c++/9/locale:43,
                 from /usr/include/c++/9/iomanip:43,
                 from main.cpp:2:
/usr/include/c++/9/bits/unique_ptr.h:715:5: note: candidate: ‘template<class _Tp, class _Dp, class _Up, class _Ep> bool std::operator==(const std::unique_ptr<_Tp, _Dp>&, const std::unique_ptr<_Up, _Ep>&)’
  715 |     operator==(const unique_ptr<_Tp, _Dp>& __x,
      |     ^~~~~~~~
/usr/include/c++/9/bits/unique_ptr.h:715:5: note:   template argument deduction/substitution failed:
main.cpp:17:28: note:   mismatched types ‘const std::unique_ptr<_Tp, _Dp>’ and ‘__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type’ {aka ‘char’}
   17 |     if (inputWord.at(i) == inputVal) {
      |                            ^~~~~~~~
In file included from /usr/include/c++/9/bits/locale_conv.h:41,
                 from /usr/include/c++/9/locale:43,
                 from /usr/include/c++/9/iomanip:43,
                 from main.cpp:2:
/usr/include/c++/9/bits/unique_ptr.h:721:5: note: candidate: ‘template<class _Tp, class _Dp> bool std::operator==(const std::unique_ptr<_Tp, _Dp>&, std::nullptr_t)’
  721 |     operator==(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) noexcept
      |     ^~~~~~~~
/usr/include/c++/9/bits/unique_ptr.h:721:5: note:   template argument deduction/substitution failed:
main.cpp:17:28: note:   mismatched types ‘const std::unique_ptr<_Tp, _Dp>’ and ‘__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type’ {aka ‘char’}
   17 |     if (inputWord.at(i) == inputVal) {
      |                            ^~~~~~~~
In file included from /usr/include/c++/9/bits/locale_conv.h:41,
                 from /usr/include/c++/9/locale:43,
                 from /usr/include/c++/9/iomanip:43,
                 from main.cpp:2:
/usr/include/c++/9/bits/unique_ptr.h:726:5: note: candidate: ‘template<class _Tp, class _Dp> bool std::operator==(std::nullptr_t, const std::unique_ptr<_Tp, _Dp>&)’
  726 |     operator==(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) noexcept
      |     ^~~~~~~~
/usr/include/c++/9/bits/unique_ptr.h:726:5: note:   template argument deduction/substitution failed:
main.cpp:17:28: note:   ‘std::string’ {aka ‘std::__cxx11::basic_string<char>’} is not derived from ‘const std::unique_ptr<_Tp, _Dp>’
   17 |     if (inputWord.at(i) == inputVal) {
      |                            ^~~~~~~~
In file included from /usr/include/c++/9/string:41,
                 from /usr/include/c++/9/bits/locale_classes.h:40,
                 from /usr/include/c++/9/bits/ios_base.h:41,
                 from /usr/include/c++/9/ios:42,
                 from /usr/include/c++/9/ostream:38,
                 from /usr/include/c++/9/iostream:39,
                 from main.cpp:1:
/usr/include/c++/9/bits/allocator.h:155:7: note: candidate: ‘bool std::operator==(const std::allocator<char>&, const std::allocator<char>&)’
  155 |       operator==(const allocator&, const allocator&) _GLIBCXX_NOTHROW
      |       ^~~~~~~~
/usr/include/c++/9/bits/allocator.h:155:18: note:   no known conversion for argument 1 from ‘__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type’ {aka ‘char’} to ‘const std::allocator<char>&’
  155 |       operator==(const allocator&, const allocator&) _GLIBCXX_NOTHROW
      |                  ^~~~~~~~~~~~~~~~
In file included from /usr/include/c++/9/bits/stl_algobase.h:67,
                 from /usr/include/c++/9/bits/char_traits.h:39,
                 from /usr/include/c++/9/ios:40,
                 from /usr/include/c++/9/ostream:38,
                 from /usr/include/c++/9/iostream:39,
                 from main.cpp:1:
/usr/include/c++/9/bits/stl_iterator.h:883:5: note: candidate: ‘template<class _IteratorL, class _IteratorR, class _Container> bool __gnu_cxx::operator==(const __gnu_cxx::__normal_iterator<_IteratorL, _Container>&, const __gnu_cxx::__normal_iterator<_IteratorR, _Container>&)’
  883 |     operator==(const __normal_iterator<_IteratorL, _Container>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/9/bits/stl_iterator.h:883:5: note:   template argument deduction/substitution failed:
main.cpp:17:28: note:   mismatched types ‘const __gnu_cxx::__normal_iterator<_IteratorL, _Container>’ and ‘__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type’ {aka ‘char’}
   17 |     if (inputWord.at(i) == inputVal) {
      |                            ^~~~~~~~
In file included from /usr/include/c++/9/bits/stl_algobase.h:67,
                 from /usr/include/c++/9/bits/char_traits.h:39,
                 from /usr/include/c++/9/ios:40,
                 from /usr/include/c++/9/ostream:38,
                 from /usr/include/c++/9/iostream:39,
                 from main.cpp:1:
/usr/include/c++/9/bits/stl_iterator.h:890:5: note: candidate: ‘template<class _Iterator, class _Container> bool __gnu_cxx::operator==(const __gnu_cxx::__normal_iterator<_Iterator, _Container>&, const __gnu_cxx::__normal_iterator<_Iterator, _Container>&)’
  890 |     operator==(const __normal_iterator<_Iterator, _Container>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/9/bits/stl_iterator.h:890:5: note:   template argument deduction/substitution failed:
main.cpp:17:28: note:   mismatched types ‘const __gnu_cxx::__normal_iterator<_Iterator, _Container>’ and ‘__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type’ {aka ‘char’}
   17 |     if (inputWord.at(i) == inputVal) {
      |                            ^~~~~~~~
In file included from /usr/include/x86_64-linux-gnu/c++/9/bits/c++allocator.h:33,
                 from /usr/include/c++/9/bits/allocator.h:46,
                 from /usr/include/c++/9/string:41,
                 from /usr/include/c++/9/bits/locale_classes.h:40,
                 from /usr/include/c++/9/bits/ios_base.h:41,
                 from /usr/include/c++/9/ios:42,
                 from /usr/include/c++/9/ostream:38,
                 from /usr/include/c++/9/iostream:39,
                 from main.cpp:1:
/usr/include/c++/9/ext/new_allocator.h:166:2: note: candidate: ‘template<class _Up> bool __gnu_cxx::operator==(const __gnu_cxx::new_allocator<char>&, const __gnu_cxx::new_allocator<_Tp>&)’
  166 |  operator==(const new_allocator&, const new_allocator<_Up>&)
      |  ^~~~~~~~
/usr/include/c++/9/ext/new_allocator.h:166:2: note:   template argument deduction/substitution failed:
main.cpp:17:28: note:   ‘std::string’ {aka ‘std::__cxx11::basic_string<char>’} is not derived from ‘const __gnu_cxx::new_allocator<_Tp>’
   17 |     if (inputWord.at(i) == inputVal) {
      |                            ^~~~~~~~
ki1q1bka

ki1q1bka1#

首先只需要得到一个 * 单个字符:* use std::cin >>,然后需要得到 * 整行 * 的输入:使用getline()

#include <iostream>
#include <string>

int main()
{
  char        c;
  std::string s;

  std::cin >> std::ws >> c;  // get the character to count
  getline( std::cin, s );    // get the rest of the text
}

std::ws跳过了所有前导空格。这可能对您的赋值语句不正确。(要搜索的字符可能是空格或制表符吗?)
我个人喜欢得到提示,所以我会这样写

std::cout << "c text? ";

在程序的顶部,但这不是必须的。
现在您需要做的就是计算c的数量:

int count = 0;
  for (size_t n = 0;  n < s.size();  n++)
    if (s[n] == c)
      ...

另外,这里有一个关于名称的简短讲座:
像"输入值"和"输出值"这样的东西不是很具体,你应该更喜欢更具体的名称。
然而,在某些情况下,非特定名称更为正确,对于这些名称,请使用通用名称,就像我对c所做的那样(字符)和s(字符串)。(其他常见的元语法名称是ch(字符)和str(字符串)。)这些名称立即表明对象的名称并不重要,但也让用户知道它是什么类型的对象。
我们并不关心output变量是否是output,而是关心它是什么,在本例中它是一个 * count *,因此它有了新的名字:count。当我们std::coutcount时,很明显它是输出的一部分。
最后,一开始你应该习惯于在标准库对象的前面输入std::,而 * 不 * 使用using namespace std。一开始输入所有的内容 * 确实 * 看起来有点过分。但很快这五个字符就会在你想的时候出现在你的屏幕上。这样做可以在以后您无意中命名了标准库中也存在的内容时省去许多麻烦。

奖金:读取错误消息

编译器是 * 冗长 * 时抱怨,呕吐有时更多的滚动缓冲区比可用的终端。
每当你遇到错误时,要密切注意第一个错误,修正它,然后重新编译。一次处理一个错误。
您的第一个错误是:

main.cpp:16:20: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::__cxx11::basic_string<char>::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
   16 |    for (int i=0; i <= inputWord.size(); ++i){
      |                  ~~^~~~~~~~~~~~~~~~~~~

阅读它,我们看到它在抱怨intlong unsigned int具有不同的"signedness",而您正在比较它们。
当使用for循环时,您需要处理这个令人讨厌的问题(因为它所抱怨的问题是一个有效的问题)。简单的修复方法是只使用size_t作为索引类型,就像我在上面的示例中所做的那样。
您得到的下一个错误是:

main.cpp:17:25: error: no match for ‘operator==’ (operand types are ‘__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type’ {aka ‘char’} and ‘std::string’ {aka ‘std::__cxx11::basic_string<char>’})
   17 |     if (inputWord.at(i) == inputVal) {
      |         ~~~~~~~~~~~~~~~ ^~ ~~~~~~~~
      |                     |      |
      |                     |      std::string {aka std::__cxx11::basic_string<char>}
      |                     __gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type {aka char}

这里编译器说它做了一个==比较,在左边有一个char,在右边有一个std::string,它甚至给你画了一个小图片,显示代码和每一项是什么。
回顾一下,你会发现inputVal确实是std::string,而字符.at(i)不是字符串,这是一个提示,你可能需要重新考虑inputVal是什么类型的东西。
在上面的代码中,我们将inputVal修改为c,并添加了新类型char,这对算法来说更有意义,因为c只需要是一个字符。
会变得更容易的。我保证!

相关问题