array search

This function will search the array for allTitles of totalRec elements to see if it contains the given title and if a match is found, then the function should return the boolean value false. Otherwise, it will locate the corresponding price from the allPrices array and pass it back to the function's caller via the function parameter price, and the function should return the boolean value true in this case.

ERROR: no error, but the results are not what I expect. what do I need to fix?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <cstring>
using namespace std;

bool findTitlePrice(string allTitles[], double allPrices[],
                      int totalRec, string title, double & price);


const int MAXSIZE=300;

int main()
{
   string x;
    string titles[]={"Book 1","Book 2"};
    double Prices[]={78.5,66.0};
    int totalRecords = 2;
    string title = "book";
    double price = 0.0;

    findTitlePrice(titles,Prices,2,title,price);
    
  //else {cout << "We probably dont have that book";}
system("pause");
return 0;
}
// function definition
bool findTitlePrice(string AllTitles[], double AllPrices[],
                      int TotalRec, string Title, double & Price)
{
   string title;  
   cout << endl;                 
   cout<< "Please enter title of the book you are searching for : ";
   cin >> title; 
   for(int i =0; i < TotalRec; ++i)
   {
      if (title == AllTitles[i])
      return true;
   }
  }//listRecords.cpp
If findTitlePrice doesn't find a match what does it return?
Hmm, in addition the function seems to be doing the opposite of what you indicated. It returns true if the specified title is found. That's fine of course but your original problem description was backwards. I don't understand the proposed implementation though. If the title cannot be found, how could the program determine a corresponding price? I don't get it.
Take a look at this for reasons why it would be better to use sequence containers as arrays.
http://cplusplus.com/forum/articles/20881/

Take a look at this for ideas on how to make your program a bit more object oriented (if you are up to that challenge). The larger example contains some more ideas on using functors, algorithms, and sequence containers with user defined classes.
http://cplusplus.com/forum/articles/10879/
Topic archived. No new replies allowed.