我有作業,但它不工作。最好的我可以猜測,爲什麼它不工作是一個自動斷點vs2013,它顯示在輸出框中。使用類 - 無效的分配大小
HEAP[hw2_ccc.exe]: Invalid allocation size - 10 (exceeded fffdefff)
First-chance exception at 0x000007FEFD9D940D in hw2_ccc.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x000000000012F350.
HEAP[hw2_ccc.exe]: Invalid allocation size - 1 (exceeded fffdefff)
First-chance exception at 0x000007FEFD9D940D in hw2_ccc.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x000000000012CE40.
First-chance exception at 0x000007FEFD9D940D in hw2_ccc.exe: Microsoft C++ exception: [rethrow] at memory location 0x0000000000000000.
First-chance exception at 0x000007FEFD9D940D in hw2_ccc.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x000000000012CE40.
A buffer overrun has occurred in hw2_ccc.exe which has corrupted the program's internal state.
這裏是我的代碼:
bankAccount.h
#ifndef H_bankAccount
#define H_bankAccount
#include <string>
#include <iostream>
using namespace std;
class bankAccount
{
public:
void setAccountName(string holderName);
void setAccountNumber(int accountNumber);
void setInterestRate(double interestRate);
void setAccountType(string accountType);
void depositBalance(double deposit);
void withdrawBalance(double withdraw);
void printAccountInformation();
void newAccount(string holderName, int accountNumber, string accountType, double interestRate);
string getAccountName();
bankAccount();
~bankAccount();
private:
string holderName;
int accountNumber;
string accountType;
double balance;
double interestRate;
};
#endif
bankAccount.cpp
#include "bankAccount.h"
using namespace std;
bankAccount::bankAccount()
{
}
bankAccount::~bankAccount()
{
}
void bankAccount::newAccount(string holderName, int accountNumber, string accountType, double interestRate)
{
setAccountName(holderName);
setAccountNumber(accountNumber);
setAccountType(accountType);
setInterestRate(interestRate);
}
void bankAccount::setAccountName(string holderName)
{
bankAccount::holderName = holderName;
}
void bankAccount::setAccountNumber(int accountNumber)
{
bankAccount::accountNumber = accountNumber;
}
void bankAccount::setAccountType(string accountType)
{
bankAccount::accountType = accountType;
}
void bankAccount::setInterestRate(double interestRate)
{
bankAccount::interestRate = interestRate;
}
void bankAccount::depositBalance(double deposit)
{
bankAccount::balance += deposit;
}
void bankAccount::withdrawBalance(double withdraw)
{
bankAccount::balance -= withdraw;
}
void bankAccount::printAccountInformation()
{
cout << "Account Name: " << bankAccount::holderName << endl;
cout << "Account Type: " << bankAccount::accountType << endl;
cout << "Account Number: " << bankAccount::accountNumber << endl;
cout << "Account Interest Rate: " << bankAccount::interestRate << endl;
cout << "Account Balance :" << bankAccount::balance << endl;
}
string bankAccount::getAccountName()
{
return holderName;
}
的main.cpp
#include "bankAccount.h"
using namespace std;
int randAccountNum();
string randAccountType();
int interestRate(string accountType);
bool printAccount(bankAccount accounts[10]);
int main()
{
bankAccount account[10];
string accountNames[10] = { "Bob", "Jack", "Billy", "James", "Kathy", "John", "Jenny", "Penny", "Sue", "Louis" };
string accountType;
int accountNumber;
bool prAcc = true;
for (int i = 0; i < sizeof(account); i++)
{
accountType = randAccountType();
accountNumber = randAccountNum();
account[i].newAccount(accountNames[i], accountNumber, accountType, interestRate(accountType));
}
while (prAcc)
{
prAcc = printAccount(account);
}
system("pause");
return 0;
}
int randAccountNum()
{
int num = rand() % 1000 + 1;
return num;
}
string randAccountType()
{
string str;
int num = rand() % 2 + 1;
if (num = 1)
{
str = "Savings";
}
else {
str = "Checking";
}
return str;
}
int interestRate(string accountType)
{
int ir;
if (accountType == "Savings")
{
ir = 2;
}
else {
ir = 4;
}
return ir;
}
bool printAccount(bankAccount accounts[10])
{
string cont;
bool contL = true;
string accountName;
cout << "Enter account name: ";
cin >> accountName;
cout << endl;
for (int i = 0; i < sizeof(accounts); i++)
{
if (accounts[i].getAccountName() == accountName)
{
accounts[i].printAccountInformation();
}
}
while (contL)
{
cout << "Enter another name? (Yes/No): ";
cin >> cont;
if (cont == "Yes")
return true;
else if (cont == "No")
return false;
else
cout << "Invalid. Please enter Yes or No" << endl;
}
}
ekhm ....'對(INT I = 0; I <的sizeof(賬戶);我++)' – 2014-09-03 17:13:47
爲什麼不使用'的std :: VECTOR'。 ..? – crashmstr 2014-09-03 17:31:05