tags:

views:

96

answers:

2

Is there something I need to do differently in setting a string in a setter method? This is my class:

class SavingsAccount
{
public:
    void setData();
    void printAccountData();
    double accountClosure() {return (accountClosurePenaltyPercent * accountBalance);}
private:
    int accountType;
    string ownerName;
    long ssn;
    double accountClosurePenaltyPercent;
    double accountBalance;
};

void SavingsAccount::setData()
{
    cout << "Input account type: \n";
    cin >> accountType;
    cout << "Input your name: \n";
    cin >> ownerName;
    cout << "Input your Social Security Number: \n";
    cin >> ssn;
    cout << "Input your account closure penalty percent: \n";
    cin >> accountClosurePenaltyPercent;
    cout << "Input your account balance: \n";
    cin >> accountBalance;
}


int main()
{
    SavingsAccount newAccount;
    newAccount.setData();
}
A: 

Don't call it "setter" :) ? It doesn't take any parameters and reads the data from stdin while for setters usual semantics is to take a parameter and assign it to appropriate fields. This one may be called "readData()"

Dmitry
A: 

Are you receiving any errors from your code or are you just asking for the best way to do it? Really you should refactor related code into their associated functions to keep the console inputs and outputs in the main method and pass the data to the function through parameters. But anyway without refactoring try this:

#include <sstream>
#include <iostream>

using namespace std;

class SavingsAccount
{
 public:
  void setData();
  void printAccountData();
  double accountClosure() {return (accountClosurePenaltyPercent*accountBalance);}
 private:
  int accountType;
  string ownerName;
  long ssn;
  double accountClosurePenaltyPercent;
  double accountBalance;
};

void SavingsAccount::setData()
{
 stringstream str;

 cout << "Input account type: \n";
 cin >> str;
 str >> accountType; // convert string to int

 cout << "Input your name: \n";
 cin >> str;
 str >> ownerName;

 cout << "Input your Social Security Number: \n";
 cin >> str;
 str >> ssn; // convert to long

 cout << "Input your account closure penalty percent: \n";
 cin >> str;
 str >> accountClosurePenaltyPercent; // convert to double

 cout << "Input your account closure penalty percent: \n";
 cin >> str;
 str >> accountClosurePenaltyPercent; // convert to double

 cout << "Input your account balance: \n";
 cin >> str;
 str >> accountBalance; // convert to double
}

int main()
{
 SavingsAccount newAccount;
 newAccount.setData();
}
Ambrosia
Not a compile error, but a run-time error that I am not familiar with. It is: Assignment8_1(491) malloc: *** error for object 0x100006240: pointer being freed was not allocated*** set a breakpoint in malloc_error_break to debugAbort traplogoutI guess I didn't know what if you could set strings the way I did it here like an int or double. Thanks!
Crystal
You might want to also think about using a constructor and destructor for the SavingsAccount class so that you can use the new and delete keywords and control the memory allocations. That could help you avoid your runtime error.
Ambrosia