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();
}