c++ - Need to accept a response from user to execute program -


i'm working on programming homework college c++ class. question states program needs able open , edit list of hardware tools file. trouble im having @ beginning says "cout << "should file initialized (y or n): "; , when run program , type either y or n program doesnt respond. great

#include <iostream> #include <iomanip> #include <fstream> #include <string>  using std::cout; using std::cin; using std::ios; using std::fstream; using std::setw; using std::setprecision; using std::cerr;   void getfile( fstream & ); void input( fstream & ); void listtools( fstream & ); void updaterecord( fstream & ); void insertrecord( fstream & ); void deleterecord( fstream & ); int instructions( void );  const int length = 30;  struct data {     int partnumber;     char toolname[ length ];     int instock;     double unitprice; };  int main() {     int choice;       char response;     fstream file( "hardware.dat", ios::in | ios::out );     void ( *f[] )( fstream & ) = { listtools, updaterecord, insertrecord,         deleterecord };      cout << "should file initialized (y or n): ";     cin >> response;  } void getfile( fstream &fref ) {     data blankitem = { -1, "", 0, 0.0 };      ( int = 0; < 100; ++i )         fref.write( (char * )( &blankitem ), sizeof( data ) ); }  void input( fstream &fref ) {     data temp;     cout << "enter partnumber (0 - 99, -1 end input): ";     cin >> temp.partnumber;      {         while ( temp.partnumber != -1 )             cout << "enter tool name: ";         cin.ignore();         cin.get( temp.toolname, length );         cout << "enter quantity , price: ";         cin >> temp.instock >> temp.unitprice;         fref.seekp( ( temp.partnumber ) * sizeof( data ) );         fref.write( ( char * )( &temp ), sizeof( data ) );         cin >> temp.partnumber;     } }  int instructions( void ) {     int choice;      cout << "\nenter choice:\n1 list tools."         << "\n2 update record.\n3 insert record."         << "\n4 delete record.\n5 end program.\n";          {         cout << "? ";         cin >> choice;     }      while ( choice < 1 || choice > 5 );      return choice; }  void listtools( fstream &fref ) {     data temp;      cout << setw( 7 ) << "record#" << " " << setiosflags( ios::left )         << setw( 30 ) << "tool name" << resetiosflags( ios::left )         << setw( 13 ) << "quantity" << setw( 10 ) << "cost\n";      ( int count = 0; count < 100 && !fref.eof(); ++count )     {         fref.seekg( count * sizeof( data ) );         fref.read( ( char *)( &temp ), sizeof( data ) );          if ( temp.partnumber >= 0 && temp.partnumber < 100 )         {             cout.setf( ios::fixed | ios::showpoint );             cout << setw( 7 ) << temp.partnumber << " "                 << setiosflags( ios::left ) << setw( 30 ) << temp.toolname                 << resetiosflags( ios::left ) << setw( 13 ) << temp.instock                 << setprecision( 2 ) << setw( 10 ) << temp.unitprice << '\n';         }     } }  void updaterecord( fstream &fref ) {     data temp;     int part;      cout << "enter part number update: ";     cin >> part;     fref.seekg( part * sizeof( data ) );     fref.read( ( char *)( &temp ), sizeof( data ) );      if ( temp.partnumber != -1 )     {         cout << setw( 7 ) << "record#" << " " << setiosflags( ios::left )             << setw( 30 ) << "tool name" << resetiosflags( ios::left )             << setw( 13 ) << "quantity" << setw( 10 ) << "cost\n";          cout.setf( ios::fixed | ios::showpoint );         cout << setw( 7 ) << temp.partnumber << " "             << setiosflags( ios::left ) << setw( 30 ) << temp.toolname             << resetiosflags( ios::left ) << setw( 13 ) << temp.instock             << setprecision( 2 ) << setw( 10 ) << temp.unitprice << '\n'             << "enter tool name: ";          cin.ignore();         cin.get( temp.toolname, length );         cout << "enter quantity , price: ";         cin >> temp.instock >> temp.unitprice;          fref.seekp( ( temp.partnumber ) * sizeof( data ) );         fref.write( ( char *) ( &temp ), sizeof( data ) );     }     else         cerr << "cannot update. record empty.\n"; }  void insertrecord( fstream &fref ) {     data temp;     int part;      cout << "enter partnumber insertion: ";     cin >> part;     fref.seekg( ( part ) * sizeof( data ) );     fref.read( ( char * ) ( &temp ), sizeof( data ) );      if ( temp.partnumber == -1 )     {         temp.partnumber = part;         cout << "enter tool name: ";         cin.ignore();         cin.get( temp.toolname, length );         cout << "enter quantity , price: ";         cin >> temp.instock >> temp.unitprice;          fref.seekp( ( temp.partnumber ) * sizeof( data ) );         fref.write( ( char *)( &temp ), sizeof( data ) );     }     else         cerr << "cannot insert. record contains information.\n"; }  void deleterecord( fstream &fref ) {     data blankitem = { -1, "", 0, 0.0 }, temp;     int part;      cout << "enter partnumber deletion: ";     cin >> part;      fref.seekg( part * sizeof( data ) );     fref.read( ( char *)( &temp ), sizeof( data ) );      if ( temp.partnumber != -1 )     {         fref.seekp( part * sizeof( data ) );         fref.write( ( char * )( &blankitem ), sizeof( data ) );         cout << "record deleted.\n";     }     else         cerr << "cannot delete. record empty.\n"; } } 

here file need open , use

hardware.dat (file)  record:  tool name:    quantity:   cost: 3        sandpaper        07       $57.98 17       screws           76       $11.99 24       sledge hammer    21       $11.00 39       lawn mower       03       $79.50 56       hose             18       $99.99 68       screwdriver      106      $06.99 77       hammer           11       $21.50 83       wrench           34       $07.50 

you have following:

int main() {     // other stuff     cout << "should file initialized (y or n): ";     cin >> response;     // end of main() } 

after read response program stops. not calling other function. should expect program finish, , that's what's happening.


Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -