I'm taking my first class in c++ this semester, so i'm a newb. The only other programming experience i have is in python. My teacher gave me this assignment and i don't even know where to start. I don't want anyone to post any code, but a few tips on what implementation files i need to create and what they should contain would be great.

Here's the assignment:

Your header file has been given to you, so you only need to create the implementation and main files. However, you may have to make some changes to the supplied file. Your program should work like the following:

Please make a selection:
1) Display Current Account Balance
2) Deposit Money
3) Withdraw Money
4) Exit

< user enters 1 >
You have $2000.00 in your account.


Please make a selection:
1) Display Current Account Balance
2) Deposit Money
3) Withdraw Money
4) Exit

< user enters 2 >
How much would you like to deposit? < user enters 300.57 >

You have deposited $300.57, and you now have $2300.57 in your account.


Please make a selection:
1) Display Current Account Balance
2) Deposit Money
3) Withdraw Money
4) Exit

< user enters 3 >
How much would you like to withdraw? < user enters 4500 >

Sorry, you don’t have that much in your account.


Please make a selection:
1) Display Current Account Balance
2) Deposit Money
3) Withdraw Money
4) Exit

< user enters 3 >
How much would you like to withdraw? < user enters 450 >
You have withdrawn $450.00, and you now have $1850.57 in your account.

Recommended Answers

All 10 Replies

So you say you know python. Why not make it in Python first and then translate it to C++. This assignment only requires the most basic function of C++ and the structure of the program in C++ wouldn't differ very much from the structure in C++.

To answer your primary question, you'll need 3 files:
1.) The provided header file (a *.h file)
2.) The class implementation file (a *.cpp) file.
3.) The main application file (another *.cpp) file.

The structure of the program will be the same as any other you should have done to this point. You just need to adapt your header usage (#includes) to the one provided by your instructor and be sure you fill in the implementation file correctly.

To answer your primary question, you'll need 3 files:
1.) The provided header file (a *.h file)
2.) The class implementation file (a *.cpp) file.
3.) The main application file (another *.cpp) file.

The structure of the program will be the same as any other you should have done to this point. You just need to adapt your header usage (#includes) to the one provided by your instructor and be sure you fill in the implementation file correctly.

Thanks for the reply,

What i was really wanting/needing to know was:
1) Do i need to make functions for all the private and public members of the header file in a separate .cpp file or just the public members?
2) What's the best way of implementing the menu?

Here is the code that our instuctor gave us as our header file:

#ifndef ACCOUNT		// this redundancy checking statement looks to see if this section has 
#define ACCOUNT		// already been included.  if it has already been included, it is ignored.

#include <iostream>
#include <string>
using namespace std;

class account {
	private:
		//private members
		string	ownerFirstName;
		string	ownerLastName;
		float		balance;

		//private methods

	public:
		//constructor
		account(string, string, float);
		void depositFunds(float);
		void withdrawFunds(float);
		float getBalance();
		
};

#endif		//this is the end of the redundancy checking statement

Please use [code=syntax] ... code tags ... [/code] they make code contained in your posts easier to read. niek_e has added them for you so far, but you need to start doing that.

RE: Q1.)
Any user-defined function that exists as a member of your class must be implemented (i.e. deposit(), withdraw(), checkBalance()) in the *.cpp file by you. I see 4 member functions that need implementation. They are all public. As far as implementation is concerned, you would not treat them any differently if they were private.

RE: Q2.)
Personally, I would create an int variable (I'll call it 'selection') then get the user's numeric selection. After that use some sort of branching control statement (such as switch or if...then...else) to determine what was entered and respond accordingly to what you determine the input was.

Thanks Fbody,

I'm gonna get to work on it tomorrow and i'll let you know how it works out, but i think i've got the general idea now.

Please use [code=syntax] ... code tags ... [/code] they make code contained in your posts easier to read. niek_e has added them for you so far, but you need to start doing that.

RE: Q1.)
Any user-defined function that exists as a member of your class must be implemented (i.e. deposit(), withdraw(), checkBalance()) in the *.cpp file by you. I see 4 member functions that need implementation. They are all public. As far as implementation is concerned, you would not treat them any differently if they were private.

RE: Q2.)
Personally, I would create an int variable (I'll call it 'selection') then get the user's numeric selection. After that use some sort of branching control statement (such as switch or if...then...else) to determine what was entered and respond accordingly to what you determine the input was.

Okay, so far this is what i have:

#include <iostream>
#include <string>
#include "account.h"
using namespace std;

int main(){

	account a("Blake", "Hunter", 5000.00);		// default account
	int selection = 0;
	while ( selection != 1){		// end program when terminate = 1

		float amt = 0;
		float amnt = 0;
		cout << "Please make a selection:\n";					//Display user menu
		cout << "1)  Display Current Account Balance\n";
		cout << "2)  Deposit Money\n";
		cout << "3)  Withdraw Money\n";
		cout << "4)  Exit\n\n\n";
		cin >> selection;

		if (selection == 1){ // view account
			cout << "You have " << a.getBalance() << " dollars in your account.\n";
			system("pause");
		}

		else if(selection == 2){ // deposit
			cout << "How much would you like to deposit: ";
			cin >> amt;
			cout << "You've successfully deposited: $" << amt << " \nYour new balance is: $" << a.getBalance() + amt << "\n\n";
			system("pause");
		}

		else if(selection == 3){ // withdraw
			cout << "How much would you like to withdraw: ";
			cin >> amnt;

			if (a.getBalance() >= amnt){ // restricts user from withdrawing more than they have in their account
				cout << "You've successfully withdrawn: $" << amnt << "\nYour new balance is:" << a.getBalance() - amnt << " \n\n";
				system("pause");
			}

			else{
				cout << "You don't have enough money for that! If\n";
				system("pause");
			}
		}

		else if(selection == 4){ //exit
			cout << endl << endl <<"Thank you for using Compass Bank 1.0!!!\n\n";
			selection++;		//increase to exit loop/program
			system("pause");
		}

		else{
			cout << "Only numerical selections 1-4 work....Please try again.\n"; //tells user how to correct mistakes
			system("pause");
		}
	system("cls");
	}
}

SOURCE FILE:

#include <iostream>
#include <string>
#include "account.h"
using namespace std;

account::account(std::string first, std::string last, float bal) {
	ownerFirstName = first;
	ownerLastName = last;
	balance = bal;
}

void account::depositFunds() {
}

void account::withdrawFunds() {
}


float account::getBalance(){
	return balance;
}
[code = syntax] [\code]

HEADER FILE:
[code = syntax] [\code]
#ifndef ACCOUNT		
#define ACCOUNT		

#include <iostream>
#include <string>
using namespace std;

class account {
	private:
		//private members
		string	ownerFirstName;
		string	ownerLastName;
		float		balance;
		//private methods

	public:
		//constructor
		account(string, string, float);
		void depositFunds();
		void withdrawFunds();
		float getBalance();
		
};

#endif

The program works like i want it to except for one thing. I CAN'T get the getBalance() function to update when the user deposit's or withdraws money.....Does anyone see a solution?

There are 2 reasons they don't do anything:

1.) You never call withdraw or deposit. You need to add lines to cases 2 and 3 that call them.

2.) Assuming you did properly call them, they wouldn't do anything. They have no code in them.

Be sure that you keep in mind what they are intended to do and add the appropriate code to them. There is code contained in the case statements in main() that would be rendered redundant if you implement withdraw and deposit correctly. Be sure you update your case statements appropriately after you figure out what is missing in withdraw and deposit.

How can i capture the value of void account::depositFunds() and then be able to use it in the getBalance() function. That's what i think needs to happen, but i don't know how to do it.

There are 2 reasons they don't do anything:

1.) You never call withdraw or deposit. You need to add lines to cases 2 and 3 that call them.

2.) Assuming you did properly call them, they wouldn't do anything. They have no code in them.

Be sure that you keep in mind what they are intended to do and add the appropriate code to them. There is code contained in the case statements in main() that would be rendered redundant if you implement withdraw and deposit correctly. Be sure you update your case statements appropriately after you figure out what is missing in withdraw and deposit.

You have some floats located in a while loop, you should fix that, always place your variable either at the start of int main() or right after you include the headers.

How can i capture the value of void account::depositFunds() and then be able to use it in the getBalance() function. That's what i think needs to happen, but i don't know how to do it.

What value can void account::depositFunds() have? How can that value be used?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.