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

Open discussion about any topic, as long as you abide by the rules of course!
Post Reply
losCHUNK
Posts: 16019
Joined: Thu May 09, 2002 7:00 am

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

Post 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 !)
[color=red] . : [/color][size=85] You knows you knows [/size]
Nightshade
Posts: 17020
Joined: Fri Dec 01, 2000 8:00 am

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

Post by Nightshade »

Use a string instead? Just kinda guessing here, C++ class was a while ago and I'm all LabVIEW now.
losCHUNK
Posts: 16019
Joined: Thu May 09, 2002 7:00 am

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

Post 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 :/
[color=red] . : [/color][size=85] You knows you knows [/size]
bitWISE
Posts: 10704
Joined: Wed Dec 08, 1999 8:00 am

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

Post 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.
User avatar
duffman91
Posts: 1278
Joined: Thu Jan 25, 2001 8:00 am

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

Post 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?
User avatar
duffman91
Posts: 1278
Joined: Thu Jan 25, 2001 8:00 am

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

Post 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
^misantropia^
Posts: 4022
Joined: Sat Mar 12, 2005 6:24 pm

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

Post 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.
losCHUNK
Posts: 16019
Joined: Thu May 09, 2002 7:00 am

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

Post 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 ?
[color=red] . : [/color][size=85] You knows you knows [/size]
^misantropia^
Posts: 4022
Joined: Sat Mar 12, 2005 6:24 pm

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

Post by ^misantropia^ »

Use std::string instead of plain string.
losCHUNK
Posts: 16019
Joined: Thu May 09, 2002 7:00 am

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

Post 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
[color=red] . : [/color][size=85] You knows you knows [/size]
phoq
Posts: 238
Joined: Wed May 03, 2000 7:00 am

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

Post 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;".
phoq
Posts: 238
Joined: Wed May 03, 2000 7:00 am

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

Post by phoq »

You might have to include stdlib or cstdlib (C++) if I remember correctly, been a couple of years now.
losCHUNK
Posts: 16019
Joined: Thu May 09, 2002 7:00 am

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

Post 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:
[color=red] . : [/color][size=85] You knows you knows [/size]
User avatar
duffman91
Posts: 1278
Joined: Thu Jan 25, 2001 8:00 am

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

Post by duffman91 »

Out of curiosity: what compiler are you using?
DiscoDave
Posts: 1645
Joined: Wed Feb 09, 2005 4:33 pm

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

Post by DiscoDave »

Just as an observationm why don't you use classes and possibly a vector for storing the info?
losCHUNK
Posts: 16019
Joined: Thu May 09, 2002 7:00 am

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

Post 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
[color=red] . : [/color][size=85] You knows you knows [/size]
DiscoDave
Posts: 1645
Joined: Wed Feb 09, 2005 4:33 pm

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

Post 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 :)
losCHUNK
Posts: 16019
Joined: Thu May 09, 2002 7:00 am

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

Post by losCHUNK »

last question -

how would i output to screen the 3 accounts with the highest balance ?
[color=red] . : [/color][size=85] You knows you knows [/size]
Nightshade
Posts: 17020
Joined: Fri Dec 01, 2000 8:00 am

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

Post 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:
DiscoDave
Posts: 1645
Joined: Wed Feb 09, 2005 4:33 pm

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

Post 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.
^misantropia^
Posts: 4022
Joined: Sat Mar 12, 2005 6:24 pm

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

Post by ^misantropia^ »

Look up std::sort(), lives in <algorithm>.
losCHUNK
Posts: 16019
Joined: Thu May 09, 2002 7:00 am

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

Post by losCHUNK »

heheh all done.... thanks guys :D
[color=red] . : [/color][size=85] You knows you knows [/size]
Post Reply