0
votes

Just a little frustrated at the moment. Having fun as a noob with C++. Using g++ compiler. In the search function, I come up with several errors on line 54. Below are the first two, since I know this all has to do with one or two errors that I cannot see at the moment.

ghp1.cpp: In function 'int search(std::string*, int)':
ghp1.cpp:54:22: error: no match for 'operator==' (operand types are 'std::string {aka std::basic_string}' and 'int')

if (casino[area] == area)
                 ^

ghp1.cpp:54:22: note: candidates are:
In file included from /usr/local/lib/gcc48/include/c++/iosfwd:40:0,
from /usr/local/lib/gcc48/include/c++/ios:38,
from /usr/local/lib/gcc48/include/c++/ostream:38,
from /usr/local/lib/gcc48/include/c++/iostream:39,
from ghp1.cpp:7:
/usr/local/lib/gcc48/include/c++/bits/postypes.h:216:5: note: template bool std::operator==(const std::fpos<_StateT>&, const std::fpos<_StateT>&)

operator==(const fpos<_StateT>& __lhs, const fpos<_StateT>& __rhs)
^

/usr/local/lib/gcc48/include/c++/bits/postypes.h:216:5: note: template argument deduction/substitution failed:
ghp1.cpp:54:25: note: 'std::string {aka std::basic_string}' is not derived from 'const std::fpos<_StateT>'

if (casino[area] == area)
                    ^

Below is the code for my program which is supposed to make a search and display the result in a function (outside of main() ).

 1  #include <iostream>
 2  #include <cstring>
 3
 4  using namespace std;
 5
 6  int search (string casino[ ], int area);        //Prototype
 7
 8  int main(void)
 9  {
10  int i=0;
11  string casino[11];                              //Creates array
12
13  casino[1] = "Alpha";                            //Fills array
14  casino[2] = "Bravo";
15  casino[3] = "Charlie";
16  casino[4] = "Delta";
17  casino[5] = "Echo";
18  casino[6] = "Foxtrot";
19  casino[7] = "Golf";
20  casino[8] = "Hotel";
21  casino[9] = "India";
22  casino[10] = "Juno";
23
24  /*Query user for search value*/
25
26  cout<<"     This program will help you find the first ten gaming zones of ";
27  cout<<"a casino."<<endl;
28  cout<<"Pick a number between 1 and 10: "<<endl;
29  cin>>i;
30
31  cout<<"     This program will help you find the first ten gaming zones of ";
32  cout<<"a casino."<<endl;
33  cout<<"Pick a number between 1 and 10: "<<endl;
34  cin>>i;
35
36  /*Call Search Function & Display of Result*/
37
38  search(casino, i);
39
40  cout<<"    *** Good Bye! ***"<<endl;
41
42  return 0;
43  }
44
45
46  int search (string casino[ ], int area)
47  {
48  string result;
49  bool answer;
50  answer=false;
51 
52  while ((area <= 10) && (!answer))
53     {                               //Check if number is in search area.
54     if (casino[area] == area)       //***BAD LINE***//
55         answer=true;
56     else
57         area++;
58     }
59  if (answer)
60     cout<<" That zone is named: "<<casino[area]<<endl;
61  else                                 //Display of incorrect input.
62     cout<<" Nice try smartie pants!"<<endl;
63     cout<<" I said, between 1 and 10."<<endl;
64 
65  return 0;
66  }
2
What do you want that line to do? If area is 3, is 3 equal to Charlie?Alan Stokes
The whole search function makes no sense whatsoever, e.g. if they type in 3 then you just want to skip the while loop and go to the output part. You don't need to do any sort of search to determine that 3 is between 0 and 10M.M
Having fun as a noob with C++. Well as a new programmer, I recommend you start your arrays from 0, not 1. Starting your arrays from 1 will at some point bite you in the end.PaulMcKenzie
Your check for invalid input doesn't handle negative numbers, BTW.Alan Stokes
You are trying to compare a string and an integer. It makes no sense to try to do that. The compiler is telling you that there is no operator provided for a comparison of this type.Paul Rooney

2 Answers

1
votes

Your comparison is between a string and an integer. There is no defined operator overload for this, so the compiler gives an error.

Perhaps you want this

#include <iostream>
#include <cstring>

using namespace std;

void search (string casino[ ], int casinolen, unsigned int area);        //Prototype

int main(int, char**)
{
    unsigned int i=0;
    string casino[10];                              //Creates array

    casino[0] = "Alpha";                            //Fills array
    casino[1] = "Bravo";
    casino[2] = "Charlie";
    casino[3] = "Delta";
    casino[4] = "Echo";
    casino[5] = "Foxtrot";
    casino[6] = "Golf";
    casino[7] = "Hotel";
    casino[8] = "India";
    casino[9] = "Juno";

    /*Query user for search value*/

    cout<<"     This program will help you find the first ten gaming zones of ";
    cout<<"a casino."<< endl;
    cout<<"Pick a number between 1 and 10: "<<endl;
    cin>>i;

    /*Call Search Function & Display of Result*/

    search(casino, 10, i);

    cout << "    *** Good Bye! ***" << endl;

    return 0;
}

void search (string casino[ ], int casinolen, unsigned int area)
{
    if (area > 0 && area <= casinolen)
    {
        cout<<" That zone is named: "<<casino[area-1]<<endl;
    }
    else
    {//Display of incorrect input.
        cout<<" Nice try smartie pants!"<<endl;
        cout<<" I said, between 1 and " << casinolen << "."<<endl;
    }
}

You can input a number between 1 and 10 and it will print the name of the relevant casino.

It passes the length of the array (hardcoded here but could be obtained using sizeof) to the search function to know if the number you have entered is within the bounds of the array. Its shifted by 1 as you want to input 1-10, whereas the array indices are actually 0-9.

Passing the length is an approach commonly used in C. For C++ you would do better use a vector, which know its own length using its size method.

0
votes

The problem is pretty simple actually. The compiler is complaining about not "know" about any operator==(...) overload for comparing string and int:

casino[area] == area  // bad string

casino is an array of strings, so casino[area] is a string while area is an integer. You function search should be expecting a string as second parameter:

int search (string casino[ ], string area)