3

Before reading this problem please note that this is a PRACTICE problem for hp codewars (a programming competition), I am not asking the forum about a real problem. My program is supposed to take the following input:

  • A number that represent the number of candies in a jar
  • The number of guesses that the user will be entering in
  • A person's name, followed by a space and then their guess

example:

  • 422: Amount of candies in jar
  • 2: Number of guesses
  • Joe 324: Name of guesser and their guess of how many candies are in the jar
  • Mary 435: Second guesser and guess

The output should be the name of the person who had the closest guess

example:

  • Mary

I am currently coding the function that returns the closest number to the guess. However when I run the code, it gives me the error no match for call '(std::vector<int>) (int) on two lines. The lines that are sending back errors are pointed out in comments in my code.

Here is my code:

vector<int> compare(vector<int> nums, int loopnum, int ans){
  vector<int> buff2;
  for (int i = 0; i<loopnum;i++){
      vector<int>diff;
      int buff = ans - nums.at(i);
      for (int j = 0; j<loopnum; j++){
         diff.push_back(buff);
         for (int k = 0; k<diff.size(); k++){
             if (k == 0){
                buff2.push_back(diff.at(k));
             }
             else{
                 // this line is sending back an error
                 if ((abs(buff2(0))) > abs(diff.at(k))) {
                     buff2.clear();
                     buff2.push_back(diff.at(k));
                 }
                 // this line is also sending back an error
                 else if ((abs(buff2(0))) == abs(diff.at(k))){
                     buff2.push_back(diff.at(k));
                 }
             }
         }
      }
  }
  return buff2;
}

Please help me fix this!

Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

8

buff2(0) should be buff2[0] or buff2.at(0)

Cornstalks
  • 37,137
  • 18
  • 79
  • 144