Thread: need help with a program

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    2

    need help with a program

    create c++ programm that defines a class to represent a bank account.
    Data members should include the depositor’s name, the account number(use a string), and the balance.
    Member functions should allow the following:
    1)Creating an object and initializing it.
    2)Displaying the depositor’s name, account number, and balance.
    3)Depositing an amount of money given by an argument.
    4)Withdrawing an amount of money given by an argument.

    having trouble with the member funtion ShowInfo() which has to output DepositorName, AccountNumber, and Balance


    Code:
    #include <cstring>
    #include <iostream>
    
    using namespace std;
    
    class BankAccount
    {
    private:
            std::string DepositorName;
            std::string AccountNumber;
            double Balance;
    public:
            BankAccount();
            BankAccount(std::string depname, std::string acnum, double bal );
            ~BankAccount();  //destructor
            void ShowInfo() const;
            void Deposit(double depamount);
            void Withdraw(double amount);
    };
    
    //non-defualt constructor
    BankAccount::BankAccount(std::string depname, std::string acnum,  double bal)
    {
               DepositorName = depname;
               AccountNumber = acnum;
               Balance = bal;
    }
    
    //destructor
    BankAccount::~BankAccount()
    {
    }
    
    void BankAccount::Withdraw(double amount)
    {
            if (amount < 0)
                  std::cout << "\nPlease enter positive number for the amount to withdraw.";
            else
                    Balance -= amount;
    }
    
    
    void BankAccount::Deposit(double depamount)
    {
              if (depamount < 0)
                   std::cout << "\nPlease enter positive number for  the amount to deposit.";
              else
                      Balance+=depamount;
    }
    
    
    void BankAccount::ShowInfo() const
    {
    	
    	cout << DepositorName;
    	cout << AccountNumber;
        cout << Balance;
    }
    
    
    
    int main()
    {
    	BankAccount somename("some name", "08973541", 60000);
    
    	somename.ShowInfo();
    
    	somename.Deposit(15000);
    
    	somename.ShowInfo();
    
    	somename.Withdraw(30000);
    
    	somename.ShowInfo();
    }
    Last edited by K13; 08-08-2011 at 10:22 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    How does it not work? One thing I notice is that you use std::string but did not #include <string>.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by K13
    it works, now. wow can i delete this post
    Please do not do edit out your answer. You basically made my reply look stupid, and when I look stupid, I may decide not to answer your questions and close your thread so as to deny you any answers. In other words, out of courtesy to other members of this community, leave your post as-is so that the thread still makes sense to a future reader.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    It is extreme bad form to edit your posts after you solved the problem. The main reason for these forums is for people to learn from eachother. No one here is paid for contributing their time; removing your posts negates the work done by the people here who contribute there time to assist because it prevents others from learning. If you do not want to contribute to the community here then find your way to another online resource, such as a paid one.

    EDIT: Too late.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    2
    sorry will post the code up as was in a sec

  6. #6
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by laserlight View Post
    How does it not work? One thing I notice is that you use std::string but did not #include .
    Is it necessary ?I just found out that code using std::string works (in gcc-4.6) without including string . Does the linker automatically find out the classes declared in the standard library but not the objects ?(std::cout always needs iostream) ?
    Last edited by manasij7479; 08-09-2011 at 12:08 AM.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by manasij7479
    Is it necessary ?I just found out that code using std::string works (in gcc-4.6) without including string .
    You may have included some other header that includes <string>.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Does iostream and/or fstream include string ?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by manasij7479
    Does iostream and/or fstream include string ?
    It depends on the implementation.

    To give you a straight answer: simply put, you need to #include <string>, whether directly or indirectly, to use std::string objects.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-03-2009, 04:47 PM
  2. Replies: 5
    Last Post: 08-16-2007, 11:43 PM
  3. Replies: 18
    Last Post: 11-13-2006, 01:11 PM
  4. making a program leave a msg for background program when it closes
    By superflygizmo in forum Windows Programming
    Replies: 2
    Last Post: 02-06-2006, 07:44 PM
  5. Math Equation Program (I can't find the problem with my program!)
    By masked_blueberr in forum C Programming
    Replies: 14
    Last Post: 07-06-2005, 11:53 AM