我的程序有问题,我不知道该怎么做......
#ifndef CUSTOMER_H
#define CUSTOMER_H
#include <string>
class customer
{
public:
customer(void);
int getphoneNumber () const;
void setAll_1( int, int, int, int, double, int);
void setAll_2( std::string, std::string, std::string, std::string);
void Display();
~customer(void);
private:
int smartCardNumber;
int serviceNumber;
int billNumber;
int phoneNumber;
int customerID;
int accountNumber;
std::string firstName, lastName;
std::string address;
std::string password;
double balance;
};
#endif // !CUSTOMER_H
#include "stdafx.h"
#include "customer.h"
#include <string>
#include <iostream>
#include <iomanip>
using namespace std;
customer::customer(void)
{
accountNumber =0;
}
customer::~customer(void)
{
}
int customer::getAccountNumber()
{
return accountNumber;
}
void customer::setAll_1( int cusID, int smartCN, int serNum, int billNum,double balanc, int accountNum)
{
customerID = cusID; smartCardNumber = smartCN; serviceNumber =serNum;
billNumber = billNum; accountNumber = accountNum; balance = balanc;
}
void customer::setAll_2( string fS, string lS, string add, string pass)
{
firstName = fS; lastName = lS;
address = add; password = pass;
}
的main.cpp
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include "customer.h"
#include <iomanip>
using namespace std;
int getAccount( const char * const prompt );
int _tmain(int argc, _TCHAR* argv[])
{
fstream inCustomer ("1.txt", ios::in | ios::out );
if (!inCustomer)
cerr << "file not opened";
customer cus1;
int accountNumber = getAccount( "Enter new account number" );
inCustomer.seekg( ( accountNumber - 1 ) * sizeof( customer ) );
inCustomer.read( reinterpret_cast< char * >( &cus1 ), sizeof( customer ) );
if ( cus1.getAccountNumber() == 0 )
{
int smartCardNumber, serviceNumber, billNumber, phoneNumber;
int customerID;
string firstName, lastName, address, password;
double balance = 10000;
cout << setw( 20 ) <<" Customer ID: ";
cin >> customerID;
cout << setw( 20 ) << " Customer firstName & lastName : " << endl;
cin >> firstName >> lastName ;
cout << setw( 20 ) << " Customer address: " << endl;
cin >> address;
cout << setw( 20 ) << " Customer Service Number" << endl ;
cin >> serviceNumber;
cout << setw( 20 ) << " Customer Smart Card Number: " <<endl;
cin >> smartCardNumber;
cout<< setw( 20 ) << " Customer Bill Number : " <<endl;
cin >> billNumber;
cout << setw( 20 ) << " Customer Phone Number: " << endl ;
cin >> phoneNumber;
cus1.setAll_1(customerID, smartCardNumber, serviceNumber, billNumber, balance, accountNumber);
cus1.setAll_2(firstName, lastName, address, password);
inCustomer.seekp( ( accountNumber - 1 ) * sizeof( customer ) );
inCustomer.write( reinterpret_cast< const char * >( &cus1 ), sizeof( customer ) );
}
else
cout << "Account #" << accountNumber << " already contains information." << endl;
return 0;
}
int getAccount( const char * const prompt )
{
int accountNumber;
do
{
cout << prompt << " (1 - 100): ";
cin >> accountNumber;
} while ( accountNumber < 1 || accountNumber > 100 );
return accountNumber;
}
当我运行代码,返回此函数时,它向我显示:!(https://i.stack.imgur.com/UYn0l.png)![1]
那为什么会发生这种情况以及如何避免这种内存异常呢?谢谢