And a few steps up from using parallel arrays to hold your data ...
(Illustrates using sstream to ease data processing.)
// structBook.cpp // using struct Book (instead of parallel arrays)
// this version 2010-06-01
#include <iostream>
#include <sstream>
#include <iomanip>
using namespace std;
const int MAX_RECORDS = 5;
struct Book
{
string title;
float price;
int quantity;
};
void showBook( const Book& );
void showBooks( Book[], int );
void takeInStock( Book[], int& );
void editBook( Book[], int&, int );
int main()
{
Book store[MAX_RECORDS] = {{"nothing",0,0},{"Book 1",77.5,4},{"Book 2",66,3}};
int count = 3;
if(count > 0) showBooks( store, count );
else cout << "Store is empty\n";
cout << endl;
takeInStock( store, count );
if(count > 0) showBooks( store, count );
else cout << "Store is empty\n";
for( ; ; )
{
cout << "\nEnter index of book to edit: " << flush;
string tmp;
getline( cin, tmp );
int i;
if( istringstream(tmp) >> i )
{
editBook( store, count, i );
}
else
{
cout << "Invalid index of " << tmp << " was entered ... Try again.\n";
continue;
}
cout << "More y/n ? " << flush;
int reply = cin.get();
cin.sync();
if( reply == 'n' || reply == 'N' ) break;
}
if(count > 0) showBooks( store, count );
else cout << "Store is empty\n";
cout << "\nPress 'Enter' to exit ... " << flush;
cin.get();
}
void showBook( const Book& b)
{
cout << setw(15) << left << b.title
<< setw(10) << right << b.price
<< fixed << setprecision(2)
<< setw(16) << right << b.quantity << endl;
}
void showBooks( Book st[], int size )
{
cout << "* * * * ALL AVAILABLE BOOK TITLES * * * *\n\n"
<< setw(15) << left << "Title"
<< setw(10) << right << "Price"
<< setw(16) << right << "Quanity" << endl
<< setw(15) << left << "====="
<< setw(10) << right << "====="
<< setw(16) << right << "=======" << endl;
for( int i = 0; i < size; ++i ) showBook( st[i] );
}
void takeInStock( Book st[], int& size )
{
for( ; size < MAX_RECORDS ; )
{
cout << "Enter new title: " << flush;
getline( cin, st[size].title );
cout << "Enter price: " << flush;
string tmp;
getline( cin, tmp );
if( istringstream( tmp ) >> st[size].price )
{
// pass ...
}
else
{
cout << "Price must be a number ... try again ... ";
continue;
}
cout << "Enter quanity: " << flush;
getline( cin, tmp );
if( istringstream( tmp ) >> st[size].quantity )
{
++size;
}
else
{
cout << "Price must be a number ... try again ... ";
continue;
}
cout << "More y/n ? " << flush;
int reply = cin.get();
cin.sync();
if( reply == 'n' || reply == 'N' ) break;
}
if( size == MAX_RECORDS ) cout << "Store is full." << endl;
}
void editBook( Book st[], int& size, int index )
{
int reply;
string tmp, msg = "edited";
if( index > size-1 || index < 0 )
{
cout << index << " NOT in valid range of 0.." << size-1 << endl;
if( index < MAX_RECORDS && size < MAX_RECORDS )
{
cout << "Add to end anyways ... y/n ? " << flush;
reply = cin.get();
cin.sync();
if( reply == 'y' || reply == 'Y' )
{
index = size++;
msg = "new";
goto addNew;
}
}
// else ...
return;
}
// else
showBook( st[index] );
cout << "ok to edit y/n ? " << flush;
reply = cin.get();
cin.sync();
if( !(reply == 'y' || reply == 'Y' ) ) return;
addNew:
// else ...
for( ; ; )
{
cout << "Enter " << msg << " title: " << flush;
getline( cin, st[index].title );
cout << "Enter " << msg << " price: " << flush;
getline( cin, tmp );
if( istringstream( tmp ) >> st[index].price )
{ /* pass ... */ }
else
{
cout << "Price must be a number ... try again ... ";
continue;
}
cout << "Enter " << msg << " quanity: " << flush;
getline( cin, tmp );
if( istringstream( tmp ) >> st[index].quantity )
{ /* pass ... */ }
else
{
cout << "Quanity must be an integer ... try again ... ";
continue;
}
cout << "Ok y/n ? " << flush;
reply = cin.get();
cin.sync();
if( reply == 'y' || reply == 'Y' )
{
if( index == MAX_RECORDS ) cout << "Store is full ... " << endl;
break;
}
}
}
// structBookStore.cpp // using struct Book and struct Store
// this version 2010-06-01
#include <iostream>
#include <sstream>
#include <iomanip>
using namespace std;
const int MAX_RECORDS = 5;
struct Book
{
string title;
float price;
int quantity;
};
struct Store
{
Book bStore[MAX_RECORDS];
int num_recs;
Store() : num_recs(0) {} // default constructor ... initial num_recs to 0
};
void showBook( const Book& );
void showStore( const Store& );
void takeInStock( Store& );
void editBook( Store&, int );
int main()
{
Book shipment1[] = {{"nothing",0,0},{"Book 1",77.5,4},{"Book 2",66,3}};
Store st; // calls default constructor to initial st.num_recs to zero (0)
int i = 0;
while( st.num_recs < MAX_RECORDS && i < int(sizeof shipment1/sizeof shipment1[0]) )
{
st.bStore[st.num_recs++] = shipment1[i++];
}
showStore( st );
cout << endl;
takeInStock( st );
showStore( st );
for( ; ; )
{
cout << "\nEnter index of book to edit: " << flush;
string tmp;
getline( cin, tmp );
int i;
if( istringstream(tmp) >> i )
{
editBook( st, i );
}
else
{
cout << "Invalid index of " << tmp << " was entered ... Try again.\n";
continue;
}
cout << "More y/n ? " << flush;
int reply = cin.get();
cin.sync();
if( reply == 'n' || reply == 'N' ) break;
}
showStore( st );
cout << "\nPress 'Enter' to exit ... " << flush;
cin.get();
}
void showBook( const Book& b)
{
cout << setw(15) << left << b.title
<< setw(10) << right << b.price
<< fixed << setprecision(2)
<< setw(16) << right << b.quantity << endl;
}
void showStore( const Store& st)
{
if( st.num_recs )
{
cout << " * * * * ALL AVAILABLE BOOK TITLES * * * *\n\n"
<< " " << setw(15) << left << "Title"
<< setw(10) << right << "Price"
<< setw(16) << right << "Quanity" << endl
<< " " << setw(15) << left << "====="
<< setw(10) << right << "====="
<< setw(16) << right << "=======" << endl;
for( int i = 0; i < st.num_recs; ++i )
{
cout << "<" << setw(2) << setfill('0') << i << ">"
<< setfill(' ') << " ";
showBook( st.bStore[i] );
}
}
else cout << "Store is empty\n";
}
void takeInStock( Store& st )
{
for( ; st.num_recs < MAX_RECORDS ; )
{
cout << "Enter new title: " << flush;
getline( cin, st.bStore[st.num_recs].title );
cout << "Enter price: " << flush;
string tmp;
getline( cin, tmp );
if( istringstream( tmp ) >> st.bStore[st.num_recs].price )
{
// pass ...
}
else
{
cout << "Price must be a number ... try again ... ";
continue;
}
cout << "Enter quanity: " << flush;
getline( cin, tmp );
if( istringstream( tmp ) >> st.bStore[st.num_recs].quantity )
{
++st.num_recs;
}
else
{
cout << "Price must be a number ... try again ... ";
continue;
}
cout << "More y/n ? " << flush;
int reply = cin.get();
cin.sync();
if( reply == 'n' || reply == 'N' ) break;
}
if( st.num_recs == MAX_RECORDS ) cout << "Store is full." << endl;
}
void editBook( Store& st, int index )
{
int reply;
string tmp, msg = "edited";
if( index > st.num_recs-1 || index < 0 )
{
cout << index << " NOT in valid range of 0.." << st.num_recs-1 << endl;
if( index < MAX_RECORDS && st.num_recs < MAX_RECORDS )
{
cout << "Add to end anyways ... y/n ? " << flush;
reply = cin.get();
cin.sync();
if( reply == 'y' || reply == 'Y' )
{
index = st.num_recs++;
msg = "new";
goto addNew;
}
}
// else ...
return;
}
// else
showBook( st.bStore[index] );
cout << "ok to edit y/n ? " << flush;
reply = cin.get();
cin.sync();
if( !(reply == 'y' || reply == 'Y' ) ) return;
addNew:
// else ...
for( ; ; )
{
cout << "Enter " << msg << " title: " << flush;
getline( cin, st.bStore[index].title );
cout << "Enter " << msg << " price: " << flush;
getline( cin, tmp );
if( istringstream( tmp ) >> st.bStore[index].price )
{ /* pass ... */ }
else
{
cout << "Price must be a number ... try again ... ";
continue;
}
cout << "Enter " << msg << " quanity: " << flush;
getline( cin, tmp );
if( istringstream( tmp ) >> st.bStore[index].quantity )
{ /* pass ... */ }
else
{
cout << "Quanity must be an integer ... try again ... ";
continue;
}
cout << "Ok y/n ? " << flush;
reply = cin.get();
cin.sync();
if( reply == 'y' || reply == 'Y' )
{
if( index == MAX_RECORDS ) cout << "Store is full ... " << endl;
break;
}
}
}