我正在为我的大学C ++课程编写一些编程作业。问题表明程序需要能够从文件中打开和编辑硬件工具列表。我遇到的唯一麻烦就是在开头说“cout<<”文件应该被初始化(Y或N):“;当你运行程序并输入Y或N时,程序没有响应。帮助会很棒
#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 the file be initialized (Y or N): ";
cin >> response;
}
void getFile( fstream &fRef )
{
Data blankItem = { -1, "", 0, 0.0 };
for ( int i = 0; i < 100; ++i )
fRef.write( (char * )( &blankItem ), sizeof( Data ) );
}
void input( fstream &fRef )
{
Data temp;
cout << "Enter the partnumber (0 - 99, -1 to end input): ";
cin >> temp.partNumber;
{
while ( temp.partNumber != -1 )
cout << "Enter the tool name: ";
cin.ignore();
cin.get( temp.toolName, LENGTH );
cout << "Enter quantity and 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 a choice:\n1 List all tools."
<< "\n2 Update record.\n3 Insert record."
<< "\n4 Delete record.\n5 End program.\n";
do
{
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";
for ( 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 the part number for 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 the tool name: ";
cin.ignore();
cin.get( temp.toolName, LENGTH );
cout << "Enter quantity and price: ";
cin >> temp.inStock >> temp.unitPrice;
fRef.seekp( ( temp.partNumber ) * sizeof( Data ) );
fRef.write( ( char *) ( &temp ), sizeof( Data ) );
}
else
cerr << "Cannot update. The record is empty.\n";
}
void insertRecord( fstream &fRef )
{
Data temp;
int part;
cout << "Enter the partnumber for insertion: ";
cin >> part;
fRef.seekg( ( part ) * sizeof( Data ) );
fRef.read( ( char * ) ( &temp ), sizeof( Data ) );
if ( temp.partNumber == -1 )
{
temp.partNumber = part;
cout << "Enter the tool name: ";
cin.ignore();
cin.get( temp.toolName, LENGTH );
cout << "Enter quantity and price: ";
cin >> temp.inStock >> temp.unitPrice;
fRef.seekp( ( temp.partNumber ) * sizeof( Data ) );
fRef.write( ( char *)( &temp ), sizeof( Data ) );
}
else
cerr << "Cannot insert. The record contains information.\n";
}
void deleteRecord( fstream &fRef )
{
Data blankItem = { -1, "", 0, 0.0 }, temp;
int part;
cout << "Enter the partnumber for 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. The record is empty.\n";
}
}
这是我需要打开并使用的文件
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
答案 0 :(得分:4)
您有以下内容:
int main()
{
// other stuff
cout << "Should the file be initialized (Y or N): ";
cin >> response;
// end of main()
}
阅读response
后,程序停止。你没有调用任何其他功能。因此,您应该期望您的计划完成,以及发生了什么。