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 }
search
function makes no sense whatsoever, e.g. if they type in3
then you just want to skip thewhile
loop and go to the output part. You don't need to do any sort of search to determine that3
is between0
and10
– M.MHaving fun as a noob with C++.
Well as a new programmer, I recommend you start your arrays from0
, not1
. Starting your arrays from1
will at some point bite you in the end. – PaulMcKenzie