gpt4 book ai didi

c++ - 需要接受用户的响应才能执行程序

转载 作者:行者123 更新时间:2023-11-27 22:58:40 25 4
gpt4 key购买 nike

<分区>

我目前正在为我的大学 C++ 类(class)做一些编程作业。问题指出程序需要能够从文件中打开和编辑硬件工具列表。我遇到的唯一麻烦是在开始时它说“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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com