Page 1 of 1

give me some c++ homework help plz :D (newb newb)

Posted: Mon May 26, 2008 4:59 pm
by losCHUNK
gotta make a bank account and i have most of it up and running but i cant figure out how to store the name of the account :/

----------------------------------------------

typedef struct bala
{

int number;
float account;
char name[15];

}bala;


void main()
{
char selection;

bala balance[10];
bala name[10];

balance[0].number = 1;
balance[1].number = 2;
balance[2].number = 3;
balance[3].number = 4;
balance[4].number = 5;
balance[5].number = 6;
balance[6].number = 7;
balance[7].number = 8;
balance[8].number = 9;
balance[9].number = 0;

balance[0].name = "Paul";
balance[1].name = "Tom";
balance[2].name = "Soph";
balance[3].name = "Susie";
balance[4].name = "Steven";
balance[5].name= "Hank";
balance[6].name= "Samantha";
balance[7].name= "Toby";
balance[8].name= "Lilly";
balance[9].name= "Ben";


balance[0].account = 1;
balance[1].account = 2;
balance[2].account = 4;
balance[3].account = -8;
balance[4].account = 16;
balance[5].account = 32;
balance[6].account = 64;
balance[7].account = -128;
balance[8].account = 256;
balance[9].account = 512;



----------------------------

im just trying to get it so that when the person logs in with the account number a greeting with their name appears, any suggestions ? this wont compile it says lvalue required where im declaring the names :( (i could get this working if peoples names were numbers !)

Re: give me some c++ homework help plz :D (newb newb)

Posted: Mon May 26, 2008 5:33 pm
by Nightshade
Use a string instead? Just kinda guessing here, C++ class was a while ago and I'm all LabVIEW now.

Re: give me some c++ homework help plz :D (newb newb)

Posted: Mon May 26, 2008 6:17 pm
by losCHUNK
your the 2nd person to say that now :L

anyone got any tutorials on strings ?, ive never used them before and im having a bit of a headache on how im gunna squeeze it in with my code :/

Re: give me some c++ homework help plz :D (newb newb)

Posted: Mon May 26, 2008 6:23 pm
by bitWISE

Code: Select all

typedef struct {
	int number;
	float account;
	char name[];
} bala;

void main() {
	char selection;
	bala balance[10];
	
	balance[0].number = 1;
	balance[1].number = 2;
	balance[2].number = 3;
	balance[3].number = 4;
	balance[4].number = 5;
	balance[5].number = 6;
	balance[6].number = 7;
	balance[7].number = 8;
	balance[8].number = 9;
	balance[9].number = 0;

	balance[0].name = "Paul";
	balance[1].name = "Tom";
	balance[2].name = "Soph";
	balance[3].name = "Susie";
	balance[4].name = "Steven";
	balance[5].name = "Hank";
	balance[6].name = "Samantha";
	balance[7].name = "Toby";
	balance[8].name = "Lilly";
	balance[9].name = "Ben";

	balance[0].account = 1;
	balance[1].account = 2;
	balance[2].account = 4;
	balance[3].account = -8;
	balance[4].account = 16;
	balance[5].account = 32;
	balance[6].account = 64;
	balance[7].account = -128;
	balance[8].account = 256;
	balance[9].account = 512;
}
Try that.

Re: give me some c++ homework help plz :D (newb newb)

Posted: Mon May 26, 2008 7:05 pm
by duffman91
To use a string, you have to declare it. So:

Code: Select all

using namespace std; 
string inputString;



To read in a string it's simple:

cin >> inputString;

to print:


cout << inputString;
Anyways, here's a website with a really nice, simple break down. http://www.cprogramming.com/tutorial/string.html

Remember though, if you have not been taught strings, you might get an F in the assignment. Are you limited to using a character array for input and output?

Re: give me some c++ homework help plz :D (newb newb)

Posted: Mon May 26, 2008 7:10 pm
by duffman91
If you need an integer value to declare the name, why not use the ASCII values of the characters?

I have a feeling this is way simpler, but it's a thought. (Two dimensional character arrays would work....)

Read this (it'll explain what I'm getting at with two dimensional arrays):

http://www.space.unibe.ch/comp_doc/c_ma ... .html#char

Re: give me some c++ homework help plz :D (newb newb)

Posted: Mon May 26, 2008 7:25 pm
by ^misantropia^

Code: Select all

typedef struct {
	/* snip */
	char name[];
} bala;
This won't work. name[] means that you, the programmer, are responsible for allocating memory for the struct + the size of the string, but you fail to do so. Correct usage:

Code: Select all

void foo(const char *string) {
    bala *bp = malloc(sizeof(*bp) + strlen(string) + 1 /* trailing nul byte */);
    strcpy(bp->name, string);
    /* and don't forget to free() bp later on */
}

Code: Select all

void main() {
main() should always, always, always be defined as a function returning an int. In C, that also means you need to return an integer. In C++, as a special case for main(), the compiler will silently insert a return 0 for you.

Re: give me some c++ homework help plz :D (newb newb)

Posted: Mon May 26, 2008 8:27 pm
by losCHUNK
ok i have to use strings as it seems, i cant remember being taught them, been playing with them for the last few hrs though >:E

quick question, if i just put

#include <string>

in the header or

string name;

into the code is there any reason why it wouldnt compile ?

Re: give me some c++ homework help plz :D (newb newb)

Posted: Mon May 26, 2008 8:35 pm
by ^misantropia^
Use std::string instead of plain string.

Re: give me some c++ homework help plz :D (newb newb)

Posted: Mon May 26, 2008 9:00 pm
by losCHUNK
ok, am i on the right track ?

------------------------------------------------------------------

typedef struct bala
{

int number;
float account;
string name;


}bala;


void main()
{
char selection;
bala balance[10];
bala name[10];

int acc;

balance[0].number = 1111;
balance[1].number = 2222;
balance[2].number = 3333;
balance[3].number = 4444;
balance[4].number = 5555;
balance[5].number = 6666;
balance[6].number = 7777;
balance[7].number = 8888;
balance[8].number = 9999;
balance[9].number = 1010;

balance[0].name = "Paul";
balance[1].name = "Tom";
balance[2].name = "Soph";
balance[3].name = "Susie";
balance[4].name = "Steven";
balance[5].name = "Hank";
balance[6].name = "Samantha";
balance[7].name = "Toby";
balance[8].name = "Lilly";
balance[9].name = "Ben";

balance[0].account = 1;
balance[1].account = 2;
balance[2].account = 4;
balance[3].account = -8;
balance[4].account = 16;
balance[5].account = 32;
balance[6].account = 64;
balance[7].account = -128;
balance[8].account = 256;
balance[9].account = 512;

cout<<"Welcome to the Bank System."<<endl<<endl;
acc = choose_account();
cout<<endl<<"Account number accepted."<<endl<<endl;
cout<<endl<<"Hello "<<balance[acc].name<<endl;

-------------------------------------------------------------------------------

string name; keeps giving me an error "type name is expected"

and if i use std::string name; it says.......

Error: mine.cpp(17,4):Qualifier 'std' is not a class or namespace name

Re: give me some c++ homework help plz :D (newb newb)

Posted: Mon May 26, 2008 9:13 pm
by phoq
Putting:
#include <string>
using namespace std;

at the top of the file should work, after that you should be able to declare a string variable by just using "string x;".

Re: give me some c++ homework help plz :D (newb newb)

Posted: Mon May 26, 2008 9:21 pm
by phoq
You might have to include stdlib or cstdlib (C++) if I remember correctly, been a couple of years now.

Re: give me some c++ homework help plz :D (newb newb)

Posted: Mon May 26, 2008 9:30 pm
by losCHUNK
if i put #include <string> in the header i get a few errors

1 saying string is out of range but i can stop that if i use #include <string.h> instead

if i use namespace it says

namespace name expected

included stdlib aswell and cstdlib was unable to be opened :|

thanks though people :up: :up: :up:

Re: give me some c++ homework help plz :D (newb newb)

Posted: Tue May 27, 2008 7:46 am
by duffman91
Out of curiosity: what compiler are you using?

Re: give me some c++ homework help plz :D (newb newb)

Posted: Tue May 27, 2008 10:02 am
by DiscoDave
Just as an observationm why don't you use classes and possibly a vector for storing the info?

Re: give me some c++ homework help plz :D (newb newb)

Posted: Tue May 27, 2008 11:55 am
by losCHUNK
duffman91 wrote:Out of curiosity: what compiler are you using?
borland, same compiler we use in class, managed to get it working after i compile it now, cant get it to run from de-bug though :/

and i know we havnt been taught anything about vectors or classes yet, im pretty sure this is the way he wants to go about it

Re: give me some c++ homework help plz :D (newb newb)

Posted: Tue May 27, 2008 2:53 pm
by DiscoDave
losCHUNK wrote:
duffman91 wrote:Out of curiosity: what compiler are you using?
borland, same compiler we use in class, managed to get it working after i compile it now, cant get it to run from de-bug though :/

and i know we havnt been taught anything about vectors or classes yet, im pretty sure this is the way he wants to go about it
Ahh i see :)

Re: give me some c++ homework help plz :D (newb newb)

Posted: Wed May 28, 2008 10:53 pm
by losCHUNK
last question -

how would i output to screen the 3 accounts with the highest balance ?

Re: give me some c++ homework help plz :D (newb newb)

Posted: Wed May 28, 2008 11:18 pm
by Nightshade
Sort the array of accounts and strip out the three highest, then print 'em. lol, I could code this in LabVIEW in less than 5 minutes, too bad my C++ skills suck. :olo:

Re: give me some c++ homework help plz :D (newb newb)

Posted: Wed May 28, 2008 11:21 pm
by DiscoDave
Damn, Nightshade beat me to it. I'll try and get a C++ compiler working on my Linux laptop and wite some code for it.

Re: give me some c++ homework help plz :D (newb newb)

Posted: Thu May 29, 2008 8:44 am
by ^misantropia^
Look up std::sort(), lives in <algorithm>.

Re: give me some c++ homework help plz :D (newb newb)

Posted: Thu May 29, 2008 3:17 pm
by losCHUNK
heheh all done.... thanks guys :D