URGENT...C++ Pointers to objects

This program program should use the class as an object (define the object with use of a pointer) that will display the "bank menu".

The program should run until Quit is selected.  When 1-5 is selected, the number of coins of that denomination is what should be entered by the user.  Option 6 displays the total cash amount on screen and 7 displays the total number of coins of each denomination on screen.

(My code is in three files: .h, .cpp and main.cpp -- this is how we are learning to program it-- the header file should remain as is).

...Here is my code (which does not work...I am lost in pointers and what exactly to do in my functions...!!!  Help!!!  I need this to work before Monday a.m.  I'm using MSVisual C++ compiler v.5.

Thank you!

//bank.h
class bank  
{
  public:
    bank();
    bank(bank&);
    virtual ~bank();
    void DisplayTotal();
    float GetTotal();
    void AddDimes(int);
    void AddNickels(int);
    void AddPennies(int);
    void AddQuarters(int);
    void AddLoonies(int);
    void DisplayBreakdown();

  private:
    int Loonies, Quarters, Dimes, Nickels, Pennies,             userInsert;
    float Total;
};

//bank.ccp
#include <iostream.h>
#include "bank.h"

bank::bank()
{

}

bank::bank(bank&)
{
  Loonies=0, Quarters=0, Dimes=0, Nickels=0, Pennies=0,   userInsert=0;
  Total=1;
}

bank::~bank()
{

}

void bank::DisplayTotal()
{
  (*transact).GetTotal();
}

float bank::GetTotal()
{
  return
  ((Dimes*.10)+(Nickels*.05)+(Pennies*.01)+(Quarters*.25)+(Loonies*1));
}

void bank::AddDimes(int Dimes)
{
  if(userInsert>0)
  {
    Dimes=userInsert;
  }
}

void bank::AddNickels(int Nickels)
{
  if(userInsert>0)
  {
    Nickels=userInsert;
  }
}

void bank::AddPennies(int Pennies)
{
  if(userInsert>0)
  {
    Pennies=userInsert;
  }
}

void bank::AddQuarters(int Quarters)
{
  if(userInsert>0)
  {
    Quarters=userInsert;
  }
}

void bank::AddLoonies(int Loonies)
{
  if(userInsert>0)
  {
    Loonies=userInsert;
  }
}

void bank::DisplayBreakdown()
{
      
}

//bankmain.cpp
#include <iostream.h>
#include "bank.h"

void main (void)
{
  int Loonies, Quarters, Dimes, Nickels, Pennies, selection,   userInsert, Quit;
  float Total=1;
  bank *transact;
  transact=new bank;

  do
  {
    cout<<"\nPiggy Bank Menu\n\n";
    cout<<"1) Insert Pennies\n";
    cout<<"2) Insert Nickels\n";
    cout<<"3) Insert Dimes\n";
    cout<<"4) Insert Quarters\n";
    cout<<"5) Insert Loonies\n";
    cout<<"6) Display Current Total in Bank\n";
    cout<<"7) Display Coin Breakdown in Bank\n";
    cout<<"0) Quit\n";
    cout<<"\nPlease enter your selection:  ";
    cin>>selection;

    switch(selection)
    {
      case 1:
        cout<<"\nPlease indicate number of Pennies inserted:         ";
        cin>>Pennies;
        (*transact).AddPennies(Pennies);
      break;
      case 2:
        cout<<"\nPlease indicate number of Nickels inserted:         ";
        cin>>userInsert;
      (*transact).AddNickels(Nickels);
      break;
      case 3:
        cout<<"\nPlease indicate number of Dimes inserted:          ";
        cin>>userInsert;
        (*transact).AddDimes(Dimes);
        break;
      case 4:
        cout<<"\nPlease indicate number of Quarters         inserted:  ";
        cin>>userInsert;
        (*transact).AddQuarters(Quarters);
        break;
      case 5:
        cout<<"\nPlease indicate number of Loonies inserted:         ";
        cin>>userInsert;
        (*transact).AddLoonies(Loonies);
        break;
      case 6:
        (*transact).DisplayTotal();
        break;
      case 7:
        (*transact).DisplayBreakdown();
        break;
      case 8:
        if(Quit=1)
        {
          cout<<"\nExiting program!\n\n";
        }
        break;
      default:
        cout<<"\nInvalid selection!\n\n";
        break;
    }
  }while(selection!=0);
}
appreciativeAsked:
Who is Participating?
 
scrapdogCommented:
:)
0
 
appreciativeAuthor Commented:
This question is not supposed to be in <unlocked> I've just posted it...it has not been answered yet!!!
0
 
scrapdogCommented:
1.  Does the code compile?
2.  If it does, which of your tasks have or have not been completed correctly?
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
EliezerCommented:
You aren't calling the initializer bank(bank&).  You're calling the initializer bank(), which doesn't clear any of the data members.  That's your main problem.  As far as I can tell, you're not doing anything wrong with pointers - although, if this were my program, I would declare transact as:
bank transact;
Which takes care of all the allocation/deallocation/dereferencing problems by making it a automatic or stack-based variable.  You still need to clear the members in bank(), however.

Other notes:

If bank(bank&) is a copy constructor it should be bank(const bank&).
Inserting pennies sets the number of Pennies, rather than adding Pennies.  
It often clears up a lot of confusion if you name the local variables "thePennies" and the members "mPennies".
I don't know why you have the "Total" data member or local variable, because it's not doing anything I can see.
DisplayTotal() gets the total and does nothing with it - doesn't print it out or anything.
DisplayBreakdown() doesn't do anything.
case 8: should be case 0:
Nobody is initializing Quit or setting it when it's time to Quit.
Where are the warnings on your compiler?  They should have caught this stuff.  Check your preferences.
0
 
appreciativeAuthor Commented:
Thanks Eliezer,

My compiler gives a 0 error, 1 warning of "double to float" for the GetTotal...the only function that works is Quit....when I call my functions in main, ex. pennies, the user enters number of pennies but the DisplayTotal , the GetTotal and Breakdown functions do nothing (well I haven't entered anything in the Breakdown, I actually don't know what to enter....!!!!!!!!!
Thanks for answering to promptly...
Appreciative  
0
 
scrapdogCommented:
It appears as if you want to use bank *transact globally.  Therefore you have to move its declaration out of main and into your header file.

If one of the specifications of your assignment was to use the header file exactly as it is shown above, you will have to pass *transaction as a parameter to some of your functions.

It appears that the only function that is using *transact is your display total function.
0
 
scrapdogCommented:
First of all, get rid of your display total function altogether.  This will allow you to leave your *transact in main.

The total is being added up, but you are doing nothing to print it.
0
 
scrapdogCommented:
OK, I just realized you would have to change the header file to et rid of display total.  Leave it in and I will see what I can do with it.
0
 
appreciativeAuthor Commented:
Hi Scrapdog,

I just sent you a message Scrapdog, but I had a server problem and it did not go through, trying again....

I'm trying to get the function DisplayTotal to display amount of cash on screen and DisplayBreakdown to display coins in each denomination....I tried cout<<(*transact).DisplayTotal and the same for Breakdown but the compiler give me approx. 10 errors... I know I'm not managing my pointers correctly, but I don't know what to do with them!!!!

Thanks++++

Appreciative


0
 
appreciativeAuthor Commented:
Hi Scrapdog,

I just sent you a message Scrapdog, but I had a server problem and it did not go through, trying again....

I'm trying to get the function DisplayTotal to display amount of cash on screen and DisplayBreakdown to display coins in each denomination....I tried cout<<(*transact).DisplayTotal and the same for Breakdown but the compiler give me approx. 10 errors... I know I'm not managing my pointers correctly, but I don't know what to do with them!!!!

Thanks++++

Appreciative


0
 
scrapdogCommented:
Before we go any further, I noticed several logic errors in your main.

Change your main to look like this:


void main (void)
{
  int userInsert, Quit;
  float Total=1;
  bank *transact;
  transact=new bank;

  do
  {
    cout<<"\nPiggy Bank Menu\n\n";
    cout<<"1) Insert Pennies\n";
    cout<<"2) Insert Nickels\n";
    cout<<"3) Insert Dimes\n";
    cout<<"4) Insert Quarters\n";
    cout<<"5) Insert Loonies\n";
    cout<<"6) Display Current Total in Bank\n";
    cout<<"7) Display Coin Breakdown in Bank\n";
    cout<<"0) Quit\n";
    cout<<"\nPlease enter your selection:  ";
    cin>>selection;

    switch(selection)
    {
      case 1:
        cout<<"\nPlease indicate number of Pennies inserted:         ";
        cin>>userInsert;
        (*transact).AddPennies(userInsert);
break;
      case 2:
        cout<<"\nPlease indicate number of Nickels inserted:         ";
        cin>>userInsert;
(*transact).AddNickels(userInsert);
break;
      case 3:
        cout<<"\nPlease indicate number of Dimes inserted:          ";
        cin>>userInsert;
        (*transact).AddDimes(userInsert);
        break;
      case 4:
        cout<<"\nPlease indicate number of Quarters         inserted:  ";
        cin>>userInsert;
        (*transact).AddQuarters(userInsert);
        break;
      case 5:
        cout<<"\nPlease indicate number of Loonies inserted:         ";
        cin>>userInsert;
        (*transact).AddLoonies(userInsert);
        break;
      case 6:
        (*transact).DisplayTotal();
        break;
      case 7:
        (*transact).DisplayBreakdown();
        break;
      case 8:
        if(Quit=1)
        {
          cout<<"\nExiting program!\n\n";
        }
        break;
      default:
        cout<<"\nInvalid selection!\n\n";
        break;
    }
  }while(selection!=0);
}


You do not need Pennies, Dimes, etc. in main, all you need is userinsert.  As far as your other two headers, I will get back to shortly.  I got it to compile just fine.
0
 
appreciativeAuthor Commented:
Here is my compile error (only one...fortunately) when I do a cout<<(*transact).DisplayTotal;

Compiling...
bankmain.cpp
C:\Windows\Desktop\cplus\bank\bankmain.cpp(54) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'void' (or there is no acceptable conversion)
Error executing cl.exe.
0
 
scrapdogCommented:
First, I forgot to declare "selection" in main above, so change it to:

int userInsert, selection, Quit;

Then change all your Add functions to the following:


void bank::AddDimes(int userInsert)
{
  if(userInsert>0)
  {
    Dimes=userInsert;
  }
}

void bank::AddNickels(int userInsert)
{
  if(userInsert>0)
  {
    Nickels=userInsert;
  }
}

void bank::AddPennies(int userInsert)
{
  if(userInsert>0)
  {
    Pennies=userInsert;
  }
}

void bank::AddQuarters(int userInsert)
{
  if(userInsert>0)
  {
    Quarters=userInsert;
  }
}

void bank::AddLoonies(int userInsert)
{
  if(userInsert>0)
  {
    Loonies=userInsert;
  }
}


Notice that the only thing that changed is the parameter.  You originally called it Dimes, Quarters, etc. when it should be userinsert (userinsert is the amount you are adding, dimes,quarters,etc. are the amounts of money already in the bank).
0
 
scrapdogCommented:
Your display breakdown function can look something like this:

void bank::DisplayBreakdown()
{
  cout<<" pennies "<< Pennies << "\n";
  cout<<" nickels "<< Nickels << "\n";
  cout<<" dimes "<< Dimes << "\n";
  cout<<" quarters "<< Quarters << "\n";
  cout<<" loonies"<< Loonies << "\n";
}

You might have to change it a bit to get the desired formatting!!
0
 
scrapdogCommented:
Get rid of the line:

float total=1;

in your main.  total isn't used anywhere.
0
 
scrapdogCommented:
bank::bank()
{
  Loonies=0, Quarters=0, Dimes=0, Nickels=0, Pennies=0,   userInsert=0;
  Total=1;
}

Do your initialization in bank::bank().  Get rid of bank::bank(bank&).
0
 
scrapdogCommented:
Displaytotal could look like this:

void bank::DisplayTotal()
{
  float total = GetTotal();
  cout << total;
}

Again, you will have to format it to how you want it!!
0
 
scrapdogCommented:
Get rid of the variable "quit".  It doesn't do anything.

And as suggested by Eliezer:

case 0:
       
        {
          cout<<"\nExiting program!\n\n";
        }
        break;

rather than case 8!!!
0
 
scrapdogCommented:
Finished product:


#include <iostream.h>

class bank  
{
  public:
    bank();
    virtual ~bank();
    void DisplayTotal();
    float GetTotal();
    void AddDimes(int);
    void AddNickels(int);
    void AddPennies(int);
    void AddQuarters(int);
    void AddLoonies(int);
    void DisplayBreakdown();

  private:
    int Loonies, Quarters, Dimes, Nickels, Pennies,             userInsert;
    float Total;
};


bank::bank()
{
  Loonies=0, Quarters=0, Dimes=0, Nickels=0, Pennies=0,   userInsert=0;
  Total=1;
}


bank::~bank()
{

}

void bank::DisplayTotal()
{
  float total = GetTotal();
  cout << total;
}


float bank::GetTotal()
{
  return
  ((Dimes*.10)+(Nickels*.05)+(Pennies*.01)+(Quarters*.25)+(Loonies*1));
}

void bank::AddDimes(int userInsert)
{
  if(userInsert>0)
  {
    Dimes=userInsert;
  }
}

void bank::AddNickels(int userInsert)
{
  if(userInsert>0)
  {
    Nickels=userInsert;
  }
}

void bank::AddPennies(int userInsert)
{
  if(userInsert>0)
  {
    Pennies=userInsert;
  }
}

void bank::AddQuarters(int userInsert)
{
  if(userInsert>0)
  {
    Quarters=userInsert;
  }
}

void bank::AddLoonies(int userInsert)
{
  if(userInsert>0)
  {
    Loonies=userInsert;
  }
}

void bank::DisplayBreakdown()
{
  cout<<" pennies "<< Pennies << "\n";
  cout<<" nickels "<< Nickels << "\n";
  cout<<" dimes "<< Dimes << "\n";
  cout<<" quarters "<< Quarters << "\n";
  cout<<" loonies"<< Loonies << "\n";
}



void main (void)
{
  int userInsert, selection;
 
  bank *transact;
  transact=new bank;

  do
  {
    cout<<"\nPiggy Bank Menu\n\n";
    cout<<"1) Insert Pennies\n";
    cout<<"2) Insert Nickels\n";
    cout<<"3) Insert Dimes\n";
    cout<<"4) Insert Quarters\n";
    cout<<"5) Insert Loonies\n";
    cout<<"6) Display Current Total in Bank\n";
    cout<<"7) Display Coin Breakdown in Bank\n";
    cout<<"0) Quit\n";
    cout<<"\nPlease enter your selection:  ";
    cin>>selection;

    switch(selection)
    {
      case 1:
        cout<<"\nPlease indicate number of Pennies inserted:         ";
        cin>>userInsert;
        (*transact).AddPennies(userInsert);
break;
      case 2:
        cout<<"\nPlease indicate number of Nickels inserted:         ";
        cin>>userInsert;
(*transact).AddNickels(userInsert);
break;
      case 3:
        cout<<"\nPlease indicate number of Dimes inserted:          ";
        cin>>userInsert;
        (*transact).AddDimes(userInsert);
        break;
      case 4:
        cout<<"\nPlease indicate number of Quarters         inserted:  ";
        cin>>userInsert;
        (*transact).AddQuarters(userInsert);
        break;
      case 5:
        cout<<"\nPlease indicate number of Loonies inserted:         ";
        cin>>userInsert;
        (*transact).AddLoonies(userInsert);
        break;
      case 6:
        (*transact).DisplayTotal();
        break;
      case 7:
        (*transact).DisplayBreakdown();
        break;
      case 0:
       
        {
          cout<<"\nExiting program!\n\n";
        }
        break;
      default:
        cout<<"\nInvalid selection!\n\n";
        break;
    }
  }while(selection!=0);
}


I compiled it as one file, so you will have to break it back up into three separate files (this shouldn't be too hard!!!).

Hope this helped...

scrappy

0
 
appreciativeAuthor Commented:
OK Scrapdog thanks+++, It'll take me a few minutes (or more) to make the changes...
very, very
Appreciative

You sure are on your toes tonight....!!!  If it works, I'll be in heaven...!!!  Thanks....
0
 
scrapdogCommented:
Anything to take a break from studying for SQL exam, math exam, law paper, etc. ETC.  ETC!!!!!!!  (zzzzzz)
0
 
scrapdogCommented:
Did it work???


By the way, I noticed that I redeclared total in DisplayTotal.  

void bank::DisplayTotal()
{
  float total = GetTotal();
  cout << total;
}

It would probably be better to do it like this:

void bank::DisplayTotal()
{
  total = GetTotal();
  cout << total;
}


Although the program will run exactly the same way, it is probably better to use total from your class rather than a local variable.  Hopefully you understand why...

(you would probably lose a few points on the assignment for not using total from the bank class!!)
0
 
appreciativeAuthor Commented:
Scrapdog,

Everything works just fine except for the DisplayTotal, whatever I enter it gives me back a total of 1  .....
Would you know what causes this????  I'll review on my side....

Thanks a zillion....
appreciative
0
 
appreciativeAuthor Commented:
Scrapdog, in the meanwhile I'm rejecting Eliezer's answer
0
 
scrapdogCommented:
Test it with these values:

1 penny 2 dimes 3 loonies.  (do not enter ANYTHING for nickels or quarters)

What is your output?
0
 
appreciativeAuthor Commented:
Not enough information or explanations....Thank you very much anyway...!!!!
0
 
scrapdogCommented:
If your output is not 3.21, paste all of your source code...
0
 
scrapdogCommented:
>Not enough information or explanations....Thank you very much
>anyway...!!!!

Run the program, and enter 1 for number of pennies, 2 for dimes, etc..

Paste your source code anyway...

0
 
appreciativeAuthor Commented:
Scrapdog, I've been testing and testing, it all works except when I enter nickels, it does not return it in the DisplayTotal nor in the DisplayBreakdown (it gives a 0)...!!

appreciative
0
 
scrapdogCommented:
Let's see your source code...
0
 
appreciativeAuthor Commented:
Sorry Scrapdog, its a mess when pasted....but I didn't want to keep you waiting...


//bank.h
class bank  
{
      public:
            bank();
            virtual ~bank();
            void DisplayTotal();
            float GetTotal();
            void AddDimes(int);
            void AddNickels(int);
            void AddPennies(int);
            void AddQuarters(int);
            void AddLoonies(int);
            void DisplayBreakdown();

      private:
            int Loonies, Quarters, Dimes, Nickels, Pennies, userInsert;
            float Total;
};



//bank.cpp
#include <iostream.h>
#include "bank.h"

bank::bank()
{
      Loonies=0, Quarters=0, Dimes=0, Nickels=0, Pennies=0, userInsert=0;
}


bank::~bank()
{

}

void bank::DisplayTotal()
{
      (float) Total=GetTotal();
      cout<<Total;
}

float bank::GetTotal()
{
      return (float)((Dimes*.10)+(Nickels*.05)+(Pennies*.01)+(Quarters*.25)+(Loonies*1));
}

void bank::AddDimes(int userInsert)
{
      if(userInsert>0)
      {
            Dimes=userInsert;
      }
}

void bank::AddNickels(int unserInsert)
{
      if(userInsert>0)
      {
            Nickels=userInsert;
      }
}

void bank::AddPennies(int userInsert)
{
      if(userInsert>0)
      {
            Pennies=userInsert;
      }
}

void bank::AddQuarters(int userInsert)
{
      if(userInsert>0)
      {
            Quarters=userInsert;
      }
}

void bank::AddLoonies(int userInsert)
{
      if(userInsert>0)
      {
            Loonies=userInsert;
      }
}

void bank::DisplayBreakdown()
{
      cout<<"pennies "<<Pennies<<"\n";
      cout<<"nickels "<<Nickels<<"\n";
      cout<<"dimes "<<Dimes<<"\n";
      cout<<"quarters "<<Quarters<<"\n";
      cout<<"loonies "<<Loonies<<"\n";       
}


//bankmain.cpp
#include <iostream.h>
#include "bank.h"

void main (void)
{
      int selection,userInsert;
      bank *transact;
      transact=new bank;

      do
      {
            cout<<"\nPiggy Bank Menu\n\n";
            cout<<"1) Insert Pennies\n";
            cout<<"2) Insert Nickels\n";
            cout<<"3) Insert Dimes\n";
            cout<<"4) Insert Quarters\n";
            cout<<"5) Insert Loonies\n";
            cout<<"6) Display Current Total in Bank\n";
            cout<<"7) Display Coin Breakdown in Bank\n";
            cout<<"0) Quit\n";
            cout<<"\nPlease enter your selection:  ";
            cin>>selection;
      
            switch(selection)
            {
                  case 1:
                        cout<<"\nPlease indicate number of Pennies                                                         inserted:  ";
                        cin>>userInsert;
                        (*transact).AddPennies(userInsert);
                        break;
                  case 2:
                        cout<<"\nPlease indicate number of Nickels                                                         inserted:  ";
                        cin>>userInsert;
                        (*transact).AddNickels(userInsert);
                        break;
                  case 3:
                        cout<<"\nPlease indicate number of Dimes                                                         inserted:  ";
                        cin>>userInsert;
                        (*transact).AddDimes(userInsert);
                        break;
                  case 4:
                        cout<<"\nPlease indicate number of Quarters                                                         inserted:  ";
                        cin>>userInsert;
                        (*transact).AddQuarters(userInsert);
                        break;
                  case 5:
                        cout<<"\nPlease indicate number of Loonies                                                         inserted:  ";
                        cin>>userInsert;
                        (*transact).AddLoonies(userInsert);
                        break;
                  case 6:
                        (*transact).DisplayTotal();
                        break;
                  case 7:
                        (*transact).DisplayBreakdown();
                        break;
                  case 0:
                        cout<<"\nExiting program!\n\n";
                        break;
                  default:
                        cout<<"\nInvalid selection!\n\n";
                        break;
            }
      }while(selection!=0);
}
0
 
scrapdogCommented:
void bank::DisplayTotal()
{
(float) Total=GetTotal();
cout<<Total;
}

You are using (float) as a typecast here.  Remove the (float)
and see if it works.

void bank::DisplayTotal()
{
Total=GetTotal();
cout<<Total;
}



0
 
appreciativeAuthor Commented:
I found it, I made a spelling mistake in void AddNickels(userInsert) I wrote (usenrInsert)....Sorry...I'll test again and I know it will work...back in a flash....
0
 
scrapdogCommented:
btw, there is one more amendment you should make:

void bank::AddDimes(int userInsert)
{
  if(userInsert>0)
  {
    Dimes += userInsert;
  }
}

void bank::AddNickels(int userInsert)
{
  if(userInsert>0)
  {
    Nickels += userInsert;
  }
}

void bank::AddPennies(int userInsert)
{
  if(userInsert>0)
  {
    Pennies += userInsert;
  }
}

void bank::AddQuarters(int userInsert)
{
  if(userInsert>0)
  {
    Quarters += userInsert;
  }
}

void bank::AddLoonies(int userInsert)
{
  if(userInsert>0)
  {
    Loonies += userInsert;
  }
}

All I did here was change the = to += in all of the add functions.  This will add the number of coins rather than set them.  (Looking at Eliezer's answer I think he/she noticed that)...
0
 
appreciativeAuthor Commented:
Yes, it was only a little typing mistake that caused all that nuisance.....
Well....once again, I really don't have words to express my gratitude....!!!
You did it again Scrapdog....Thanks are not enough ....  Again I worked hard on this one, but I'm just starting C++, hope I will eventually get the hang of it....I wasn't that far off  : )

Only one little problem here, how do I transer the point to you????  I rejected the other answer, but now I don't have the dialogue box asking if I accept your answer....!!!!


0
 
scrapdogCommented:
Don't hesitate to come back in the future if you need help!!

:)
0
 
appreciativeAuthor Commented:
made the changes, thanks Scrapdog (Scrappy)... now I can go to bed (it's 4:35)....bye...
always,
appreciative
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.