2

I'd like to show any character of a string separately.

I am using the library and the Idea is:

    for(int i=0; i< input.size() ; i++)
        cout<< input[i]<<endl;
    } 

The error is that "input.size()" is not an int, so I can't use the for sentence.

    #include <string>
    int main(){
    string input;
    for(int i=0; i< input.size() ; i++)
    cout<< input[i]<<endl;
    } 

I except that it works, but I get an error:

main.cpp:9:15: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::__cxx11::basic_string<char>::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
    9 |  for(int i=0;i< recibir.size();i++) cout<<"hola"<<endl;
Rajan Sharma
  • 2,211
  • 3
  • 21
  • 33
jpflow
  • 53
  • 6
  • 2
    Change `for(int i=0; i< input.size() ; i++)` to `for(size_t i=0; i< input.size() ; i++)` – πάντα ῥεῖ Jun 08 '19 at 15:06
  • 4
    It's not a error. It's a warning (that doesn't prevent your code from compiling). – HolyBlackCat Jun 08 '19 at 15:07
  • Also, see this answer for an in depth explanation: https://stackoverflow.com/questions/5416414/signed-unsigned-comparisons/5416498#5416498 – Chad Jun 08 '19 at 15:11
  • Hi, the answer of πάντα ῥεῖ worked for me. , I don't find the button to mark "solved ". Thank you – jpflow Jun 08 '19 at 15:12
  • @jpflow "*I don't find the button to mark solved*" - that is because no answer was posted, and you can't mark comments as accepted. Also, the real solution is to use `std::string::size_type` which may or may not be `std::size_t` depending on implementation. Or better, use iterators instead: `for(auto iter = input.begin(); iter != input.end(); ++iter) { cout << *iter << endl; }` or a range-based `for` loop: `for(char ch : input) { cout << ch << endl; }` – Remy Lebeau Jun 08 '19 at 15:28
  • This doesn't address the question, but you really don't need the extra stuff that `std::endl` does. `'\n'` ends a line. – Pete Becker Jun 08 '19 at 17:05

0 Answers0