Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Problem with switch statement and function call...URGENT!!

Posted on 2003-12-09
10
409 Views
Last Modified: 2010-04-01
I only have 380 points left but two very urgent problems due to a deadline of tomorrow. If each question is answered by the different people, I will split the point evenly or based on how complex the resolution is. The code of my entire program is listed below. My two problems are:

1. Each case in the switch statement does not break after completion and just keeps looping.

2. The Modify function generates this error: error C2676: binary '[' : 'DVD' does not define this operator or a conversion to a type acceptable to the predefined operator.

Please help...this is my final class project and I have been struggling with it for days.

Thanks!!
AC

#include <iostream>
#include <iomanip>
#include <ctype.h>
#include <string.h>

using namespace std;

const int MAXTITLE = 25;      //max no. of characters in title.
enum DVDGenre{ACTION, MYSTERY, COMEDY, ROMANCE}Genre;


struct DVD
{
      char Title[25];
      int Year;
      double Price;
      int Genre;
};

void populate(struct DVD &theDVD);
void display(struct DVD &theDVD, int);
void modify(struct DVD &theDVD);

int main() //displays user menu and calls functions to display and modify the DVD array
{
      struct DVD myDVD[3];
      char response;
      
      cout << "1. Populate the DVD structure" << endl;
      cout << "2. Display the DVD structure" << endl;
      cout << "3. Modify the DVD structure" << endl;
      cout << "Q. Quit" << endl << endl;
      cout << "What would you like to do? ";
      cin >> response;

      do
      {
      switch(response)
            {
            case '1': //populates the DVD array
                        for (int i=0; i<3; i++)
                        populate(myDVD[i]);
                        break;
            case '2': //displays the DVD array
                        for (int i=0; i<3; i++)
                        display(myDVD[i], i);
                        cout << endl;
                        break;
            case '3': //modifys the DVD array
                        modify(myDVD[3]);
                        break;                        
            case 'Q':
            case 'q':
                        break;
            default :
                        cout << "Invalid Entry" << endl << endl;;
                        break;
            }
      }while(response != 'Q' && response != 'q');
      
      
}

void populate(DVD &theDVD) //populates a single DVD structure
{
      int tempYear=0;
      double tempPrice;
      char TempTitle[MAXTITLE] = "";
      int inputGenre;
            
      cin.ignore();
      cout << "\nEnter the movie title: ";
      cin.getline(TempTitle,MAXTITLE, '\n');
      for(int v = 0; TempTitle[v] != '\0'; v++) //convert title to uppercase
            TempTitle[v] = toupper(TempTitle[v]);
      strcpy(theDVD.Title, TempTitle);
            
      while(1) //validates the year.
      {
            char inputYear[10]="";
            
            cout << "\nEnter the year of release: ";
            cin.getline(inputYear, 10);
            if(strlen(inputYear) != 4)
            {
                  cout << "\nInvalid year" << endl << endl;
                  continue;
            }
            if (!isdigit(inputYear[0]) ||
                  !isdigit(inputYear[1]) ||
                  !isdigit(inputYear[2]) ||
                  !isdigit(inputYear[3]))  
            {
                  cout << "\nInvalid year" << endl << endl;
                  continue;
            }            
            tempYear = atoi(inputYear);

            if(tempYear < 1910 || tempYear > 2003)
            {
                  cout << "\nInvalid year" << endl << endl;
                  continue;
            }
            
            theDVD.Year = tempYear;
            break;
                  
      }
      theDVD.Year = tempYear;
      

      while(1) //validates the price
      {
            char inputPrice[10];
            
            cout << "\nEnter the DVD price: ";
            cin.getline(inputPrice, 10);
            int len = strlen(inputPrice);

            if (len > 5)
            {
                  cout << "Price cannot be exceed $99.99" << endl << endl;
                  continue;
            }
            if (len < 4)
            {
                  cout << "Invalid Price" << endl << endl;
                  continue;
            }
            if (len == 4 && !ispunct(inputPrice[1]))
            {
                  cout << "Invalid Price" << endl << endl;
                  continue;
            }
            if (len == 4 && (!isdigit(inputPrice[0]) ||
                              !isdigit(inputPrice[2]) ||
                              !isdigit(inputPrice[3])))
            {
                  cout << "Invalid Price" << endl << endl;
                  continue;
            }
            if (len == 5 && !ispunct(inputPrice[2]))
            {
                  cout << "Invalid Price" << endl << endl;
                  continue;
            }
            if (len == 5 && (!isdigit(inputPrice[0]) ||
                              !isdigit(inputPrice[1]) ||
                              !isdigit(inputPrice[3]) ||
                              !isdigit(inputPrice[4])))
            {
                  cout << "Invalid Price" << endl << endl;
                  continue;
            }
            
            tempPrice = atof(inputPrice);
            break;
            
      }
      theDVD.Price = tempPrice;


      
      do //chooses the DVD genre
      {
            cout << "\n[0]ACTION  [1]MYSTERY  [2]COMEDY  [3]ROMANCE" << endl;
            cout << "Choose the genre: ";
            cin >> inputGenre;
            

      }while(inputGenre > 3 || inputGenre < 0);

      theDVD.Genre = inputGenre;
      
}
void display(DVD &theDVD, int i) //displays a single DVD structure
{
      
            cout << endl << endl <<setiosflags(ios::left)
                  << setw(8) << i << "  " << theDVD.Title
                  << setw(5) << theDVD.Year << setw(6) << theDVD.Price << "  ";
                  if(theDVD.Genre == 0)
                        cout << "ACTION";
                  else if(theDVD.Genre == 1)
                        cout << "MYSTERY";
                  else if(theDVD.Genre == 2)
                        cout << "COMEDY";
                  else if(theDVD.Genre == 3)
                        cout << "ROMANCE";



            return;
}



void modify(DVD theDVD) //modifies a single DVD structure
{
      int j;
      while(1)
      {
            cout << "\nWhich array do you wish to modify? ";
            cin >> j;
            if (j < 0 || j > 2)
            {
                  cout << "\nInvalid selection" << endl << endl;
                  continue;
            }
            break;
      }
      populate(theDVD[j]); //calls the populate function
}
0
Comment
Question by:verbal11
  • 5
  • 2
  • 2
  • +1
10 Comments
 
LVL 44

Assisted Solution

by:Karl Heinz Kremer
Karl Heinz Kremer earned 100 total points
ID: 9909021
You do while loop is not getting any more input: Just before you enter the loop, you are reading the response, but that's the only time you are getting user input. You have to move the section that displays the menu and reads the response into the loop.
0
 
LVL 2

Author Comment

by:verbal11
ID: 9909035
Thanks, that worked for part 1 of my question. Can you provide any assistance for part 2?
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 9909041
Your second problem is that you are passing in a DVD struct, which is only one element. You are however trying to access theDVD[j], like it was an array. This will not work. You have to pass in an array for this to work. Because this looks like homework, I'll leave it at this. You should be able to figure out how to fix this witht the information you have.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 1

Expert Comment

by:meow00
ID: 9909844
Hello .....

     I am not sure what u exactly want to do in the modify function. But there are some problems in modify function.
-----------------------------------------------------------------------
void modify(DVD& theDVD) //modifies a single DVD structure
{
//     int j;
//     while(1)
//   {
//          cout << "\nWhich array do you wish to modify? ";
//          cin >> j;
//          if (j < 0 || j > 2)
//          {
//               cout << "\nInvalid selection" << endl << endl;
//               continue;
//          }
//          break;
//     }

     populate(theDVD); //calls the populate function

}
---------------------
1. It should be "void modify(DVD& theDVD)" rather than "void modify(DVD theDVD)"
2. It should be "populate(theDVD)" rather than populate(theDVD[j]) ;
    since you already pass theDVD[3] to the modify function, now it is a "theDVD", and
    an "theDVD array"
    So the "int j" in the "void modify" doesn't really make much sense ......

    I am not quite sure what u want to do in the modify function, but if u comment those lines as above ... at least the code would compile .....

meow......


0
 
LVL 2

Author Comment

by:verbal11
ID: 9909932
Basically this program asks the user to input the Title, Year, Price and Genre for 3 different DVDs, and stores them in a DVD structure. The modify function should allow the user to modify a single DVD structure depending on which one the user chooses. I tried your suggestion, and yes it does compile, but it does not modify anything.

Thanks...
AC
0
 
LVL 4

Accepted Solution

by:
dhyanesh earned 280 total points
ID: 9909990
Hi

Make your modify function something like this it should work fine.

void modify(DVD theDVD[]) //modifies a single DVD structure
{
    int j;
    while(1)
    {
         cout << "\nWhich array do you wish to modify? ";
         cin >> j;
         if (j < 0 || j > 2)
         {
              cout << "\nInvalid selection" << endl << endl;
              continue;
         }
         break;
    }
    populate(theDVD[j]); //calls the populate function
}


The only change is here:

void modify(DVD theDVD[])

You have to accept a pointer to entire array.

Also in main you have to pass only myDVD i.e. something like:

  case '3': //modifys the DVD array
                   modify(myDVD);
                   break;                    

Dhyanesh
0
 
LVL 2

Author Comment

by:verbal11
ID: 9910016
The line:

modify(myDVD);

returns this error:
error C2664: 'modify' : cannot convert parameter 1 from 'DVD [3]' to 'DVD &'
0
 
LVL 2

Author Comment

by:verbal11
ID: 9910033
Nevermind, I forgot to change the function prototype.
0
 
LVL 1

Expert Comment

by:meow00
ID: 9910042
okay ...

I guess u need someting like :
----------------------------------
void modify(DVD& theDVD) //modifies a single DVD structure
{
//     int j;
//     while(1)
//   {
//          cout << "\nWhich array do you wish to modify? ";
//          cin >> j;
//          if (j < 0 || j > 2)
//          {
//               cout << "\nInvalid selection" << endl << endl;
//               continue;
//          }
//          break;
//     }
     cout << "So, what do u want to modify ? " << endl ;
     cout << " 1. title  2. year 3. Price 4. Genre" << endl ;
     cout << " choose a noumber : "<<endl ;
     cin.ignore(80,'\n') ;
     int choice ;
     cin >> choice ;
  switch(choice){

       case 1 :
       {  char TempTitle[MAXTITLE] ;
          cout << "enter the new title : " << endl ;
          cin.getline(TempTitle,MAXTITLE, '\n');
     for(int v = 0; TempTitle[v] != '\0'; v++) //convert title to uppercase
          TempTitle[v] = toupper(TempTitle[v]);
     strcpy(theDVD.Title, TempTitle);

          break ;
       }

       case 2 :
       {
         blah ... blah..
       }
       case 3 :
       {
         blah ... blah ..
       }

     }


     populate(theDVD); //calls the populate function

}

-------------------------------
 You can copy blah blah from your populate() function ......

meow......

0
 
LVL 2

Author Comment

by:verbal11
ID: 9910056
Thank you everyone for all your help!!!
I have split the point in favor of dhyanesh because part 2 of my questions ended up being much more difficult than part 1. But thanks none the less to khkremer for answering part 1.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Handle Exceptions during instantiation 28 423
mixing C++ & C# in Vis Studio 2013 7 202
C++ Language error 28 246
Add values of each row in an array 3 64
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question