By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,581 Members | 2,007 Online
Bytes IT Community
+ Ask a Question
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
  1. bool findTitlePrice(string allTitles[], double allPrices[],
  2. int totalRec, string title, double & price)
  3. {
  4. for(int i=0; i < totalRec; i++)
  5. {
  6. // your code here:
  7. // for each element of array allTitles, check if it matches
  8. // the given title, i.e. if title and allTitles[i] are the same;
  9. // if yes, fetch the corresponding price from
  10. // the array allPrices and exit the function (returned value: true)
  11. }
  12. // your code here:
  13.  
  14.  
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
Share this Question
Share on Google+
20 Replies


Dheeraj Joshi
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

weaknessforcats
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<string>. 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
  1. vector<string> allTitles;
  2. //etc...
  3.  
  4. if (allTitles[i] == "The C++ Puzzle Book")
  5. //etc...
  6.  
  7. string obj("The C++ Puzzle Book")
  8.  
  9. if (allTitles[i] == obj)
  10. //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
  1.  
  2. # include <iostream>
  3. # include <string>
  4. # define MAXSIZE = 300
  5. using namespace std;
  6.  
  7. bool findTitlePrice(string allTitles[300], double allPrices [300],int totalRec,string title,double price);
  8.  
  9.  string allTitles[300] = {
  10.         "Book 1",
  11.         "Book 2"};
  12.     double allPrices[300] = {
  13.         78.5, 66.
  14.     };
  15. int main () 
  16. {
  17.  
  18.  
  19. bool findTitlePrice(string allTitles [300], double allPrices [300],int totalRec,string title,double price); 
  20.  
  21.  
  22.  
  23. int totalRec =2; 
  24.   for(int i=0;i<totalRec;i++); 
  25.  
  26.     string Title;
  27.     string allTitles [300];
  28.    cout << "Please Enter Title";
  29.     cin >> Title;
  30. if (Title == allTitles [300])
  31. cout <<"Value Found";
  32.  
  33. if (Title != allTitles [300]) cout << " Book Not Found ";
  34.  
  35. system ("pause");
  36.  
  37.  
  38.          }
  39.  
  40.  

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

Dheeraj Joshi
Expert 100+
P: 1,123
Expand|Select|Wrap|Line Numbers
  1. if (Title == allTitles [300])
  2. cout <<"Value Found";
  3.  
  4. if (Title != allTitles [300]) cout << " Book Not Found ";
  5.  
  6. system ("pause");
  7.  
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
  1. for(int i=0;i<totalRec;i++)
  2. {
  3.    char * book_at_i = allTitles [i])
  4.    if(strcmp (title,book_at_i) == 0)
  5.    {
  6.      return true;
  7.    }
  8. }
  9. return false;
  10.  
Regards
Dheeraj Joshi
Jun 1 '10 #7

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 <TotalRec I++) maybe a strCMP or as i have been told something like if Title == allTitles [i] ?
any help on this would be greatly appriciated
please
Jun 1 '10 #8

Dheeraj Joshi
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
  1. # include <iostream>
  2. # include <string>
  3. # define MAXSIZE = 300
  4. using namespace std;
  5.  
  6. bool findTitlePrice(string allTitles[300],double allPrices[300],int totalRec, string title,double & price);
  7.  
  8.     string allTitles[300] = {
  9.         "Book 1",
  10.         "Book 2"
  11.     };
  12.     double allPrices[300] = {
  13.         78.5, 66.
  14.     };
  15. int main ()
  16. {
  17. bool findTitlePrice(string allTitles[], double allPrices[], int totalRec, string title, double & price);
  18. {  
  19.      int totalRec = 2;
  20.      for (int i=0; i<totalRec; i++)
  21.      {
  22.          string title;
  23.  
  24.          cin >> title;
  25.          if (title==allTitles[i])
  26.          {
  27.              return true;
  28.          }
  29.      else if (title!=allTitles[i])
  30.      return false;  }//End of For Statment
  31.  
  32.  
  33. }
  34. system ("pause");}
  35.  
  36.  
  37.  

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

weaknessforcats
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
  1. if (a == true)
  2. {
  3.     //do this
  4. }
  5. else if (a == false)
  6. {
  7.     //do that
  8. }
All you need is:

Expand|Select|Wrap|Line Numbers
  1. if (a== c)
  2. {
  3.    //do this
  4. }
  5. //do that
Or maybe, depending upon the function:
Expand|Select|Wrap|Line Numbers
  1. if (a== true)
  2. {
  3.     //do this
  4. }
  5. else
  6. {
  7.     //do that
  8. }
3) you do not need a ; after a }:

Expand|Select|Wrap|Line Numbers
  1. if ( a == true)
  2. {
  3.     //do this
  4. };   <--- ; 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
  1.  
  2. # include <iostream>
  3. # include <string>
  4. # define MAXSIZE = 300
  5. using namespace std;
  6.  
  7. bool findTitlePrice(string allTitles[300],double allPrices[300],int totalRec, string title,double & price);
  8.  
  9.     string allTitles[300] = {
  10.         "Book 1",
  11.         "Book 2"
  12.     };
  13.     double allPrices[300] = {
  14.         78.5, 66.
  15.     };
  16. int main ()
  17. {
  18. bool findTitlePrice(string allTitles[], double allPrices[], int totalRec, string title, double & price);
  19. {  
  20.      int totalRec = 2;
  21.      for (int i=0; i<totalRec; i++)
  22.      {
  23.          string title;
  24.  
  25.          cin >> title;
  26.          if (title==allTitles[i])
  27.          {
  28.          cout << " Book Found";
  29.  
  30.          cout <<" Here is The Title of the Book" << allTitles [i];
  31.          cout << " Here is The Price Of the Matched Book" <<allPrices [i];
  32.          return true;
  33.          }
  34.  
  35.          {else
  36.       cout << " Book Not Avaliable"; 
  37.     return false; 
  38.   }//End of For Statment
  39.  
  40. system ("pause");}
  41.  
  42. }}
  43.  
  44.  

Extra braces are caused by the compiler throwing errors


Expected; before else occurs even though it is in the {} of the secondary function


im also sure that my check function is not performing the check i would be most grateful for any further help you can apply
Jun 2 '10 #13

Dheeraj Joshi
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

Dheeraj Joshi
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
  1. //Globals and includes here
  2.  
  3. int main()
  4. {
  5.     // Call to findTitlePrice with parameter.
  6. }
  7.  
  8. boolean findTitlePrice(arguments...) //No semicolon
  9. {
  10.     //Code logic;
  11. }
  12.  
Your for loop need to return true if for loop fails. You return a false.

Expand|Select|Wrap|Line Numbers
  1. for (int i=0; i<totalRec; i++)
  2. {
  3.    string title;
  4.    cin >> title;
  5.    if (title==allTitles[i])
  6.    {
  7.      cout << " Book Found";
  8.      cout <<" Here is The Title of the Book" << allTitles [i];
  9.      cout << " Here is The Price Of the Matched Book" <<allPrices [i];
  10.      return true;
  11.    }
  12. }
  13. return false;
  14.  
Regards
Dheeraj Joshi
Jun 2 '10 #16

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
  1.  
  2. # include <iostream>
  3. # include <string>
  4. # define MAXSIZE = 300
  5. using namespace std;
  6.  
  7. bool findTitlePrice(string allTitles[300],double allPrices[300],int totalRec,string title,double price);
  8.  
  9.    string allTitles[300]  ={ 
  10.         "Book 1",
  11.         "Book 2"
  12. };
  13.     double allPrices[300]  = {
  14.         78.5, 66.0 };
  15.  
  16.  
  17. int main ()
  18.     int totalRec =2;
  19.     string title;
  20.     double price;
  21.     findTitlePrice(allTitles,allPrices,totalRec,title,price);  
  22. }{
  23. bool findTitlePrice(string allTitles [],double allPrices [],int totalRec , string title ,double price);
  24.  
  25.      for (int i=0; i<totalRec; i++) {
  26.  
  27.  
  28.  
  29.          cin >> title;
  30.          if (title==allTitles[i])
  31.          {
  32.          cout << " Book Found";
  33.  
  34.          cout <<" Here is The Title of the Book" << allTitles [i];
  35.          cout << " Here is The Price Of the Matched Book" <<allPrices [i];
  36.          return true;
  37.          }
  38.  
  39.          else {
  40.       cout << " Book Not Avaliable"; 
  41.     return false; 
  42.   }//End of For Statment
  43.  
  44. system ("pause");}
  45. }
  46.  
  47.  

Though the other functions it is causing more complier errors

I think i have made more mistakes in adding this function by changing the other ones as well
Jun 2 '10 #18

Dheeraj Joshi
Expert 100+
P: 1,123
Expand|Select|Wrap|Line Numbers
  1. {
  2. bool findTitlePrice(string allTitles [],double allPrices [],int totalRec , string title ,double price);
  3.  
This is wrong.

Try giving
Expand|Select|Wrap|Line Numbers
  1. bool findTitlePrice(string allTitles [],double allPrices [],int totalRec , string title ,double price) --> No semicolon
  2. {
  3.  
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
  1.       # include <iostream>
  2.  
  3.       # include <string>
  4.  
  5.       const int MAXSIZE = 300;
  6.  
  7.       using namespace std;
  8.  
  9.       bool findTitlePrice(string allTitles[MAXSIZE], double allPrices[MAXSIZE], int totalRec, string title, double price); //the names of the parameters are optional
  10.  
  11.       string allTitles[MAXSIZE] ={
  12.  
  13.       "Book 1",
  14.  
  15.       "Book 2"
  16.  
  17.       };
  18.  
  19.       double allPrices[MAXSIZE] = {
  20.  
  21.       78.5, 66.0 };
  22.  
  23.       //in the prototype
  24.  
  25.       int main()
  26.  
  27.       {
  28.  
  29.       string title;
  30.  
  31.       double price;
  32.  
  33.       int totalRec =2;
  34.  
  35.       findTitlePrice(allTitles,allPrices,totalRec,title,price);
  36.  
  37.       }
  38.  
  39.       bool findTitlePrice(string allTitles[2], double allPrices[2], int totalRec,
  40.  
  41.       string title, double price)
  42.  
  43.       {
  44.  getline (cin,title);
  45.       for (int i=0;i<2;i++) {
  46.  
  47.  
  48.  
  49.       if (title==allTitles[i])
  50.  
  51.       {
  52.  
  53.       cout << " Book Found";
  54.  
  55.       cout <<" Here is The Title of the Book" << allTitles [i];
  56.  
  57.       cout << " Here is The Price Of the Matched Book" <<allPrices [i];
  58.  
  59.       return true;
  60.  
  61.       }
  62.  
  63.       }
  64.  
  65. cout << " Book Not Avaliable";
  66.  
  67.       return false;
  68.  
  69.       cin.get ();
  70.  
  71.       }
  72.  

It is not reading the price from the price array though any help on this would be greatly appreciated it also displays the title as follows when it matches


book = Book 0 which is placing the element in the cout statement.

:)
Jun 4 '10 #21

Post your reply

Sign in to post your reply or Sign up for a free account.