Thread: CS2 Assignment: Error

  1. #1
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331

    CS2 Assignment: Error

    In CS2 we had to write a basic bank program with classes. I wrote the program, in a well done manner imo, but the output is messed up. Me and my teacher both must be missing the problem, as we have tried several things to no avail. I know its gotta be something lame, the output is commented at the end of the program, its not commented but should be pretty easy to follow.

    /*

    the two smiley's are Deposit and Display.

    */


    Code:
    /*	Steven Billington
    	Febuary 13, 2002
    	BankAccountCS2.cpp
    */
    
    #include "stdafx.h"
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    class BankAccount
    {
    public:
    	void get_initial();
    	void get_type();
    	void get_amount();
    	void Deposit();
    	void Withdrawl();
    	void Display();
    private:
    	double initial,
    		   amount,
    		   final;
    	char type;
    };
    
    
    int main(int argc, char* argv[])
    {
    	BankAccount Object;
    
    	Object.get_initial();
    	Object.get_type();
    	Object.get_amount();
    	Object.Display();
    
    	return 0;
    }
    
    void BankAccount::get_initial()
    {
    	cout << "Enter your previous balance: ";
    	cin >> initial;
    }
    
    void BankAccount::get_type()
    {
    	cout << "Withdrawl (W) or Deposit (D): ";
    	cin >> type;
    
    	type = toupper(type);
    
    	switch (type)
    	{
    	case 'W':
    		Withdrawl();
    		break;
    	case 'D':
    		Deposit();
    		break;
    	default:
    		cout << "Invalid selection\n";
    	}
    }
    
    void BankAccount::Withdrawl()
    {
    	if (amount > initial)
    	{
    		cout << "Withdrawl is larger than funds, transaction aborted.\n";
    	}
    	else
    	{
    		final = initial - amount;
    	}
    }
    
    void BankAccount::Deposit()
    {
    	final = initial + amount;
    }
    
    void BankAccount::get_amount()
    {
    	cout << "How much would you like too " << type 
    		 << "?: ";
    	cin >> amount;
    }
    
    void BankAccount::Display()
    {
    	cout << setiosflags(ios::fixed | ios::showpoint | ios::right)
    		 << setprecision(2);
    	cout << "Starting Balance: " << initial 
    		 << endl;
    	cout << "Transaction Type: " << type
    		 << endl;
    	cout << "Transaction Amount: " << amount
    		 << endl;
    	cout << "New Balance: " << final
    		 << endl;
    }
    
    /*	Output is the same no matter the
    	input!
    
     Enter your previous balance: 20.00
    Withdrawl (W) or Deposit (D): D
    How much would you like too D?: 14.00
    Starting Balance: 20.00
    Transaction Type: D
    Transaction Amount: 14.00
    New Balance: -92559631349317831000000000000000000000000000000000000000000000.00
    Press any key to continue
    */

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    In your get_type( ) function, it calls Deposit or Withdraw before get_amount( ) is called.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    duh me, thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  4. file reading
    By gunghomiller in forum C++ Programming
    Replies: 9
    Last Post: 08-07-2007, 10:55 PM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM