I need the user to input a book title then price, once that is done when the user searches for the book it displays the title and price that the user has made. Right now its showing weird numbers when displaying the price for book.

#include<iostream>
#include <iomanip>
using namespace std;

void pause();
void clrscr();
void skipBlanks();

void displayMenu(string &title, string allTitles[], double allPrices[], int totalRec);
void listRecords(string allTitles[], double allPrices[], int totalRec);
void displayRecord(string title, string allTitles[], double allPrices[], int totalRec);  
   
bool findTitlePrice(string allTitles[], double allPrices[], int totalRec, string title, double & price);
bool readRecord(string allTitles[], double allPrices[], string & title, int &totalRec); 

const int MAXSIZE=300;

int main()
{
  string allTitles [MAXSIZE] = {"Book 1", "Book 2"}, title;
  double allPrices [MAXSIZE] = {78.5,66.00}, price=0;
  int totalRec=2; 
  displayMenu (title, allTitles, allPrices, totalRec);
  return 0;
}

void pause()
{
 system("pause");
} 
       
void clrscr()
{
 system("cls");
}

void displayMenu(string &title, string allTitles[], double allPrices[], int totalRec)
{
  char option;
    cout << "Book Price (1705 John Smith) ver 1.0\n\n";
    cout << "MAIN MENU\n";
    cout << "0. Exit         1. Enter book     2. Find book\n";
    cout << "3. List all     4. Delete book    5. Update book\n\n";
    cout << "Your choice ->";
    cin >> option;
     
switch (option)
{
  case '0':
    cout << "Selected: Exit\n";
    break;
  case '1':
    cout << "Selected: Enter book\n\n";
    readRecord(allTitles, allPrices, title, totalRec);
    pause ();
    clrscr();
    cin.clear();
    displayMenu(title, allTitles, allPrices, totalRec);
    break;     
  case '2':
    cout << "Selected: Find book\n\n";  
    displayRecord(title, allTitles, allPrices, totalRec);
    pause();
    clrscr();
    cin.clear();
    displayMenu(title, allTitles, allPrices, totalRec);           
    break;
  case '3':
    cout << "Selected: List all\n\n";  
    listRecords(allTitles, allPrices, totalRec);
    pause();
    clrscr();
    cin.clear();
    displayMenu(title, allTitles, allPrices, totalRec);  
    break;
  case '4':
    cout << "Selected: Delete book\n\n"; 
    pause();
    clrscr();
    cin.clear();
    displayMenu(title, allTitles, allPrices, totalRec);
    break;
  case '5':
    cout << "Selected: Update book\n\n"; 
    pause();
    clrscr();
    cin.clear();
    displayMenu(title, allTitles, allPrices, totalRec);
    break;
  default:
    cout << "No option selected, please try again\n\n"; 
    pause();
    clrscr();
    cin.clear();
    displayMenu(title, allTitles, allPrices, totalRec);
   }
}

bool readRecord(string allTitles[], double allPrices[], string & title, int &totalRec)
{
    string temp;
    double price, myNumber=0, temp2;
    if(totalRec < MAXSIZE) 
   {
    cout << "\nEnter a Book Record:\nBook Title ->";
    skipBlanks();
    if(getline(cin,title)) 
   {
    cin.clear();
    if(findTitlePrice(allTitles, allPrices, totalRec, title, price)==0)
   {
    allTitles[totalRec]=title;
    cout << "Enter a price for "<< allTitles[totalRec] << ": ";
    skipBlanks();
    cin >> price;
          
    allPrices[totalRec]=temp2;
    totalRec++;
    cout <<"The Book Title: " << title << " costs: $" << price <<endl;   
    return true;
   }
    else
    cout << "Aborted: book entry failed." << endl;
   }
    else
    cout << "Aborted: book entry failed." << endl;
   }
    return false;
}

void displayRecord(string title, string allTitles[], double allPrices[], int totalRec)
{
    double price;
    cout << "Enter a Book title to search for: ";
    skipBlanks();
    getline(cin, title);
   
    if(findTitlePrice(allTitles, allPrices, totalRec, title, price)!=0)
   {
    cout << title << " costs $" << price << endl << endl;
   }
    else
   {
    cout << "Book Title Not Found\n\n";   
   }   
}

bool findTitlePrice(string allTitles[], double allPrices[], int totalRec, string title, double & price) 
{
  for(int i=0; i < totalRec; i++)
 {
  if(allTitles[i]==title)
 {
  price=allPrices[i];
  return true;
 }
}
  return false;
}

void listRecords(string allTitles[], double allPrices[], int totalRec) 
{
  if(totalRec <= 0)
    cout << "* No book titles are available.\n";
  else 
  {
    cout << "\n***  ALL AVAILABLE BOOK TITLES  ***\n\n"
         << "TITLE                           PRICE ($)\n"
         << "-----------------------------------------\n";
  for (int i=0;i<totalRec;i++)
  {
    cout << left << setw(32) << setprecision(4) << allTitles[i] << right << allPrices[i] << endl;
  }
}
    
}
void skipBlanks() {
  char ch;
  while((ch=cin.peek())==' ' || ch=='\t' || ch=='\n' || ch=='\r') cin.get();
}

Recommended Answers

All 5 Replies

Also how would I draw this into a structure diagram?

Problem #1:
Your DisplayMenu() function is doing way too much work. It should simply display the menu. The rest of the code in that function should be in another function.

Problem #2:
Your DisplayMenu() function calls itself (called recursion) which is dangerous in this type of program. Unless you must, never call a function from itself just to start it over. Exit the function and put the original call within a loop.

Problem#3:
If you don't want garbage in your price value, look at what you loaded into the allPrices array during your read.

Thank you for replying, I want to ask you have you tried running the code. If not please try and tell me what I can do to fix the issue of the long number displaying.

No I didn't, and I don't need to. Look at my point#3 and your code. Figure out what you are trying to do and what you did that didn't work properly. It's all right there in 5 lines of code. I already gave you a HUGE hint.

commented: WaltP IS a compiler ! +4

hmmm.... temp2???

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.