I'm currently writing a C++ program for an assignment.
"Create a C++ program to manage 10 bank accounts... "
To include;
"An appropriate type definition to store the name, account number and
balance of a bank account
An array to record the details of the 10 bank accounts
Functions/procedures to update/interrogate the bank accounts array."
I have all the code for a single bank account where you can depost/
withdaw and vew balance, but, I’m not quite sure about arrays and how
to access them, so my idea was this:
float account [2][10];
// account munber on [0][x]
account[0][0] = 10001;
account[0][1] = 10002;
account[0][2] = 10003;
account[0][3] = 10004;
account[0][4] = 10005;
account[0][5] = 10006;
account[0][6] = 10007;
account[0][7] = 10008;
account[0][8] = 10009;
account[0][9] = 10010;
// account balance on [1][x]
account[1][0] = 0;
account[1][1] = 0;
account[1][2] = 0;
account[1][3] = 0;
account[1][4] = 0;
account[1][5] = 0;
account[1][6] = 0;
account[1][7] = 0;
account[1][8] = 0;
account[1][9] = 0;
But I have no idea how to include a name, or how I would start with
"insert account number" and get this to pull details.
Consider defining a new *type* that would store all this information in
"one place". Something like
struct Account {
float balance; // if you think that 'float' is the right type
std::string owner; //
unsigned number; //
};
Or have you not gone over user-defined types?
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Not to my knowledge :/
Arrays have been specified in the assignment brief
not looking for specifics just some subtle guidance :)
The most important thing to know about arrays in C++ is that you should
avoid them. They're considered "evil", because their behaviour is
confusing, error-prone and inconsistent with the rest of the language.
std::vector is a safe, robust alternative.
http://www.cppreference.com/cppvector/index.html
http://www.parashift.com/c++-faq-lite/containers.html
I sincerely hope your teacher does not force you to use arrays rather
than std::vector! :)
--
Christian Hackl
Well, if you didn't go through user-defined types (which is rather
strange because that's the bread-and-butter of C++), you could simply
define an array of ten account number, ten account balances, ten account
names (account holder names), ten whatever else you want or see relevant
to your task...
I don't have any "online reading materials". Didn't your course
instructor give you anything?
Unfortunatly he is
You must start by learning about "struct" which is geek speak for what is
called in English, a "record". This is the bare minimum entry point to this
question. Details beyond that depend on the order of teaching things that
the instructor is using. If he said to use an array, use an array. If he
hasn't told you about the string *class*, don't use it. I am sure you can
see that the method you have started with is doomed to failure for a bank
with several thousand accounts. Wikipedia is often a great help is CSci
problems.
It is even possible that the instructor neglected to teach struct yet. Such
is life with real people.
>> even some direction to some online reading materials which would be
>> relevant to this would be more than appreciated.
>>
>> not looking for specifics just some subtle guidance :)
>
> Well, if you didn't go through user-defined types (which is rather strange
> because that's the bread-and-butter of C++), you could simply define an
> array of ten account number, ten account balances, ten account names
> (account holder names), ten whatever else you want or see relevant to your
> task...
Gee, it's been so long since I did that that I completely forgot about the
approach. Self taught programmers sometimes use this technique, it is
called "parallel arrays". This went out with the Volkswagen beetle. (sp?)
Hmmm. I suppose any C++ programmer should be able to recognise arrays and
understand roughly how they work, but using them in this day and age seems
questionable to say the least. If you don't use the Standard Template
Library (including std::vector) then I think you'll be missing out on many
of the best features available to C++ programmers. Apart from being more
reliable, its so much more enjoyable using the STL.
Chris Gordon-Smith
www.simsoup.info
It is quite possible that the OP's instructor is self-taught. Otherwise
he would have started with UDTs instead of arrays or pointers or any
other C cruft. Keep in mind that the OP has to conform to the
requirements the course has. In real life everything is different.
>
>> I don't have any "online reading materials". Didn't your course
>> instructor give you anything?
>>
>> V
>> --
>> Please remove capital 'A's when replying by e-mail
>> I do not respond to top-posted replies, please don't ask
Consider reduced quoting, even if you're in a hurry to stick your word
in before somebody else. It doesn't really take long to trim quoting
away to the normal level, does it?
Your instructor, or your book should explain how to create "an
appropriate type definition to store the name, account number and
balance of a bank account." Hint, it will involve the "struct"
keyword.
Based on the rest of your post, the above [not knowing how to define a
type] is your basic problem...
Programmer defined types are compositions of other, already defined
types (both programmer defined and intrinsic.)
For example you might make a "person" type:
struct Person {
string firstName;
string lastName;
};
Now when you define a Person object, it will have both a firstName and
a lastName. Like this:
int main() {
Person p;
p.firstName = "Joe";
p.lastName = "Cool";
// do something with 'p'
}
You can also pass objects of this type to functions and return them
from functions:
void print( Person p );
Person findTeacherForRoom( int i );
> "Victor Bazarov" write:
[..]
>> define an array of ten account number, ten account balances, ten
>> account names (account holder names), ten whatever else you want or
>> see relevant to your task...
>
> Gee, it's been so long since I did that that I completely forgot about
> the approach. Self taught programmers sometimes use this technique,
> it is called "parallel arrays". This went out with the Volkswagen
> beetle. (sp?)
In some applications (high throughput analysis) this approach is still
used, in the form of tables composed of column arrays. This is beneficial
if one routinely performs operations on whole columns. The cache locality
is better and the functions operating on the array do not need to care
about what other data there is in the table.
Regards
Paavo
Humm... Well, if I were teaching a C++ class on basic data-structures... I
would probably introduce it to my students like:
Okay students, today we are going to learn about a single basic
data-structure. I suppose most of you know what the `std::vector' is; am I
right? Raise your hand if you don't know what `std::vector' is... Those of
you with raised hands might have an upper hand ;) I assume you are most
comfortable with C. The master rule for this C++ class is that you CANNOT
make use of ANYTHING under the `std::' namespace! You will use the `my_std'
instead. Humm... `my_std' namespace does not exist... Well, your going to
create it. The STL can be implemented in pure C++ and that's exactly what
were are going to do!
Your first goal is to implement `my_std::vector' such that it is completely
interchangeable with the syntax `std::vector<T>'. Those of you who
understand that a `std::allocator' can be user-defined and passed as a
template argument need to realize that `std::allocator' implementation
teachings occurs the basic data-structure implementation phase.
Then I would teach my students exactly how to implement `my_std::vector'.
After that I will force them to create a basic applications using
`my_std::vector' to show them the power of abstraction. `my_std::vector' is
basically a type-safe simple to use abstraction over a raw array
implementation...
> If you don't use the Standard Template
> Library (including std::vector) then I think you'll be missing out on many
> of the best features available to C++ programmers. Apart from being more
> reliable, its so much more enjoyable using the STL.
Agreed. However, IMVHO, the need to understand how to actually create
`std::vector', `std::allocator' aside, from scratch is fundamental. If you
don't know how to do that, then, we have an issue. Anyway, this view comes
from my on personal bias... See, I am a C programmer at heart!
;^o
Last sentence should read as:
Those of you who understand that a `std::allocator' can be user-defined and
passed as a
template argument need to realize that `std::allocator' implementation
teachings occur _AFTER_ the basic data-structure implementation phase...
[...]
> > Gee, it's been so long since I did that that I completely
> > forgot about the approach. Self taught programmers
> > sometimes use this technique, it is called "parallel
> > arrays". This went out with the Volkswagen beetle. (sp?)
Let's hope it doesn't suffer a similar comeback. (It actually
went out when Fortran introduced used defined types.)
> In some applications (high throughput analysis) this approach
> is still used, in the form of tables composed of column
> arrays. This is beneficial if one routinely performs
> operations on whole columns. The cache locality is better and
> the functions operating on the array do not need to care about
> what other data there is in the table.
On some processors, multiplying by a power of two in the address
calculation may be significantly faster than multiplying by some
arbitrary numer. The basic types will all have sizes which are
a power of two; a user defined type probably won't.
Still, we're talking about very exotic optimizing techniques
here, not things that a beginner should start with.
--
James Kanze (GABI Software) email:james...@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
> int main() {
> Person p;
> p.firstName = "Joe";
> p.lastName = "Cool";
>
> // do something with 'p'
>
> }
>
> You can also pass objects of this type to functions and return them
> from functions:
>
> void print( Person p );
> Person findTeacherForRoom( int i );
okay.
if I have this correct, then i could use the account munber as the
person, i.e.:
int main() {
Person 10001; //10001 being the account number
10001.firstName = "Joe";
10001.lastName = "Cool";
}
could I then add "10001.balance = 250;"
also, in the program I would like to start it with "please enter
account number: " and for this to access the "person 10001"'s details
so that the balance may be added to/subtracted from and also to be
able to set this to 0 (all these i should be able to cover) but also
to change the balance with "cin"
i then need to have all acounts printing to screen and an additional
option to print to file. to print to screen the average (mean) balance
over all accounts, the account numbers and balances of all account <0
and also tho print to screen the account numbers and balances of the
'n' richest accounts.
i'm guesisng not, as 10001.f is being highlighted as red, assuming
that this cannot be a numerical field
struct Account
{
string fName;
string lName
float balance;
};
void main ()
{
Account 10001, 10002, 10003, 10004, 10005, 10006, 10007, 10008, 10009,
10010;
10001.fName = "joe";
10001.lName = "cool";
10001.balance = 250;
was my basic understanding of this other than the numerical struct
10001 is not a valid identifier, you can't use '10001' as a variable
name. You can, of course, do
Person a10001;
...
#include <iostream>
> #include <stdlib.h>
#include <cstdlib>
using namespace std;
>
> struct Account
> {
> string fName;
> string lName
> float balance;
> };
>
> void main ()
int main ()
> {
>
> Account 10001, 10002, 10003, 10004, 10005, 10006, 10007, 10008, 10009,
> 10010;
Account a10001, a10002, ...
>
> 10001.fName = "joe";
> 10001.lName = "cool";
> 10001.balance = 250;
>
> was my basic understanding of this other than the numerical struct
Close enough.
awesome :)
now; the following is my basic bank account program. all working and
fully tested. how would i get it to "load" (if that's the right word)
a10001's (or any other Account) details to manipulate through this?
cout<< "Do you wish to:"<<endl;
cout<< "1: Deposit"<<endl;
cout<< "2: Withdraw"<<endl;
cout<< "3: View Balance"<<endl;
cout<< "4: Exit" <<endl;
cin>>choice;
while ((choice != 1) && (choice != 2) && (choice != 3) && (choice !=
4))
{
cout<<"you have input and invalid choice, try again"<<endl;
cout<< "Do you wish to:"<<endl;
cout<< "1: Deposit"<<endl;
cout<< "2: Withdraw"<<endl;
cout<< "3: View Balance"<<endl;
cout<< "4: Exit" <<endl;
cin>>choice;
}
while (choice != 4)
{
if (choice == 1)
{
cout<<"Please enter deposit amount: " <<endl;
cin>>amount;
account = account + amount;
cout<<"New balance: "<<account<<endl;
}//end if 1
else if (choice == 2)
{
cout<<"Please enter withdrawl amount: " <<endl;
cin>>amount;
account = account - amount;
cout<<"New balance: "<<account<<endl;
}//end if 2
else if(choice == 3)
{
cout<<"The account balance is: ";
cout<<account<<endl;
}//end if 3
cout<< "Do you wish to:"<<endl;
cout<< "1: Deposit"<<endl;
cout<< "2: Withdraw"<<endl;
cout<< "3: View Balance"<<endl;
cout<< "4: Exit" <<endl;
cin>>choice;
while ((choice != 1) && (choice != 2) && (choice != 3) && (choice !=
4))
{
cout<<"you have input and invalid choice, try again"<<endl;
cout<< "Do you wish to:"<<endl;
cout<< "1: Deposit"<<endl;
cout<< "2: Withdraw"<<endl;
cout<< "3: View Balance"<<endl;
cout<< "4: Exit" <<endl;
cin>>choice;
}
}//end while
}//end main
...or
Person l00O1
...or any other combination of 0s and Os. This is one of the worst
cases of unnecessary obfuscation.
Error: softdev1ass2.cpp(2,2):Unable to open include file 'CSTDLIB.h'
>
> using namespace std;
Error: softdev1ass2.cpp(3,16):Namespace name expected
Using Borland 5.02 :/
Who said anything about .h?
>
>> using namespace std;
>
> Error: softdev1ass2.cpp(3,16):Namespace name expected
>
>
> Using Borland 5.02 :/
Get a better compiler.
the complier
> Get a better compiler.
open to suggestions :)
Microsoft Visual C++ Express Edition 2008. Free. Download, install and
work in one of the best development environments around.
I have the 2005 edition already installed it seems :)
stupid question #1: how do i compile in this environment?
stupid question#2: how do i run what i have wrote?
thanks in advance
Now you can make an array and maybe use the index in the array as the
account number:
#include <string>
#include <iostream>
struct Account{
std::string fName;
std::string lName;
float balance;
};
int main()
{
// one way to create and fill array of accounts
Account Accounts [] =
{
{"John","Smith",1000.0},
{"Mary","Jones",2000.0},
{"Bill","Simpkins",20000.0}
};
// one way to know how many accounts were created for this type of array
// use of static is a technicality which you could find out about later
/*static */const int NumAccounts = sizeof(Accounts)/ sizeof(Account);
// for here means go forever unless you get a valid account number
for (;;){
std::cout << "Please enter your account number : ";
//maybe start with invalid account number
int AccountNumber=-1;
// read in from user
std::cin >> AccountNumber ;
std::cout << "\n\n";
if( (AccountNumber >= 0) && (AccountNumber < NumAccounts)){
std::cout << "Thankyou "
<< Accounts[AccountNumber].fName
<< " "
<< Accounts[AccountNumber].lName
<< "\n. You have a balance of "
<< Accounts[AccountNumber].balance
<< " euros in your account\n";
break; // break out of loop
}
else{
std::cout << "Sorry, This isnt a valid account number...\n";
continue; // go round the loop again
}
}
}
regards
Andy Little
He better stick with what he has been given. If that is the compiler his
tutor uses then he had better just plug away until he getes it to work on
That compiler :-)
regards
Andy Little
[SNIP]
Following the rest of this thread it seems that you don't have a good
understanding what "appropriate type definition" or how to use it in an
array. Since this is homework, I can't/won't give you the answer, but look
at this:
#include <string>
struct Foo
{
std::string Name;
int Quantity;
float Value;
};
int main()
{
Foo Data[10];
Data[0].Name = "Screw";
Data[0].Quantity = 10;
Data[0].Value = 12.3;
Data[1].Name = "Bolt";
Data[1].Quantity = 20;
Data[1].Value = 3.1415926;
}
That is basically giving you everything right there as far as your data
goes. I probably gave you more information that I should have, but you
seemed rather lost.
--
Jim Langston
tazm...@rocketmail.com
F7
> stupid question#2: how do i run what i have wrote?
F5
Also, visit news:microsoft.public.vc.ide_general and other forums.
#include <iostream.h>
#include <stdlib.h>
#include <string>
struct Account{
std::string fName;
std::string lName;
float balance;
};
int main ()
{
Account Accounts [] =
{
{"Joe","Smithers",250.0},
{"Anesa","Williams",-500.0},
{"Sarah","Jacobs",1204.0},
{"Daniel","Lewis",-600.0},
{"Sabrina","Le Rue",789.0},
{"Gordon","Platt",-2987.0},
{"Tom","Jones",8721.0},
{"David","Brown",-20.0},
{"Lucy","Thomas",-3988.0},
{"Jenna","Allen",10000.0}
};
std::cout << "Please enter your account number : ";
//maybe start with invalid account number
int AccountNumber=-1;
std::cin >> AccountNumber ;
std::cout << "\n\n";
****************************************************************************
Info :Compiling C:\Users\G\Desktop\SoftDev1Ass2\softdev1ass2.cpp
Warn : string.h(549,3):Functions containing for are not expanded
inline
Warn : string.h(557,3):Functions containing while are not expanded
inline
Warn : string.h(563,3):Functions containing for are not expanded
inline
Warn : string.h(575,3):Functions containing for are not expanded
inline
Warn : string.h(742,96):Conversion may lose significant digits
Warn : string.h(768,96):Conversion may lose significant digits
Warn : iterator.h(570,72):Conversion may lose significant digits
Warn : iterator.h(570,72):Conversion may lose significant digits
Warn : iterator.h(529,72):Conversion may lose significant digits
Warn : iterator.h(530,72):Conversion may lose significant digits
Warn : iterator.h(531,72):Conversion may lose significant digits
Warn : iterator.h(532,72):Conversion may lose significant digits
Warn : iterator.h(529,72):Conversion may lose significant digits
Warn : iterator.h(530,72):Conversion may lose significant digits
Warn : iterator.h(531,72):Conversion may lose significant digits
Warn : iterator.h(532,72):Conversion may lose significant digits
Error: softdev1ass2.cpp(18,9):Cannot convert 'char *' to 'Account'
Error: softdev1ass2.cpp(18,10):} expected
Error: softdev1ass2.cpp(18,26):Declaration syntax error
Error: softdev1ass2.cpp(18,26):Declaration missing ;
Warn : softdev1ass2.cpp(18,26):'account' is assigned a value that is
never used
Warn : softdev1ass2.cpp(18,26):'choice' is declared but never used
Warn : softdev1ass2.cpp(18,26):'amount' is declared but never used
Error: softdev1ass2.cpp(18,27):Declaration terminated incorrectly
Error: softdev1ass2.cpp(28,3):Unexpected }
Error: softdev1ass2.cpp(32,10):'cout' is not a member of 'std'
Error: softdev1ass2.cpp(32,13):Declaration terminated incorrectly
Error: softdev1ass2.cpp(35,13):'cin' is not a member of 'std'
Error: softdev1ass2.cpp(35,16):Declaration terminated incorrectly
Error: softdev1ass2.cpp(36,14):'cout' is not a member of 'std'
Error: softdev1ass2.cpp(36,17):Declaration terminated incorrectly
Error: softdev1ass2.cpp(100,2):Unexpected }
>...but you seemed rather lost.
this is a huge under-statement, everytime i get a response (which has
been A1 from everyone) it just throws spanners into the works
OK. Well I think the problem is that you have a very old compiler. In that
case you have to start at the bottom and see what it will find acceptable.
Start off with code that AFAIK is basically C code: I've removed std::string
and also used an alternative way to initialise.
BTW Generally when debugging you should only change one thing at a time, but
I changed 2 :-)
struct Account{
const char* fName; // mod
const char* lName; //mod
float balance;
};
int main()
{
// one way to create and fill array of accounts
Account Accounts[] =
{ // mods...
"John","Smith",1000.0,
"Mary","Jones",2000.0,
"Bill","Simpkins",20000.0
};
}
See if the compiler is happy with that. BTW just ignore anything that says
WARN. You only want to look at ERROR... but only at this "go or nogo" stage.
regards
Andy Little
Error: (1,1):Undefined symbol std::rwse_StringIndexOutOfRange in
module softdev1ass2.cpp
Error: (1,1):Undefined symbol std::rwse_PosBeyondEndOfString in
module softdev1ass2.cpp
Error: (1,1):Undefined symbol std::nullref in module softdev1ass2.cpp
Error: (1,1):Undefined symbol std::rwse_InvalidSizeParam in module
softdev1ass2.cpp
Error: (1,1):Undefined symbol std::rwse_ResultLenInvalid in module
softdev1ass2.cpp
Error: (1,1):Undefined symbol std::rwse_UnexpectedNullPtr in module
softdev1ass2.cpp
Error: (1,1):Undefined symbol std::__rw_stdexcept_NoNamedException in
module softdev1ass2.cpp
*pulls hair out* :P
it seems #include <string> was messing it up
which compiler is this... Borland or Microsoft VC++?
regards
Andy Little
Borland 5.02 (1997) :/
#include <iostream>
> #include <stdlib.h>
#include <cstdlib>
> #include <string>
--
Jim Langston
tazm...@rocketmail.com
> YES!
>
> it seems #include <string> was messing it up
That seems highly unlikely, as that is the header required for
std::string.
Brian
Fascinating... (I've never used Borland. I was warned off it many years ago
by an old pro ...:-) )
If you could recompile and also show us the *exact, full* source code you
are compiling and include a couple of line numbers in comments on the very
lines they refer to, ( before you compile !!! :-) ) in the source code so we
can see where it says the errors are..
regards
Andy Little
#include <iostream.h>
#include <stdlib.h>
#include <string>
struct Account{
std::string fName; //5
std::string lName; //6
float balance;
};
int main ()
{
int amount, choice, account = 0;
Account Accounts [] =
{
{"Joe","Smithers",250.0, //18
{"Anesa","Williams",-500},
{"Sarah","Jacobs",1204},
{"Daniel","Lewis",-600},
{"Sabrina","Le Rue",789},
{"Gordon","Platt",-2987},
{"Tom","Jones",8721},
{"David","Brown",-20},
{"Lucy","Thomas",-3988},
{"Jenna","Allen",10000},
}; //28
std::cout << "Please enter your account number : "; //33
//maybe start with invalid account number
int AccountNumber=-1;
// read in from user
std::cin >> AccountNumber ; //39
std::cout << "\n\n"; //41
}
Info :Compiling C:\Users\G\Desktop\SoftDev1Ass2\softdev1ass2.cpp
Warn : string.h(549,3):Functions containing for are not expanded
inline
Warn : string.h(557,3):Functions containing while are not expanded
inline
Warn : string.h(563,3):Functions containing for are not expanded
inline
Warn : string.h(575,3):Functions containing for are not expanded
inline
Warn : string.cc(686,32):Comparing signed and unsigned values
Error: softdev1ass2.cpp(18,10):Cannot convert 'char *' to 'Account'
Error: softdev1ass2.cpp(18,11):} expected
Error: softdev1ass2.cpp(28,3):Declaration syntax error
Error: softdev1ass2.cpp(28,3):Declaration missing ;
Error: softdev1ass2.cpp(33,10):'cout' is not a member of 'std'
Error: softdev1ass2.cpp(33,13):Declaration terminated incorrectly
Error: softdev1ass2.cpp(39,13):'cin' is not a member of 'std'
Error: softdev1ass2.cpp(39,16):Declaration terminated incorrectly
Error: softdev1ass2.cpp(41,14):'cout' is not a member of 'std'
Error: softdev1ass2.cpp(41,17):Declaration terminated incorrectly
Error: softdev1ass2.cpp(43,6):Unexpected }
Error: softdev1ass2.cpp(106,2):Unexpected }
> Error: softdev1ass2.cpp(18,11):} expected
--
Erik Wikström
<... snip preamble stuff>
> int main ()
> {
>
> int amount, choice, account = 0;
>
> Account Accounts [] =
> {
> {"Joe","Smithers",250.0, //18
> {"Anesa","Williams",-500},
> {"Sarah","Jacobs",1204},
> {"Daniel","Lewis",-600},
> {"Sabrina","Le Rue",789},
> {"Gordon","Platt",-2987},
> {"Tom","Jones",8721},
> {"David","Brown",-20},
> {"Lucy","Thomas",-3988},
> {"Jenna","Allen",10000},
> }; //28
<.. snip the warnings>
> Error: softdev1ass2.cpp(18,10):Cannot convert 'char *' to 'Account'
> Error: softdev1ass2.cpp(18,11):} expected
OK the first error message is confusing, but the second one is pretty clear
and does actually point to an obvious mistake in your typing ( a 'typo')....
Bear in mind that the compiler is looking at a sequence of tokens to compile
the program. If you make a very simple mistake such as the above it throws
the compiler into total confusion, but the compiler bravely tries to carry
on. So when you make a simple mistake you can often get a huge number of
error messages for one simple mistake.
... Welcome to C++ ... :-)
but its still unclear whether Borland 5.0 97' accepts that method of
initialising... (it should do as its C language style AFAIK)
Anyway what you generally need to do is fix that mistake, recompile, get a
load of error messages, fix that mistake , etc, etc... One day way in the
future you will fall of your chair with surprise... no errors :-)
as I said ... Welcome to C++... er... hmm... Borland 5.0 97' style ... :-)
regards
Andy Little
#include <iostream.h>
#include <stdlib.h>
struct Account{
const char* Name;
float Balance;
};
int main ()
{
Account Accounts [] =
{
"Joe Smithers",250.0,
"Anesa Williams",-500.0,
"Sarah Jacobs",1204.0,
"Daniel Lewis",-600.0,
"Sabrina Le Rue",789.0,
"Gordon Platt",-2987.0,
"Tom Jones",8721.0,
"David Brown",-20.0,
"Lucy Thomas",-3988.0,
"Jenna Allen",10000.0
};
const int NumAccounts = sizeof(Accounts)/ sizeof(Account);
int amount, choice, AccountNumber=-1;
cout<< "Please enter an account number: ";
cin>> AccountNumber ;
cout<< "\n";
if( (AccountNumber >= 0) && (AccountNumber < NumAccounts))
{
cout<< "Thank you "
<<Accounts[AccountNumber].Name ;
cout<<"\n\n";
cout<< "Do you wish to:"<<endl;
cout<< "1: Deposit"<<endl;
cout<< "2: Withdraw"<<endl;
cout<< "3: View Balance"<<endl;
cout<< "4: Exit"<<endl;
cin>>choice;
}
else
{
cout << "Sorry, This isnt a valid account number.\n";
cout << "Please enter an account number: ";
cin >> AccountNumber ;
cout << "\n\n";
while ((choice != 1) && (choice != 2) && (choice != 3) &&
(choice != 4))
{
cout<<"you have input and invalid choice, try again"<<endl;
cout<< "Do you wish to:"<<endl;
cout<< "1: Deposit"<<endl;
cout<< "2: Withdraw"<<endl;
cout<< "3: View Balance"<<endl;
cout<< "4: Exit" <<endl;
cin>>choice;
}
while (choice != 4)
{
if (choice == 1)
{
cout<<"Please enter deposit amount: " <<endl;
cin>>amount;
Accounts[AccountNumber].Balance =
Accounts[AccountNumber].Balance + amount;
cout<<"New balance:
"<<Accounts[AccountNumber].Balance<<endl;
}//end if 1
else if (choice == 2)
{
cout<<"Please enter withdrawl amount: " <<endl;
cin>>amount;
Accounts[AccountNumber].Balance =
Accounts[AccountNumber].Balance - amount;
cout<<"New balance:
"<<Accounts[AccountNumber].Balance<<endl;
}//end if 2
else if(choice == 3)
{
cout<<"The account balance is: ";
cout<<Accounts[AccountNumber].Balance<<endl;
}//end if 3
cout<< "Do you wish to:"<<endl;
cout<< "1: Deposit"<<endl;
cout<< "2: Withdraw"<<endl;
cout<< "3: View Balance"<<endl;
cout<< "4: Exit" <<endl;
cin>>choice;
while ((choice != 1) && (choice != 2) && (choice != 3) && (choice !=
4))
{
cout<<"you have input and invalid choice, try again"<<endl;
cout<< "Do you wish to:"<<endl;
cout<< "1: Deposit"<<endl;
cout<< "2: Withdraw"<<endl;
cout<< "3: View Balance"<<endl;
cout<< "4: Exit" <<endl;
cin>>choice;
}
}
}
}
and then.... DISASTER!
so my program opens, i enter the account number (0-9) the menu pops up
the choice is entered and then the program stops i've been poring over
it and can't seen to find the crux
Thank you guys so much for all your help!
But I’m not quite out of the trees :/
I need to print all account numbers (0-90) with their balances to
screen in one, and also print to a file :S
Average all balances
Print all accounts and balances "<0" to screen
And also print to screen a user entered number of accounts and
balances with the most money in
I guess what I’m asking is how do I now manipulate the data I have en
masse? I’m pretty hopeful if I see at least how to do one, the rest
will follow pretty simply :)
> I guess what I雋 asking is how do I now manipulate the data I have
> en masse?
With loops.
> I雋 pretty hopeful if I see at least how to do one, the rest will
> follow pretty simply :)
int average_of( int arr[], int length )
{
int result = 0;
for ( int i = 0; i < length; ++i )
result += arr[i];
return result / length;
}
That should give you an idea of what to do.
... and add some function()s to .. :-)
regards
Andy Little