bool findTitlePrice function code help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • 16800960
    New Member
    • May 2010
    • 22

    bool findTitlePrice function code help

    The Code is as Follows
    Code:
    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 ?
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    #2
    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

    Comment

    • 16800960
      New Member
      • May 2010
      • 22

      #3
      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.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        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.

        Code:
        vector<string> allTitles;
        //etc...
        
        if (allTitles[i] == "The C++ Puzzle Book")
        //etc...
        
        string obj("The C++ Puzzle Book")
        
        if (allTitles[i] == obj)
        //etc...

        Comment

        • 16800960
          New Member
          • May 2010
          • 22

          #5
          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

          Comment

          • 16800960
            New Member
            • May 2010
            • 22

            #6
            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

            Code:
            # include <iostream>
            # include <string>
            # 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<totalRec;i++); 
               
                string Title;
                string allTitles [300];
               cout << "Please Enter Title";
                cin >> 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

            Comment

            • Dheeraj Joshi
              Recognized Expert Top Contributor
              • Jul 2009
              • 1129

              #7
              Code:
              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:
              Code:
              for(int i=0;i<totalRec;i++)
              {
                 char * book_at_i = allTitles [i])
                 if(strcmp (title,book_at_i) == 0)
                 {
                   return true;
                 }
              }
              return false;
              Regards
              Dheeraj Joshi

              Comment

              • 16800960
                New Member
                • May 2010
                • 22

                #8
                Originally posted by dheerajjoshim
                Code:
                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:
                Code:
                for(int i=0;i<totalRec;i++)
                {
                   char * book_at_i = allTitles [i])
                   if(strcmp (title,book_at_i) == 0)
                   {
                     return true;
                   }
                }
                return false;
                Regards
                Dheeraj Joshi
                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

                Comment

                • Dheeraj Joshi
                  Recognized Expert Top Contributor
                  • Jul 2009
                  • 1129

                  #9
                  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

                  Comment

                  • 16800960
                    New Member
                    • May 2010
                    • 22

                    #10
                    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

                    Code:
                    # include <iostream>
                    # include <string>
                    # 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<totalRec; i++)
                         {
                             string title;
                             
                             cin >> 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

                    Comment

                    • weaknessforcats
                      Recognized Expert Expert
                      • Mar 2007
                      • 9214

                      #11
                      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:

                      Code:
                      if (a == true)
                      {
                          //do this
                      }
                      else if (a == false)
                      {
                          //do that
                      }
                      All you need is:

                      Code:
                      if (a== c)
                      {
                         //do this
                      }
                      //do that
                      Or maybe, depending upon the function:
                      Code:
                      if (a== true)
                      {
                          //do this
                      }
                      else
                      {
                          //do that
                      }
                      3) you do not need a ; after a }:

                      Code:
                      if ( a == true)
                      {
                          //do this
                      };   <--- ; not needed
                      4) All functions need an opening anc closing brace.

                      Comment

                      • 16800960
                        New Member
                        • May 2010
                        • 22

                        #12
                        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.

                        Comment

                        • 16800960
                          New Member
                          • May 2010
                          • 22

                          #13
                          I have got it looking like this

                          Code:
                          # include <iostream>
                          # include <string>
                          # 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<totalRec; i++)
                               {
                                   string title;
                                   
                                   cin >> 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" <<allPrices [i];
                                   return true;
                                   }
                                   
                                   {else
                                cout << " Book Not Avaliable"; 
                              return false; 
                            }//End of For Statment
                          
                          system ("pause");}
                          
                          }}

                          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

                          Comment

                          • Dheeraj Joshi
                            Recognized Expert Top Contributor
                            • Jul 2009
                            • 1129

                            #14
                            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

                            Comment

                            • 16800960
                              New Member
                              • May 2010
                              • 22

                              #15
                              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

                              Comment

                              Working...