473,806 Members | 2,732 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

bool findTitlePrice function code help

22 New Member
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
20 1949
Dheeraj Joshi
1,123 Recognized Expert Top Contributor
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
16800960
22 New Member
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
9,208 Recognized Expert Moderator Expert
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
16800960
22 New Member
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
16800960
22 New Member
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
1,123 Recognized Expert Top Contributor
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
16800960
22 New Member
@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
1,123 Recognized Expert Top Contributor
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
16800960
22 New Member
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

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

Similar topics

1
5420
by: Haibao Tang | last post by:
Hail Python pals! I played with the R (http://r-project.cran.org) last night to do some statistics and it has an interactive session too, and I found a feature that is quite useful. I found by actually typing a function name, the R gives you a code body output. > f1 = function(x){x*x} > f1 function(x){x*x}
9
2418
by: hope | last post by:
Hi Access 97 I'm lost on this code please can you help ================================= Below is some simple code that will concatenate a single field's value from multiple records into a single string separated by a user defined character. There is no error trapping (by design), USE AT YOUR OWN RISK.
4
2832
by: Terencetrent | last post by:
I having been using Access '97/2002 for about 4 years now and have never really had the need or the time to learn visual basic. Well, I think the time has finally come. I need help with Visual Basic code that will examine numeric value for a particular field in a query, and assign a new numeric vaule to that field. There are over 21 possible values and I am told that IIF statement will only handle 9 of the possibilities and that I need...
3
4743
by: Webdiyer | last post by:
I want to integrate SecurID two-factor authentication system of the RSASecurity.inc into our asp.net application,but I get into trouble when trying to call the API functions of the ACE Agent,I got an error message saying "Cannot marshal parameter #2: Invalid managed/unmanaged type combination (this value type must be paired with Struct)" when calling "AceGetPinParams(iHandle,ref sd_pin)" function,here's my test code: private static...
1
1604
by: Bern McCarty | last post by:
What do you make of this? I cannot tell for sure but it almost seems as the the transition thunk to get back from the native bool method to the managed caller is looking at eax and, if any bit is set, normalizing it to 0x00000001. If it wants to normalize the value then it should only operate on the al register since that's all that the native bool method uses to hold the return value. Is this a known VC 7.1 bug? Is there a hotfix...
5
1304
by: deercreek | last post by:
I could use a little help from a good code writer out there. I found some code and modified it a bit for my needs but, I need a little help to finish it up. What I am trying to due is to get a number to fill out a text box on my form. I want it to look at the form and get the CampStartDate and my CampEndDate also to look at a table of holidays. Then I want the text box to be filled with the number of days they will be staying that do not...
2
1852
by: Rash33d | last post by:
Hi there! could som1 please help me out? i need toknow if there's something wrong with this javascript code. The code is meant to give the american dollar and european equivalent of a price. i.e when i type a price in pounds, it gives me the price in dollars and euros. Thanks in advance function fix(thenumber,noplaces){ // returns the number to n decimal places var oldnumber=thenumber;
5
1189
by: BabyBlue | last post by:
I have a function like this: function get_articles($cat=1,$numberposts=1); it can be used: get_articles(cat=4&numberposts=6); //will display 6 posts from category 6 then I want to use that function within another function, like this: function display_cat($new_cat=1,$new_num=1,$othercontent="")
2
2112
by: whodgson | last post by:
The code below is part of the title fuction. What has been omitted is setting all elements from 2 to a user entered bound in prime to true and then setting all elements from j=2 to 2*j< bound to false thus ruling out all even numbers as prime. This last code finds all primes after 2 and before bound and works properly. int p=3 int p = 3; while (p<= bound/2) { for(int j=2;p*j<bound;j++) prime = false;//multiples of p are...
4
2146
by: phpmel | last post by:
Hi, i have a javascript function that changes the text in a label (ASP.NET form) to blank....It works fine just asit is shown below. However, I dont want to hard code the label name to the function. I want to be a ble to probably pass the label name as a parameter so that the function can work no matter what label it is. Can some one please help me add the modifications to it. <script type="text/javascript"> function...
0
10623
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10371
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10111
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9192
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7650
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4330
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3852
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3010
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.