Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,109 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,702 people online right now. Registration is fast and FREE... Join Now!




functions.

 
Reply to this topicStart new topic

functions.

kristen21023
15 Oct, 2007 - 01:07 PM
Post #1

New D.I.C Head
*

Joined: 5 Oct, 2007
Posts: 3


My Contributions
i'm writting a program that is supposed to display...

{ bankAccount: Hill, Annie, $123.00}
{ bankAccount: Becker, Bob, $45.60}

my code doesnt work though, i'm so confused right now.
any advice would help soooo much. i know it's something
that i'm doing wrong in the function part.

CODE

#include <iostream>
#include "BACCOUNT.H"

void display(const bankAccount & anAcct)
{
string name=anAcct.name();
int space=name.find(" ");
string firstname;
name.substr( 0, int space)
string lastname;
name.substr(int space, 1);

return name;

}

int main()
{cout << "Kristen Hoeve, CO127, Avtivity 5.4 "<<endl;
bankAccount a("Annie Hill", 123.00);
bankAccount b("Bob Becker", 45.60);
display(a);
display(b);
cout<<"{bank values:"<<lastname<<", "<<firstname<<", $"<<etc" <<"}"<<endl;
system ("pause");
return 0;
}



here's my baccount. h file too..but im pretty sure it's irrelevent.

CODE

#ifndef BACCOUNT_H   // Avoid redeclaring class bankAccount.
#define BACCOUNT_H   // This code is compiled only once
#include <string>    // for class string
using namespace std; // avoid having to write std:: as in std::string

///////////////////////////////////////////
/////// class bankAccount defintion ///////
///////////////////////////////////////////

class bankAccount {
public:  // class member functions

//--constructors
  bankAccount();

  bankAccount(string initName, double initBalance);
    // post: A bankAccount with two arguments when called like this:
    //       bankAccount anAcct("Hall", 100.00);

//--modifiers

    void deposit(double depositAmount);
    // post: depositAmount is credited to this object's balance

    void withdraw(double withdrawalAmount);
    // post: withdrawalAmount is debited from this object's balance

//--accessors
    
  double balance() const;
    // post: return this account's current balance

    string name() const;
   // post return the account name

private:
    string my_name;    // Uniquely identify an object
    double my_balance; // Store the current balance (non-persistent)
};

//--Auxilliary functions

// With these two functions, bankAccount objects can be
// sorted and searched by the standard algorithms
bool operator <  (const bankAccount & left, const bankAccount & right);
bool operator == (const bankAccount & left, const bankAccount & right);
bool operator != (const bankAccount & left, const bankAccount & right);
bool operator <= (const bankAccount & left, const bankAccount & right);
bool operator >  (const bankAccount & left, const bankAccount & right);
bool operator >= (const bankAccount & left, const bankAccount & right);

#endif   // ifndef BACCOUNT_H


thanks again if you can help me at all i REALLY appreciate it.

kristen.
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Functions.
15 Oct, 2007 - 02:33 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,198



Thanked: 213 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Hi Kristen, you have quite a few things wrong with this, so I am going to list the changes here and then show you a working example.

1) To start, lets look at your header file. You defined a class, but you failed to put in the actual function implementation so there is no way for you to call bank account details and get any information back. You only did one side of the coin here by defining the class "blueprint" but you forgot to tell the compiler how the functions actually work before you started using them. So I put those in and hooked them to your class' private member variables.

2) Now over on your cpp file, you are not creating instances of your classes and then calling them correctly. When you create a class you have to use the "new" keyword to create an instance of your classes and assign them to variables of your class type.

bankAccount *a = new bankAccount("name",balance);

The above example is saying declare a variable pointer of type bankAccount called "a". So the variable "a" points to an address to where a bankAccount class is stored.

3) Since you now have pointers to two classes you created, you have to pass these pointers to the display function. Now I noticed you tried a reference here, but being that you have classes and pointers to the classes, you have to dereference those variables. Which means you have to ask the pointer to lookup the object it points to and return a reference to it. We do that using the -> operator. Which you also may know is the same as (*anAcct) and then using the dot notation.

4) So once we are in the display function we need to get at the functions you have defined. So we use the -> to call its member variables... which is why you needed to define them back in your header file so they will actually return a value.

5) You defined a "space" variable and then defined it again in the other two functions. You just needed to use it once it was defined.

6) Your call to find the last name had two parameters, where you needed just one. The first argument is where to start in the string and the second is the ending index. I want to make you aware that this will break if the name doesn't have a space. So make sure your data is valid when you get to this point in your program. Like if I put my name as "Martyr2" there is no space so this breaks. You also need to set your firstname and lastname variables to the values returned by the substr call. You also forgot your semicolon on the end of your lastname line.

7) Fetch the name, balance and then print right in display(). That is what it is there for, to display the info. You had a return statement here but the function was set as void (not to return a function). So that was another error. Since you are displaying here, no need to display back in the main function.

Those are the main changes. I took out a few of your operator definitions and your preprocessor declarations just to help simplify things. This is how it all should look.

BACCOUNT.H

CODE

#include <string>    // for class string
using namespace std; // avoid having to write std:: as in std::string

///////////////////////////////////////////
/////// class bankAccount defintion ///////
///////////////////////////////////////////

class bankAccount {
public:  // class member functions

//--constructors
  bankAccount();

  bankAccount(string initName, double initBalance);
    // post: A bankAccount with two arguments when called like this:
    //       bankAccount anAcct("Hall", 100.00);

//--modifiers

    void deposit(double depositAmount);
    // post: depositAmount is credited to this object's balance

    void withdraw(double withdrawalAmount);
    // post: withdrawalAmount is debited from this object's balance

//--accessors
    
  double balance() const;
    // post: return this account's current balance

    string name() const;
   // post return the account name

private:
    string my_name;    // Uniquely identify an object
    double my_balance; // Store the current balance (non-persistent)
};

// Definition for your parameterless constructor
bankAccount::bankAccount() {

}

// Definition for your constructor that takes parameters.
// Notice how we set the member variables using "this" which refers to the current instance
bankAccount::bankAccount(std::string initName, double initBalance) {
    this->my_name = initName;
    this->my_balance = initBalance;
}

// Some empty definitions for your other functions
void bankAccount::deposit(double depositAmount) {

}

void bankAccount::withdraw(double withdrawalAmount) {

}

// Definition for returning the customer's balance
double bankAccount::balance() const {
    return this->my_balance;
}


// Definition for returning the customer's name
string bankAccount::name() const {
    return this->my_name;
}


So as you can see I added definitions with the same signatures as defined by your class. Some of the implementation was filled in for you because it was needed for the display portion of your program. Below is the main file...

CODE

#include <iostream>
#include "BACCOUNT.H"

// Display the account information
void display(const bankAccount *anAcct)
{
    // Save the name of the account holder
    string name = anAcct->name();

    // Break the name on the space
    int space = (int) name.find(" ");

    // Store the firstname and lastname
    string firstname = name.substr(0, space);
    string lastname = name.substr(space);

    // Get the balance
    double balance = anAcct->balance();

    // Show all the information for this account
    cout << "{bank values:" << lastname << ", " << firstname << ", $" << balance << "}" << endl;
}

int main()
{
    // You forgot the "c" in activity
    cout << "Kristen Hoeve, CO127, Activity 5.4 "<<endl;

    // Define two instances of bank accounts, passing in their initializing details
    bankAccount *a = new bankAccount("Annie Hill", 123.00);
    bankAccount *b = new bankAccount("Bob Becker", 45.60);

    // Pass the pointers to these accounts to our display function
    display(a);
    display(b);
    
    system ("pause");

    return 0;
}


So read through the comments and see how I did everything. If you have any questions, just throw it on this thread and either I or someone else can help you out.

Enjoy!

"At DIC we be coding ninjas!" decap.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 09:29PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month