Arrays?

J

Jim Langston

Hi,

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:

[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.
 
G

gbvk

Thank you kwikius, but i seem to get bombarded with error messages
with this:

#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
 
K

kwikius

Thank you kwikius, but i seem to get bombarded with error messages
with this:

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
 
G

gbvk

Info :Compiling C:\Users\G\Desktop\SoftDev1Ass2\softdev1ass2.cpp

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
 
K

kwikius

Info :Compiling C:\Users\G\Desktop\SoftDev1Ass2\softdev1ass2.cpp

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

which compiler is this... Borland or Microsoft VC++?

regards
Andy Little
 
J

Jim Langston

Thank you kwikius, but i seem to get bombarded with error messages
with this:

#include <iostream.h>

#include said:
#include <stdlib.h>

#include said:
#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
 
K

kwikius

Borland 5.02 (1997) :/

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
 
G

gbvk

well i took out #include <string> and all the std:: before cout and
cin and it workes. sod's law - just before a crash, so i have a saved
state before i changed it.

#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 }
 
E

Erik Wikström

well i took out #include <string> and all the std:: before cout and
cin and it workes. sod's law - just before a crash, so i have a saved
state before i changed it.

#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
^^^
Missing a } at the end of the line. Which the error-message below points
out.
 
K

kwikius

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
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
 
G

gbvk

finally feel like i'm getting somewhere:


#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
 
G

gbvk

Right, all is working :)

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 :)
 
G

gbvk

Right, all is working :)

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 :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top