0

I have a problem I'm not sure where the error is occurring. I think it's when I used cin.ignore in my code.


My problem is occurring in case A. For some reason the program stops right after I enter my price information. I tried doing small codes to figure where my problem is coming from. Everything works fine except the price variable. I am not sure why?

int main()
{
    
    double amount;
    char choice;
    
    cout << "Please enter a starting amount in the store's cash registar:" << endl;
    cin >> amount;

    while(amount < 0)
    {
        cout << "Must enter a positive price.  Please re-enter:";
        cin >> amount; 
    }
    cout << endl;
    Store Bookstore(amount);

    Menu();
    do
    {
        cout << endl;
        cout << "Enter your menu selection:  ";
        cin >> choice;
        cin.ignore();
        
       // Respond to the  user's menu selection
       switch (choice)
       {
       case 'a':
       case 'A':
           cout << "Please enter the information in the following order: title, author, genre, price" << endl;
           char title[31], author[21], genre, gen;
           double price;
           cin.getline(title,31);
           cin.getline(author,21);
           cin.get(genre);
           gen = CheckGenre(genre);
           cin >> price;
           Bookstore.AddBook(title, author, gen, price);
           break;

       case 'F':
       case 'f':
           cout << "Please enter title or author to search for a book in the inventory list"<< endl;
           char selection[30];
           cin.getline(selection,31);
           Bookstore.FindBook(selection);
           break;
           
       case 'S:':
       case 's':
           cout << "Please type in the title of the book you would like to sell"<< endl;
           char sold[31];
           cin.getline(sold,31);
           Bookstore.SellBook(sold);
           break;
       
       case 'D':
       case 'd':
           Bookstore.Inventory();
           break;
       
       case 'G':
       case 'g':
           cout << "Please input a genre"<< endl;
           cout << "Please input a genre" << endl;
           char genre2;
           cin.get(genre2);
           CheckGenre(genre2);
           Bookstore.SumGenre(genre2);
           break;

       case 'M':
       case 'm':
           Menu();
           break;
       
       case 'X':
       case 'x':
           double money;
           money = Bookstore.GetAmount();
           cout << "$ " << setprecision(2) << fixed << right << money << endl;
           return 0;
           break;
       default:
           cout << "Incorrect selection, Please select a selection from the memu" << endl;
       }
    }
    while (isalpha(choice));


   return 0;
}                    // end main()

void Menu()
{
/* Declare your array and write the menu loop */
 
       // Display the menu and get a choice
  cout << "A:   Add a book to inventory" << endl; 
  cout << "F:   Find a book from inventory" << endl;  
  cout << "S:   Sell a book" << endl;  
  cout << "D:   Display the inventory list" << endl;  
  cout << "G:   Genre summary" << endl;  
  cout << "M:   Show this Menu" << endl;  
  cout << "X:   eXit the program" << endl;  
       
}
char CheckGenre(char genre)
{
    while ((genre != 'F') && (genre != 'f') && (genre != 'M') && (genre != 'm') && (genre != 'S') && (genre != 's') && (genre != 'C') && (genre != 'c'))
           {
               cout << "Invalid genre entry.  Please re-enter: ";
               cin >> genre;
           }
    return genre;
}

Also When did the Add function to work, The Inventory function would display that there is no bokk in the inventory.

Add Book Function

void Store::AddBook(const char* t, const char* a, char g, double p)
// Insert a new book into the direrctory.
{
    Genre gen;
    gen = ConvertGenre(g);
    if (sizeNow == maxSize)                        // If the directory is full, grow it.
        Increase();
    bookEntry[sizeNow++].Set(t, a, gen, p);        // read new book.
}

Increase Function

void Store::Increase()
{
    maxSize = sizeNow + 5;                // Determine a new size.
    Book* newBook = new Book[maxSize];    // Allocate a new array.
    
   for (int i = 0; i < sizeNow; i++)    // Copy each book into
    newBook[i] = bookEntry[i];            // the new array.
        
   delete [] bookEntry;                    // Remove the old array
   bookEntry = newBook;                    // Point old name to new array.

}

Inventory Function

void Store::Inventory()
{
   if (sizeNow == 0)
   {
    cout << "\nCurrent inventory is empty.\n";
    return;
   }
    
 // Display a header.
  cout << setw(30) << left <<"Title" << setw(20)<< left << "Author" << setw(11) << left << "Genre" << setw(7) << right << "Price" << endl << endl;
    
   for (int i = 0; i < sizeNow; i++)    // For each book,
    bookEntry[i].Display();                // send it to output
}
2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by Greywolf333
0

Hey,
Can you post your entire code, including all your custom data types and objects, so I can compile it? It'll just be quicker that way for me to see the problems.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.