Home > Enterprise >  debugging c code, how to count characters in a string
debugging c code, how to count characters in a string

Time:02-02

Image that show the question with test cases

Hey guys, I'm a beginner who is trying to learn C and I'm trying to do a lab that asks to output the number of times the character appears in the string, with test cases for calrifcation in the picture above. I was trying to do the lab but I came upon a error message that I can not figure out what's the problem is at exacly.

I was trying to ask for an input of the input character and the input string and was trying to make a for loop that goes in every letter of the string input and calculate how many times the input character match with the same character in the string.

this is my code below:

#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;
   }
}

this is the error message below:

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) {
      |                            ^~~~~~~~

CodePudding user response:

You first need only get a single character: use std::cin >>. Then you need to get the entire line of input: use 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
}

That std::ws there skips any leading whitespace. That may not be correct for your assignment. (Is it possible that the character to search is a space or tab?)

I personally like getting prompted, so I would put something like

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

at the top of the program, but it is not necessary.

Now all you need is to count the cs:

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

Also, here is a short lecture on names:

Things like “input value” and “output value” are not very specific. You should prefer more specific names.

In some cases, however, non-specific names are more correct. For these, use common names, like I did for c (character) and s (string). (Other common meta-syntactic names are ch (character) and str (string).) These names instantly signal that the name of the object is not important, but also lets the user know what kind of object it is.

We care less that the output variable is to be output, but what it is. In this case it is a count. Hence its new name: count. When we std::cout the count it is pretty clear that it is part of the output.

Finally, right at the start you should get used to the idea of just typing std:: right at the front of Standard Library objects and not using using namespace std. It does seem a little overboard to do all that typing at first, but soon enough those five characters will just appear on your screen when you think them. Doing so will save you a lot of grief later when you inadvertently name something that also exists in the Standard Library.


Bonus: reading error messages

The compiler is verbose when complaining, barfing sometimes for more scrollback buffer than available in your terminal.

Whenever you get an error, pay close attention to the first one, fix it, and recompile. Take errors one at a time like that.

Your first error is:

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){
      |                  ~~^~~~~~~~~~~~~~~~~~~

Reading it we see that it is complaining that int and long unsigned int have different “signedness” and you are comparing them.

This is an obnoxiousness that you need to deal with when using for loops (because the problem it is complaining about is a valid issue). The easy fix is to just use size_t as your index type, as I did in the example above.

The next error you get is:

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}

Here the compiler is saying it do an == comparison that has a char on the left-hand side and a std::string on the right-hand side. It even draws you a little picture showing the code and what each item is.

You can look back and see that inputVal is indeed a std::string, and a character .at(i) is not a string. This is a hint that you might need to revisit what type of thing inputVal is.

In the code I presented above we changed inputVal to c, complete with the new type of char. This makes more sense for the algorithm, since c only needs to be a single character anyway.

It does get easier. I promise!

  • Related