0

I'm supposed to write a main program that prompts the user for a name and initial balance. Your program should create two objects, one using the default constructor and another using the constructor that allows the user-entered name and balance to be used to initialize its object. Then prompt the user for one amount to be credited to an account and one amount to be debited. Use creditAccount to credit money to the object created with the parameterized constructor and debitAccount to subtract the debit amount from the object created with the default constructor. Use displayBalance to display the balance in both objects. Repeat the cycle above until the user enters an option to exit the program. I just don't understand where to go from here to have it actually build and use the class. Main.cpp

//9/17/2013
//
//I have read and understand the Lab Submittal Policy document on BB.

#include <iostream>
#include <sstream>
#include <string>
#include "Account.h"

using namespace std;

int main()
{
    char Redo;
    string CustomerName;

do
{
    float InitialBalance = -1;
    float balance1 = 0;
    float balance2 = 0;


    Account Account;
    Account.CreditAccount (balance1, InitialBalance);
    Account.DebitAccount (balance2, InitialBalance);
    Account.DisplayBalance (CustomerName, balance1, balance2);

    //Asks user if they want redo the program
    cout << "Would you like to redo the program?\n";
    cout << "Please enter Y or N: \n \n";
    cin >> Redo;
}while(Redo == 'Y' || Redo == 'y');


char exitchar; //Exit's the program.
cout << "\nPress any key and <enter> to exit the program.\n";
cin >> exitchar;

return 0;
}

Account.h

using namespace std;

class Account {
public:
    float balance1;
    float balance2;
    string CustomerName;
    float InitialBalance;
    float CreditAccount(float& balance1, float InitialBalance);
    float DebitAccount(float& balance2, float InitialBalance);
    float DisplayBalance(string CustomerName, float balance1, float balance2);
    Account ();


    Account(float balance)
    {
        SetInitialBalance(balance);
    }
    void Account::SetInitialBalance(float balance)
    {
        if(balance >= 0)
        {
            InitialBalance = balance;
        }
        else
               cout << "Error! Initial Balance cannot be less than 0." << endl;
    }
};

Account::Account(void)
{
string CustomerName;

cout << "Your Account Machine" << endl;
cout << "Please enter your last name." << endl;
cin >> CustomerName;
while(InitialBalance < 0)
{
cout << "Please enter your account balance. No Commas." << endl;
cin >> InitialBalance;
if(InitialBalance < 0)
    cout << "Error account balance must be positive." << endl;
}
}

float Account::CreditAccount(float& balance1, float InitialBalance)
    {
        float CreditInput = -1;
        while(CreditInput<0){
        cout << "Would you like to credit the account? Enter the amount you would like to credit." << endl;
        cin >> CreditInput;
        if (CreditInput<0)
            cout << "Credit must be positive." << endl;
        }
        balance1 = (CreditInput + InitialBalance);
        return balance1;
    }
float Account::DebitAccount(float& balance2, float InitialBalance)
    {
        float DebitInput = 0;
        while((InitialBalance - DebitInput)<0){
        cout << "Would you like to debit the account? Enter the amount you would like to debit." << endl;
        cin >> DebitInput;
        if((InitialBalance-DebitInput)<0)
            cout << "You cannot debit more than you have avalaible." << endl;
        }
        balance2 = (InitialBalance - DebitInput);
        if( DebitInput > InitialBalance)
        {
        cout << "Debit amount exceeds account balance." << endl;
        return 0;
        }
        else 
            return balance2;
    }
float Account::DisplayBalance(string CustomerName, float balance1, float balance2)
    {
        cout << "Customer Name: " << CustomerName << endl;
        cout << "Account Balance for credited account: " << balance1 << endl;
        cout << "Account Balance for debited account: " << balance2 << endl;
        return 0;
    }

Updated I updated my code and I just have one question. I would like to know why my program is not reading in any CustomerName or InitialBalance. Everything else works to my liking.

Thank you again!

Brett Holmes
  • 397
  • 5
  • 20
  • 1
    I would also add `homework` tag to your post. – lapk Sep 18 '13 at 01:07
  • It wont let me add that tag sadly. – Brett Holmes Sep 18 '13 at 01:10
  • Watch out, `Account balance();` declares a function. If you want to create an object and let its default constructor be called, either use `Account my_object_name;` or C++11's `Account my_object_name{};`. If you want to create an object and pass something to its constructor, use `Account my_object_name(data);`, e.g. `Account my_account_name(balance);`. – dyp Sep 18 '13 at 01:11
  • 1
    Homework tag has long been depricated @PetrBudnik. – ChiefTwoPencils Sep 18 '13 at 01:11
  • Are you required to use a make file? Are you using an IDE? – ChiefTwoPencils Sep 18 '13 at 01:14
  • 1
    @BobbyDigital Ah, you are right. Reading on the rationale now ;)... – lapk Sep 18 '13 at 01:16
  • You need to put your function definitions in the other .cpp file. The one you haven't created yet. The .h is typically used to lay out your prototypes/signatures. That's where you'd need to prefix with Account:: on all such functions. – ChiefTwoPencils Sep 18 '13 at 01:17
  • Required to use a .h file and account.cpp and I don't think I'm using an IDE. I'm using Visual Studio 10 on Parallels. – Brett Holmes Sep 18 '13 at 01:18
  • I added the rest of the homework question if that will help anything. – Brett Holmes Sep 18 '13 at 01:22
  • Wait... your question is "how do I divide a class into a source file and a header file, then get it all to compile and run?". You should try that with `HelloWorld`, not with this complicated example. – Beta Sep 18 '13 at 01:36
  • There are several major issues besides a bunch of minor issues (that need some more experience/explanation to see and avoid). The majors issues include: `Account::Account(float balance)` inside class `Account` should just be `Account(float balance)`. The member functions of `Account` with a `float` return type need to return a value. As I said before, `Account balance();` should be something like `Account my_account(balance);`. After changing those things, it should compile. – dyp Sep 18 '13 at 01:38
  • I changed all of those and I got this error. MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup \\psf\Home\Documents\Visual Studio 2010\Projects\CSCI112\Lab2Project\Debug\Lab2Project.exe : fatal error LNK1120: 1 unresolved externals – Brett Holmes Sep 18 '13 at 01:49
  • 1
    @BrettHolmes The solution to this problem can easily be found on SO by searching. For example, try [this](http://stackoverflow.com/q/6626397/420683). – dyp Sep 18 '13 at 01:55
  • @DyP Thanks so much! I did everything you said and it finally compiled. Thats just a huge relief so far. I just get really confused with constructors and classes because I just learned them in class a week ago. – Brett Holmes Sep 18 '13 at 01:59
  • @PetrBudnik, the `homework` tag has been determined to be unnecessary and has probably been banned. Do a search on meta.stackoverflow.com to see the reasoning. – Mark Ransom Sep 18 '13 at 02:50
  • @BrettHolmes: Line breaks inserted every three or four sentences. They make text easier to read. – Ed S. Sep 18 '13 at 03:06
  • @eds. Oh ok I will definitely do that next time. Sorta new to this. I don't understand though why my teacher wants the declarations and source in seperate files from my main cpp. – Brett Holmes Sep 18 '13 at 03:20
  • 2
    @BrettHolmes: Because that's canonical in C and C++. It's a form of encapsulation. You declare your structures and data in a header file. That's the public interface of your library/whatever. You define it in your implementation file(s). It also has the advantage of requiring users of your code to only include the bare minimum in theirs (i.e., the header). This avoids clashes with other headers. For example, you may want to add a `using some_namespace_or_type` in your implementation file for convenience. You never do that in a header because you pollute the global namespace. – Ed S. Sep 18 '13 at 03:31
  • I don't know how to use using some_namespace_or_type. I don't think I have gotten that far. My code works great now but I need to have it use the object more or even at all and thats really killing me right now. – Brett Holmes Sep 18 '13 at 04:51
  • @BrettHolmes Ed S. is referring to your `using namespace std;` in the header file `Account.h`. `using namespace` shouldn't occur at global scope in header files. "I need to have it use the object more or even at all" Try to remove all user communication from the class (all `cin` and `cout`). For the constructor (and functions called in the constructor), that's unfortunately not possible unless you use exceptions (which I doubt you know already of). The class `Account` is there to *manage* an account, not to be the bank teller. – dyp Sep 18 '13 at 04:58
  • @DyP I'll take the using namespace std; out of the header file. I also am just going put the member function prototypes in the header file and the functions in the Account.cpp file I will be making. I'll change the main.cpp to call the member functions. – Brett Holmes Sep 18 '13 at 18:43

1 Answers1

1

I researched constructors, classes, and separate files. I learned how to make an object, and call the member functions from the other classes that were on different files. I should have done more research about this before I asked this question.

Brett Holmes
  • 397
  • 5
  • 20