Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Arrays?

Reply
Thread Tools

Arrays?

 
 
gbvk@hotmail.co.uk
Guest
Posts: n/a
 
      05-28-2008


> 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.
 
Reply With Quote
 
 
 
 
gbvk@hotmail.co.uk
Guest
Posts: n/a
 
      05-28-2008

> 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;"


i'm guesisng not, as 10001.f is being highlighted as red, assuming
that this cannot be a numerical field

 
Reply With Quote
 
 
 
 
gbvk@hotmail.co.uk
Guest
Posts: n/a
 
      05-28-2008
#include <iostream.h>
#include <stdlib.h>

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
 
Reply With Quote
 
gbvk@hotmail.co.uk
Guest
Posts: n/a
 
      05-28-2008
> Person a10001;
> ...



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
 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      05-28-2008
Victor Bazarov <(E-Mail Removed)> writes:
>10001 is not a valid identifier, you can't use '10001' as a variable
>name. You can, of course, do
> Person a10001;


or

Person l000l;

 
Reply With Quote
 
gbvk@hotmail.co.uk
Guest
Posts: n/a
 
      05-28-2008
> > #include <stdlib.h>
>
> #include <cstdlib>


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 :/
 
Reply With Quote
 
gbvk@hotmail.co.uk
Guest
Posts: n/a
 
      05-28-2008
> Who said anything about .h?

the complier

> Get a better compiler.


open to suggestions
 
Reply With Quote
 
gbvk@hotmail.co.uk
Guest
Posts: n/a
 
      05-28-2008
> 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
 
Reply With Quote
 
kwikius
Guest
Posts: n/a
 
      05-28-2008

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>> Person a10001;
>> ...

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


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

 
Reply With Quote
 
kwikius
Guest
Posts: n/a
 
      05-28-2008

"Victor Bazarov" <(E-Mail Removed)> wrote in message
news:g1k8aq$q64$(E-Mail Removed)...
> http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
>>>> #include <stdlib.h>
>>> #include <cstdlib>

>>
>> Error: softdev1ass2.cpp(2,2):Unable to open include file 'CSTDLIB.h'

>
> Who said anything about .h?
>
>>
>>> using namespace std;

>>
>> Error: softdev1ass2.cpp(3,16):Namespace name expected
>>
>>
>> Using Borland 5.02 :/

>
> Get a better compiler.


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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments