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

Need to accept a response from user to execute program

本文关键字:响应 执行程序 用户      更新时间:2023-10-16

我目前正在为我的大学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 ) << "Costn";
    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 ) << "Costn";
        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

您有以下各项:

int main()
{
    // other stuff
    cout << "Should the file be initialized (Y or N): ";
    cin >> response;
    // end of main()
}

读取response后,程序将停止。您没有调用任何其他函数。所以你应该期待你的程序完成,这就是正在发生的事情。