C++ Switch Statement within program to perform multiple functions

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

    C++ Switch Statement within program to perform multiple functions

    Here is the code for my program
    Code:
    // Book Price (16800960)
    
     
      #include <iostream>
      #include <string>
     #include <iomanip>
    
     const int MAXSIZE 300;
    
     using namespace std;
     void displayMenu ();
     void listRecords(string allTitles[300], double allPrices[300], int totalRec);
    
     string allTitles[MAXSIZE] = {
            "Book 1",
            "Book 2"
        };
        double allPrices[MAXSIZE] = {
            78.5, 66.
        };
             int main ()
             
             {
               
     void displayMenu ();
    
    
    
     cout << "MAIN MENU"<<"\n";cout<<endl;
     cout << " 0. Exit " " 1. Enter Book " " 2. Find Book "<<"\n"; cout <<endl;
     cout << " 3. List All "  " 4. Delete Book"   " 5. Update Book " << "\n"; 
     cout   <<endl;
    
           string Title;
           double Price; 
           int totalRec =2;
           char menu;
           cout << "Please Enter Menu Selection" ;
            cin >> menu; 
            while (menu!=0);
            switch (menu)
            
           {
         
             case '4':
                 string title;
         cout << "Enter Title of the book record to delete ->";
    
        getline(cin,title);
       for(int i = 0; i < totalRec; i++)
         {
          if (title == allTitles[i])
          {allTitles[i] = allTitles[i--];
          allPrices[i] = allPrices[i--];
           totalRec = (totalRec --);
          cout << " Record Erased Successfully"; cout <<endl;
    
          }}

    The compiler is not showing errors but i want to see if the function i have written in case 4 will accept a title check against the array if it finds a match delete the record and the corresponding price


    as the program is not loading once a number is inputted basically it need it to perform the delete function if a matching title is found


    now it is the if loop and array declaration that i believe i have got wrong

    Any Help on this would be greatly appreciated
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    #2
    Is it incomplete code or you forgot to declare your main function?

    Regards
    Dheeraj Joshi

    Comment

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

      #3
      Also what is the logic behind following codes?

      Code:
      if (title == allTitles[i]){
      allTitles[i] = allTitles[i--];
      allPrices[i] = allPrices[i--];
      Regards
      Dheeraj Joshi

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Originally posted by 16800960
        Here is the code for my program
        Code:
        // Book Price (16800960)
        
         
          #include <iostream>
          #include <string>
         #include <iomanip>
        
         const int MAXSIZE 300;
        
         using namespace std;
         void displayMenu ();
         void listRecords(string allTitles[300], double allPrices[300], int totalRec);
        
         string allTitles[MAXSIZE] = {
                "Book 1",
                "Book 2"
            };
            double allPrices[MAXSIZE] = {
                78.5, 66.
            };
                 int main ()
                 
                 {
                   
         void displayMenu ();
        
        
        
         cout << "MAIN MENU"<<"\n";cout<<endl;
         cout << " 0. Exit " " 1. Enter Book " " 2. Find Book "<<"\n"; cout <<endl;
         cout << " 3. List All "  " 4. Delete Book"   " 5. Update Book " << "\n"; 
         cout   <<endl;
        
               string Title;
               double Price; 
               int totalRec =2;
               char menu;
               cout << "Please Enter Menu Selection" ;
                cin >> menu; 
                while (menu!=0);
                switch (menu)
                
               {
             
                 case '4':
                     string title;
             cout << "Enter Title of the book record to delete ->";
        
            getline(cin,title);
           for(int i = 0; i < totalRec; i++)
             {
              if (title == allTitles[i])
              {allTitles[i] = allTitles[i--];
              allPrices[i] = allPrices[i--];
               totalRec = (totalRec --);
              cout << " Record Erased Successfully"; cout <<endl;
        
              }}

        The compiler is not showing errors but i want to see if the function i have written in case 4 will accept a title check against the array if it finds a match delete the record and the corresponding price


        as the program is not loading once a number is inputted basically it need it to perform the delete function if a matching title is found


        now it is the if loop and array declaration that i believe i have got wrong

        Any Help on this would be greatly appreciated
        now it is the if loop and array declaration that i believe i have got wrong
        Good spot.

        The first issue, book title and price are related on a 1 - 1 basis. However you have implemented them as 2 separate things in 2 separate arrays. A book is an object that has many attributes, you should implement it as such using a class. If your book currently only has title and price but is implemented in a working program as a class then it is fairly easy to add more attributes, author say, by altering the class. Currently to add a new attribute you would need to add a new array and alter most of the code.

        1. Implement book as a class

        Your for loop does not handle the array correctly, it only acts on one entry in the array but in reality it would need to act of all entries following the erased entry. However in C++ there is a better way than arrays which are not managed and have little functionality. In C++ there are the STL containers, they manage memory for you and provide additional functionality. Using a container you would just have to find the correct record and then call the erase function. For an application like this I would recommend a <list>.

        This will greatly simply your code.

        2. Don't use arrays use STL containers

        Comment

        • 16800960
          New Member
          • May 2010
          • 22

          #5
          the logic behind the -- functions though they are not declared correctly was if a matching title is found it is meant to remove the record from the arrays the reason two arrays are used is that the program specifies that the two arrays must be present and executed before the menu appears


          i had a better for loop for the other section though i would use a list as long as i can point it to the arrays cannot use classes unfortunately i would have used a struct normally though this doesn't need the extra attributes though they would be a nice feature not required by program specifications


          if you can see an easy way to resolve this issue then i would be most greatful as i have limited time to get this done mainly have to deal with case 4 and the menu not returning causing the lockup of the program.


          as far as main goes i should have closed it earlier after the menu is called i mispositioned the closing bracket

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            OK I thought you might say something like that.

            So try this, if you start with an array of book titles

            {"Book 1", "Book 2", "Book 3", "Book 4", "Book 5"}
            size = 5

            And you want to remove "Book 2" from the array so it looks like this

            {"Book 1", "Book 3", "Book 4", "Book 5"}
            size = 4

            Remembering that arrays do not actually have an erase function what operations do you have to perform to transform the first list to the second?

            Comment

            • 16800960
              New Member
              • May 2010
              • 22

              #7
              first function is to search the array for the matching title

              then select the position of the match then remove that position and update the total rec


              Then the Price is done similarly though it selects based on the psoiton of the title though

              really i would

              search for string title " book2"

              use a for loop which will show

              the position of alltitles[i]

              then maybe use a where statement where pos of alltitles = pos of allprices delete
              ?

              delete [] Alltitles where string title = alltitles [i] or maybe memset ()

              im not sure as i haven't done a delete before

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                OK and once you have found the position of the book to remove, what is it you actually do to remove it?

                Comment

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

                  #9
                  Hi 16800960.
                  Arrays may not be best option to solve your problem.
                  Is there any specification saying you need to achieve it using arrays?
                  If not go for Templates. Banfa already mentioned about <list>

                  Regards
                  Dheeraj Joshi

                  Comment

                  • 16800960
                    New Member
                    • May 2010
                    • 22

                    #10
                    the specifications Dheera is that the data is stored in the arrays not that i have to use the arrays to delete though that was i what i was assuming as it would be easiest.

                    as far as the delete wouldn't i have to find the element which matches the getline string then set element = number 1 lower and apply this to all larger elements

                    something similar to this maybe

                    for ( i = 0; i < n; i++ ) {

                    if ( alltitles[i] == target )

                    break;

                    while ( ++i < n )

                    a[i - 1] = a[i];

                    --n;

                    Comment

                    Working...