0
#include <iostream>
#include <vector>
#include <string>
std::string likes(const std::vector<std::string> &names)
{
if (names.size() == 0)
return "no one likes this";
else if (names.size() == 1)
return names[0] + " likes this";
else if (names.size() == 2)
return names[0] + " and " + names[1] + " like this";
else if (names.size() == 3)
return names[0] + ", " + names[1] + " and " + names[2] + " like this";
else if(names.size() > 3)
return names[0] + ", " + names[1] + " and " + (names.size()-2) + " others like this"; /*this line isn't working, i guess the problem is in names.size() */

The last line is causing my application to not run, i have tried every possible way i know to solve this but nothing really helped, can you please help?

tifa
  • 1
  • 1
  • What is teh full and complete error message? If you're using Visual Studio, this is in the "Output" pane, not the "Errors" pane. – Mooing Duck May 21 '21 at 15:35

1 Answers1

0

you can't concatenate a str with an int/float

corrected code:

#include <iostream>
#include <vector>
#include <string>
std::string likes(const std::vector<std::string> &names)
{
if (names.size() == 0)
return "no one likes this";
else if (names.size() == 1)
return names[0] + " likes this";
else if (names.size() == 2)
return names[0] + " and " + names[1] + " like this";
else if (names.size() == 3)
return names[0] + ", " + names[1] + " and " + names[2] + " like this";
else if(names.size() > 3)
return names[0] + ", " + names[1] + " and " + std::to_string(names.size()-2) + " others like this";
Milo Wind
  • 61
  • 4