440,581 Members | 2,007 Online
Need help? Post your question and get tips & solutions from a community of 440,581 IT Pros & Developers. It's quick & easy.

bool findTitlePrice function code help

 P: 22 The Code is as Follows Expand|Select|Wrap|Line Numbers bool findTitlePrice(string allTitles[], double allPrices[], int totalRec, string title, double & price) { for(int i=0; i < totalRec; i++) { // your code here: // for each element of array allTitles, check if it matches // the given title, i.e. if title and allTitles[i] are the same; // if yes, fetch the corresponding price from // the array allPrices and exit the function (returned value: true) } // your code here:     as theya re not included in this code i have given an outline of two previous functions which join on the program i wrote previously Int totalrec=2 maxsize of the arrays = 300 currently stored values is 3 with 2 having titles and prices being value 1 and value 2 Basically i need help to write this function any information can be provided easily basically i know if it doesn't occur Return False; else Return true; as the data is currently stored in two elements we can check those two ideally though a function to potentially check all possible slots would be better i was thinking an I++ until == 300 Any Help would Be Greatly Appriciated Thanks I Realised that a check is needed on array all titles with a cin string search so maybe a linear search type function possibly. if so how to define this ? May 31 '10 #1
20 Replies

 Expert 100+ P: 1,123 Take each record from the allTitles array in for loop and compare it with the title. Use strcmp to do the comparison. And if they are same then fetch the value from the allPrices array at the ith index and return the value. Some questions, Does your function return the price? Then why you have declared the return type as boolean? Regards Dheeraj Joshi May 31 '10 #2

 P: 22 No it doesn't it is meant to be a find match then return bool type so i chose true and false as the representative terms Dheeraj how would i express this yes it is meant to check the array all titles then return the value say ith record in the allprices array as well as they store the same amount of records and are matched arrays so if it was say record 3 in titles that matches the input string then grab the price from the 3rd element in allprice if it doesn't match on title return value false. could you right up a function to do this as a guide as i haven't used strcmp before I am greatful for you help. May 31 '10 #3

 Expert Mod 5K+ P: 9,197 Since you are using string objects for titles, you must be using C++. Because of that you should not be using either arrays or strcmp. Instead of the array, use a vector. You can then use the string compare functions rather than strcmp. strcmp only works for C strings and not C++ strings. Expand|Select|Wrap|Line Numbers vector allTitles; //etc...   if (allTitles[i] == "The C++ Puzzle Book") //etc...   string obj("The C++ Puzzle Book")   if (allTitles[i] == obj) //etc... May 31 '10 #4

 P: 22 That maybe true but program specifications are to use arrays so that is whyI have done so basically all i need is the statement to check if cin >> "title" matches array alltitles [i] if there is a match grab the corresponding price from the allPrices [i] as an example i already know to return true if condition is met and return false if it does not match just need to figure out how to write the check and determine if i have prototyped and initialized the function correctly. Thanks I was thinking along the lines of if [title == Alltitles [i]] i know that isn't valid of course but an if loop which compares the cin string value to the array and then as the arrays are matched then pulls the eqivilant element from the all prices so if the cin string was equal to postion 3 in the allTiltes array then pull in the price in position 3 in the Prices array Else if it doesn't match anything return false; the logic is easy but i haven't got the check working correctly at this stage though. that is the main thing i am worried about. though do not remove the function BoolFindTitle () as that is a require element. Any help would be greatly appreciated Jun 1 '10 #5

 P: 22 Here is the code i have at the moment there ar eno complier errors but want to make sure that the function is set to check the cin inut matches any element in the ararys Expand|Select|Wrap|Line Numbers   # include  # include  # define MAXSIZE = 300 using namespace std;   bool findTitlePrice(string allTitles[300], double allPrices [300],int totalRec,string title,double price);    string allTitles[300] = {         "Book 1",         "Book 2"};     double allPrices[300] = {         78.5, 66.     }; int main ()  {     bool findTitlePrice(string allTitles [300], double allPrices [300],int totalRec,string title,double price);        int totalRec =2;    for(int i=0;i> Title; if (Title == allTitles [300]) cout <<"Value Found";   if (Title != allTitles [300]) cout << " Book Not Found ";   system ("pause");              }     I would love to ensure that i have done this correctly i know that std:: string with an == check may do this would the function i have applied achieve the same result if so how do i set it to return true if it is equal and pull the price from the equivalent element in the prices array as that if element 0 matches pull that in and pull price 0 in as well as they are " linked " arrays I know it is missing return true for the matching and return false for the not = function Any help on this would be most appreciated thanks for your time Jun 1 '10 #6

 Expert 100+ P: 1,123 Expand|Select|Wrap|Line Numbers if (Title == allTitles [300]) cout <<"Value Found";   if (Title != allTitles [300]) cout << " Book Not Found ";   system ("pause");   This is wrong. You are always checking the title with the 300th element of the array, where the data which you are interested will not be present. And i am not sure about usage of "==" operator for string comparison. And your for loop is ended with a semicolon. It is wrong. Your loop should look like this: Expand|Select|Wrap|Line Numbers for(int i=0;i

 P: 22 @dheerajjoshim I figured that the choice of field was wrong but can you help me define my code better Basically Say write a check if cin value >> "Title" == allTitles [] cout "the Book was found" return true else cout << "Book not found" return false" the code you seem to write is C i need C++ to deal with the array checks can you use my code and change it to meet my requirements if possible i figured i had used at least two semicolons i didn't need but it complies without errors It would be right to intialize Totalrec=2 (while i=0

 Expert 100+ P: 1,123 What made you to conclude that the code written by me won't work ? And where is your function definitions? I can not see any opening curly braces for function? Can you post latest code if you have? Regards Dheeraj Joshi Jun 1 '10 #9

 P: 22 I don't know that he code you posted doesn't work if i offended you i am sorry The code above is the latest another variation is as follows if it is more usable Expand|Select|Wrap|Line Numbers # include  # include  # define MAXSIZE = 300 using namespace std;   bool findTitlePrice(string allTitles[300],double allPrices[300],int totalRec, string title,double & price);       string allTitles[300] = {         "Book 1",         "Book 2"     };     double allPrices[300] = {         78.5, 66.     }; int main () { bool findTitlePrice(string allTitles[], double allPrices[], int totalRec, string title, double & price); {        int totalRec = 2;      for (int i=0; i> title;          if (title==allTitles[i])          {              return true;          }      else if (title!=allTitles[i])      return false;  }//End of For Statment     } system ("pause");}       I think this is closer to the required but still is not performing the check correctly Some of the functions don't have curly braces most already have the correct braces from what i can see which functions are missing them ? im assuming as the compiler reports no errors on either of these any corrections are much appreciated thanks again Jun 1 '10 #10

 Expert Mod 5K+ P: 9,197 THs code won't compile becuse you cannot write a function inside another function. This code shows a function written inside main(). Other problems: 1) totalRec is passed in as an argument and is then immediately set to a hard-coded 2. Why is it set at all? 2) when an "if" statement is true, there is no need to test false: Expand|Select|Wrap|Line Numbers if (a == true) {     //do this } else if (a == false) {     //do that } All you need is: Expand|Select|Wrap|Line Numbers if (a== c) {    //do this } //do that Or maybe, depending upon the function: Expand|Select|Wrap|Line Numbers if (a== true) {     //do this } else {     //do that } 3) you do not need a ; after a }: Expand|Select|Wrap|Line Numbers if ( a == true) {     //do this };   <--- ; not needed 4) All functions need an opening anc closing brace. Jun 1 '10 #11

 P: 22 I know basically i was meaning to set a true condtion which would return true else return false so realistically a single if statement if (title == AllTitles [i]) { Cout << " Match Found " Return True; Else Cout << Match Not Found Return False; and Pause TotalRec was hardcoded to 2 because there are two elements ideally i think i could leave it unassigned but really TotalRec is just to show the number of records Like there are 2 in each array so it is set at 2 I will Try That i was worried i had not set the if == function correctly to confirm is it looking at all elements in allTitles[] i think so but the basic thing is the check on the title condion if match is found hence the condtion == true statement the cout book title and price ANy Further Help Would Be Greatly Appreciated. Jun 2 '10 #12

 P: 22 I have got it looking like this Expand|Select|Wrap|Line Numbers   # include  # include  # define MAXSIZE = 300 using namespace std;   bool findTitlePrice(string allTitles[300],double allPrices[300],int totalRec, string title,double & price);       string allTitles[300] = {         "Book 1",         "Book 2"     };     double allPrices[300] = {         78.5, 66.     }; int main () { bool findTitlePrice(string allTitles[], double allPrices[], int totalRec, string title, double & price); {        int totalRec = 2;      for (int i=0; i> title;          if (title==allTitles[i])          {          cout << " Book Found";            cout <<" Here is The Title of the Book" << allTitles [i];          cout << " Here is The Price Of the Matched Book" <

 Expert 100+ P: 1,123 First of all are you trying to achieve what you want via OOP concept? And read weaknessforcats's post(#11) again. I still see some of coding errors. Regards Dheeraj Joshi Jun 2 '10 #14

 P: 22 No OOP concept i agree there are sill some errors mainly just need the check function figured out to cout the Title when the match is found and then the mathcing price can you check that i have declared that correctly there are erros with {} in some of the sections that is due to the complier throwing up errors So to Define the Program has to use the BOOLFINDPRICE function then check if a mathc is found if a match is found cout the element that matches such as if Alltitles [3] matches the cin input then print out the title string and the price in allPrices [3] as the two arrays are storing the same number of elements In regards to the if statement it has to return true if a match is found and as i said above cout the title and corresponding price from the prices arary else return false i have added an addition cout statements for clarity. The initialization of the boolfind Price with the [] on the arrays shows that i need to maybe enter the value Jun 2 '10 #15

 Expert 100+ P: 1,123 Your function definition must be out side the main function. And function definition must not end with a semicolon. And your for loop executes only ones even if it is true or false. Strip function findTitlePrice outside the main Expand|Select|Wrap|Line Numbers //Globals and includes here   int main() {     // Call to findTitlePrice with parameter. }   boolean findTitlePrice(arguments...) //No semicolon {     //Code logic; }   Your for loop need to return true if for loop fails. You return a false. Expand|Select|Wrap|Line Numbers for (int i=0; i> title;    if (title==allTitles[i])    {      cout << " Book Found";      cout <<" Here is The Title of the Book" << allTitles [i];      cout << " Here is The Price Of the Matched Book" <

 P: 22 it is meant to return false if it fails to find a match hence the else statement. yes i also realized that he call to findTitlePrice should be done in main will post if i have any issues with this section of code it is mainly the if title == function that i need to get right. Jun 2 '10 #17

 P: 22 I think i hav efindTitlePrice Declared Okay Expand|Select|Wrap|Line Numbers   # include  # include  # define MAXSIZE = 300 using namespace std;   bool findTitlePrice(string allTitles[300],double allPrices[300],int totalRec,string title,double price);      string allTitles[300]  ={          "Book 1",         "Book 2" };     double allPrices[300]  = {         78.5, 66.0 };     int main () {      int totalRec =2;     string title;     double price;     findTitlePrice(allTitles,allPrices,totalRec,title,price);   }{ bool findTitlePrice(string allTitles [],double allPrices [],int totalRec , string title ,double price);        for (int i=0; i> title;          if (title==allTitles[i])          {          cout << " Book Found";            cout <<" Here is The Title of the Book" << allTitles [i];          cout << " Here is The Price Of the Matched Book" <

 Expert 100+ P: 1,123 Expand|Select|Wrap|Line Numbers { bool findTitlePrice(string allTitles [],double allPrices [],int totalRec , string title ,double price);   This is wrong. Try giving Expand|Select|Wrap|Line Numbers bool findTitlePrice(string allTitles [],double allPrices [],int totalRec , string title ,double price) --> No semicolon {   One question do you understand the concepts of functions? Regards Dheeraj Joshi Jun 2 '10 #19

 P: 22 I tried that but it stated undeclared functions when i used it with error expected ; before for So i put it in there and the error went away i do understand the concept of functions try compiling my code and remove the ; see what happens using devC++ Apart from that does the rest seem okay or am i getting further off course i need to get this working Thanks again for your post can you see anything else that i have done wrong ? I Have spent many hours on this and it is doing my head in so any guidance towards my goal would be much appreciated. Jun 2 '10 #20

 P: 22 I have the code to this point Expand|Select|Wrap|Line Numbers       # include          # include          const int MAXSIZE = 300;         using namespace std;         bool findTitlePrice(string allTitles[MAXSIZE], double allPrices[MAXSIZE], int totalRec, string title, double price); //the names of the parameters are optional         string allTitles[MAXSIZE] ={         "Book 1",         "Book 2"         };         double allPrices[MAXSIZE] = {         78.5, 66.0 };         //in the prototype         int main()         {         string title;         double price;         int totalRec =2;         findTitlePrice(allTitles,allPrices,totalRec,title,price);         }         bool findTitlePrice(string allTitles[2], double allPrices[2], int totalRec,         string title, double price)         {  getline (cin,title);       for (int i=0;i<2;i++) {             if (title==allTitles[i])         {         cout << " Book Found";         cout <<" Here is The Title of the Book" << allTitles [i];         cout << " Here is The Price Of the Matched Book" <