473,322 Members | 1,526 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Data Reading and Displaying....

OK...Here's the deal, I implemented a class for banking application with simple functions like

1.Withdrawal
2.Deposit
3.Balance Inquiry
4.Creation of Account.


I have this code with me and it works fine until I create individual account,after which it is entering infinite loop.I checked the code and its not givin me any clues for error whatsoever.

I plan to create account and then perform above functions but its just not running properly.I also need suggestions for any improvements regarding string usage and class implementation.

Do I need to use file to read the account details? Or it will work without it.

Here's the code below.

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<string>
  3.  
  4. using namespace std;
  5. using std::cout;
  6. using std::endl;
  7. using std::string;
  8. class account
  9. {
  10.  string name;
  11.  string type;
  12.     int acc_no;
  13.     float bal;
  14. public:
  15.     void create(string &n,string &t,int an,float bl)
  16.     {
  17.         name.assign(n);
  18.         type.assign(t);
  19.  
  20.         acc_no=an;
  21.         bal=bl;
  22.     }
  23.       void deposit(float dep)
  24.     {
  25.         bal=bal+dep;
  26.         cout<<"You deposited "<<dep<<"and your new balance is "<<bal;
  27.     }
  28.    void withd(float dep)
  29.    {
  30.        bal=bal-dep;
  31.        cout<<"Your withdrawl amount is "<<dep<< " and your balance is "<<bal;
  32.    }
  33.    void display(void);
  34. };
  35. void account::display(void)
  36. {
  37.     cout<<"Name "<<name<<endl;
  38.     cout<<"Balance "<<bal;
  39. }
  40. int main()
  41. {
  42.     account ac;
  43.     string a_name;
  44.     string a_type;
  45.   //  char c;
  46.     int a_no,ch;
  47.     float a_bal;
  48.  
  49. //    cout<<"Do you have an account in bank? Y/N\n";
  50. //    cin>>c;
  51. //    if(c=='N')
  52. //    {
  53.     cout<<"Enter your details in following manner\n";
  54.     cout<<"Name of the account holder \t";
  55.     cin>>a_name;
  56.     cout<<"Type of the account whether savings or current \t ";
  57.     cin>>a_type;
  58.     cout<<"Account Number \t";
  59.     cin>>a_no;
  60.     cout<<"Initial Balance \t";
  61.     cin>>a_bal;
  62.     ac.create(a_name,a_type,a_no,a_bal);
  63.     cout<<"Your account has been created!!!";
  64. //    }
  65.  
  66. //    else
  67. //    {
  68.  
  69.         cout<<"Enter your options as follows\n";
  70.         cout<<"1.Deposit\n";
  71.         cout<<"2.Withdrawl\n";
  72.         cout<<"3.Balance Enquiry";
  73.     while(ch!=4)
  74.  
  75.     {
  76.         switch(ch)
  77.         {
  78.             case 1:
  79.  
  80.                 cout<<"Enter amount to be deposited\n";
  81.                 cin>>a_bal;
  82.                         ac.deposit(a_bal);
  83.                         break;
  84.  
  85.             case 2:
  86.  
  87.                 cout<<"Enter amount to be withdrawled";
  88.                 cin>>a_bal;
  89.                         ac.deposit(a_bal);
  90.                         break;
  91.  
  92.             case 3:
  93.  
  94.                 ac.display();
  95.                 break;
  96.  
  97.             case 4:
  98.  
  99.                 exit(0);
  100.  
  101.             default:
  102.                 cout<<"Please enter proper option!!";
  103.  
  104.  
  105. //        }
  106.     }
  107.     }
  108.  
  109.     return 0;
  110.  
  111. }
  112.  
May 23 '10 #1

✓ answered by weaknessforcats

You need some design here.

First, your user interface is buried in the acocunt class. The user interface should be in main() and not in the class. That is, all of your displays need to be in main() and not in the acocunt class.

Second, your create function shoul return a pointer to the object it created.
That is, your main() should look like:

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.    account* obj = createAccount(SAVINGS)
  4.    accunt* obj1 = createAccunt(CHECKING);
  5.  
  6. }
To make this work you will need a class hierarchy for account. The derived classes are checkingAccount and savingsAccount. The createAccount function is a factory function. It uses the argument to determine which type of derived object to create and passes back the address of that object as a base class pointer.

That is, you will need a little polymorphism here.

That will mean a design of the account class interface.

But what ever you do, you cannot write an entire program as one class. That's what you do in C. If you look ar your account class it really is a main().

I always tell people to write the code for main() and then write the classes to support that code. This way your classes contain only the minimum number of features to make main() work. If main() requires more features later then add them later.

1 1360
weaknessforcats
9,208 Expert Mod 8TB
You need some design here.

First, your user interface is buried in the acocunt class. The user interface should be in main() and not in the class. That is, all of your displays need to be in main() and not in the acocunt class.

Second, your create function shoul return a pointer to the object it created.
That is, your main() should look like:

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.    account* obj = createAccount(SAVINGS)
  4.    accunt* obj1 = createAccunt(CHECKING);
  5.  
  6. }
To make this work you will need a class hierarchy for account. The derived classes are checkingAccount and savingsAccount. The createAccount function is a factory function. It uses the argument to determine which type of derived object to create and passes back the address of that object as a base class pointer.

That is, you will need a little polymorphism here.

That will mean a design of the account class interface.

But what ever you do, you cannot write an entire program as one class. That's what you do in C. If you look ar your account class it really is a main().

I always tell people to write the code for main() and then write the classes to support that code. This way your classes contain only the minimum number of features to make main() work. If main() requires more features later then add them later.
May 24 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Michal Szpadzik | last post by:
I'm a newbie. I have some problems with bits data reading. I have binary data file where data is written as 12bits pack. I need to read it becouse there are saved values which have to processed...
3
by: pmud | last post by:
Hi, I have 4 columns in my sql database table. I added a data adapter , data set & a data grid to view this information. But the data grid is displaying those 4 columns TWICE , i.e in the data...
0
by: Tormod Ulsberg | last post by:
Hi! I am using a data grid to display an event log using the code listed below: EventLog log = new EventLog(); log.Log = "System"; log.MachineName = "."; logGrid.DataSource = log.Entries;...
0
by: youngeagle | last post by:
If I am not in the correct newsgroup, my apologies, I wasn't sure if this was a sql question or an aspnet question. Is there a way to sort data, like from a sql query, so that a selected column...
1
by: Rich | last post by:
Hello, I am trying to use the Reportviewer control. I have been following an example from the web, and the instructions from the help files on set up a ..rdlc and binding it to the reportviewer...
0
by: rose | last post by:
I have an ACCESS graph and when I turn on show data tables, the data table displays a whole number instead of the percentage like the graph. I have formatted the sourc query to be percentage, but I...
1
by: Anna MZ | last post by:
Hi. I want to retrieve data from a database and display it in the menu list for a client registration form. The data to be displayed is suburb from a table in MySQL called suburbs with columns...
2
by: Dawnyy | last post by:
I'm in desperate need of knowing how I can bind my data grid so that I have the following Customer ID - parent table Customer Name - parent table Status ID - parent table Status Description -...
1
by: drwigginton | last post by:
I have created several bar charts in MS Access using query results to provide the data for the chart. I added data tables to the charts to show the actual values. My problem is that one of the fields...
5
by: brendan.mcgrath | last post by:
Hi All DB - SQL Express 2005 ST - ASP VBScript Dev Env OS - Win XP IIS5 I am trying to retrieve records from the DB and write them into a csv file/display onscreen for further processing....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.