Global var in Balance calculation

M

Maria Mela

Hello everyone...
Anybody can help me on this question, please?
i´ve this code in my application:

/*for Card creation*/
typedef struct card
{
etc,etc
} Card;
typedef Card *PtrCrt;

/*For trans creation*/
typedef struct trans
{
etc,etc
} Trans;
typedef Trans *PtrTrans;

I need to calculate the Balance of every card and how put this verification
in one function?!
 
M

Mike Wahler

Maria Mela said:
Hello everyone...
Anybody can help me on this question, please?
i´ve this code in my application:

/*for Card creation*/
typedef struct card
{
etc,etc
} Card;
typedef Card *PtrCrt;

/*For trans creation*/
typedef struct trans
{
etc,etc
} Trans;
typedef Trans *PtrTrans;

I need to calculate the Balance of every card and how put this
verification in one function?!

int balance(const Card *c)
{
int result;
/* calculate */
return result;
}

int main(void)
{
Card c = { /* etc. */ };
printf("balance = %d\n", balance(&c));
return 0;
}

For more specific advice, ask more specific questions.

-Mike
 
J

Jeff Mullen

Maria said:
Hello everyone...
Anybody can help me on this question, please?
i´ve this code in my application:

/*for Card creation*/
typedef struct card
{
etc,etc
} Card;
typedef Card *PtrCrt;

/*For trans creation*/
typedef struct trans
{
etc,etc
} Trans;
typedef Trans *PtrTrans;

I need to calculate the Balance of every card and how put this verification
in one function?!

Please be more specific. What is the balance of a card? What do you
need to verify? How are you representing the aggregation of cards?
As an array? A singly-linked list? Many singly-linked lists?

P.S.

http://www.nls.net/mp/413/sea.ZIP

Self-expanding arrays. (This is the code your college professor
warned you about.) :) :) :)
 
M

Maria Mela

Thks for your help... But to be more specific here´s one part of my code...

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <alloc.h>
#include <string.h>

typedef enum observations
{
Item1=1,Item2,Item3,Item4,Item5

}Observations;


typedef enum trans_type
{
CRG=1, DBT,TRF

}Trans_type;

typedef enum utente
{
Student=1, Functionary ,Professor

}Utente;

typedef struct data
{
int day, month, year;

}Data;


typedef struct card
{
int num_card;
char name[255];
Utente utente;
Data data;
float balance;
char morada[60];
struct card *next;

} Card;

typedef Card *PtrCrt;

typedef struct transaccions
{
int codigo;
char nome[60];
Data data;
Trans_tipo trans_tipo;
float montante,crg;
Observacoes observacoes;
char local_treinos[60];
char nome_treinador[60];
struct trans *next;
PtrCrt p_cartao;

} Trans;

typedef Trans *PtrTrans;
/*------------------------Functions------------------------*/

void Insert_Card (PtrCrt *lista);
void Change_Card (PtrCrt *lista);
void Remove_Card (PtrCrt *lista);
void View_Cards (PtrCrt *lista);
void Delet_Cards (PtrCrt *lista);
int View_if_Cards (PtrCrt *lista, int cod, int ver);

void Insert_Trans (PtrTrans *lista);
void Remove_Trans (PtrTrans *lista);
void View_Trans (PtrTrans *lista);
void Delet_Trans (PtrTrans *lista);
int View_if_Trans (PtrTrans *lista, int cod, int ver);


void Error (char *msg);
PtrCrt Adress_Card (PtrCrt *lista, int cod);
PtrTrans Adress_Trans (PtrTrans *lista, int cod);

void View_Cards_Assoc (PtrCrt *lista);
void View_Card_Trans (PtrTrans *lista);
void Menu_Cards (void);
void Menu_Transaccions (void);

void Menu_Listall (void);
I need to calculate the Balance of every card in all new transaccions and
how put this verification
in one function?!
It´s better create one more strcut for movements (transaccions) cards?
I´m confused...
 
C

CBFalconer

Maria said:
Thks for your help... But to be more specific here´s one part of my code...

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <alloc.h>
#include <string.h>

Please don't top-post. Your answer belongs after (or intermixed
with) the material to which you reply, after snipping. See the
references in my sig. below.

The above code is off-topic here, since conio.h and alloc.h are
non-standard.

--
Some informative links:
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/> (taming google)
<http://members.fortunecity.com/nnqweb/> (newusers)
 
F

Flash Gordon

Jeff Mullen wrote, On 22/01/07 07:47:

P.S.

http://www.nls.net/mp/413/sea.ZIP

Self-expanding arrays. (This is the code your college professor
warned you about.) :) :) :)

Extracts from the code in the archive above...

from sea.h:

| #ifndef _SEA_H_
|
| #define _SEA_H_

Names starting with an underscore followed by an upper case letter are
reserved for use by the implementation. Don't do that.

from sea.c:
| else if ((ST_sea = malloc(sizeof(SEA))) == NULL)

Better to use:
else if ((ST_sea = malloc(sizeof *ST_sea))) == NULL)
Then you do not have to worry about getting the type correct. Very
useful if the type ever gets changed.

| {
| ST_sea = NULL;
| } /* if */

Isn't assigning NULL to ST_sea a bit pointless when you have just found
that it is equal to NULL? This smells like it has not been properly
thought out to me.

| if ((ST_sea->V_items = realloc(ST_sea->V_items, ST_sea->i_num_alloc
| * ST_sea->i_item_size)) == NULL)

If realloc fails here you have just lost your pointer to the old, still
valid memory. You should always assign the result of realloc to a
temporary variable for testing and then, only if realloc succeeded
overwrite your old pointer.

Also you use something called WITHIN the definition of which is not
provided. Possibly it is defined in friendly.h, but you don't provide
that in the ZIP you provide that in the ZIP you pointed people at.

I've not looked any further because it currently is not in a compilable
state.
 
R

Richard Heathfield

CBFalconer said:

The above code is off-topic here, since conio.h and alloc.h are
non-standard.

I don't entirely agree, or at least I think your explanation is incomplete.
It is entirely possible that conio.h and alloc.h are user-defined headers
whose source the OP unaccountably forgot to include, which might well be
written entirely in ISO C.
 
J

Jeff Mullen

Maria said:
Thks for your help... But to be more specific here´s one part of my code...

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <alloc.h>
#include <string.h>

typedef enum observations
{
Item1=1,Item2,Item3,Item4,Item5

}Observations;


typedef enum trans_type
{
CRG=1, DBT,TRF

}Trans_type;

typedef enum utente
{
Student=1, Functionary ,Professor

}Utente;

typedef struct data
{
int day, month, year;

}Data;


typedef struct card
{
int num_card;
char name[255];
Utente utente;
Data data;
float balance;
char morada[60];
struct card *next;

} Card;

typedef Card *PtrCrt;

typedef struct transaccions
{
int codigo;
char nome[60];
Data data;
Trans_tipo trans_tipo;
float montante,crg;
Observacoes observacoes;
char local_treinos[60];
char nome_treinador[60];
struct trans *next;
PtrCrt p_cartao;

} Trans;

typedef Trans *PtrTrans;
/*------------------------Functions------------------------*/

void Insert_Card (PtrCrt *lista);
void Change_Card (PtrCrt *lista);
void Remove_Card (PtrCrt *lista);
void View_Cards (PtrCrt *lista);
void Delet_Cards (PtrCrt *lista);
int View_if_Cards (PtrCrt *lista, int cod, int ver);

void Insert_Trans (PtrTrans *lista);
void Remove_Trans (PtrTrans *lista);
void View_Trans (PtrTrans *lista);
void Delet_Trans (PtrTrans *lista);
int View_if_Trans (PtrTrans *lista, int cod, int ver);


void Error (char *msg);
PtrCrt Adress_Card (PtrCrt *lista, int cod);
PtrTrans Adress_Trans (PtrTrans *lista, int cod);

void View_Cards_Assoc (PtrCrt *lista);
void View_Card_Trans (PtrTrans *lista);
void Menu_Cards (void);
void Menu_Transaccions (void);

void Menu_Listall (void);
I need to calculate the Balance of every card in all new transaccions and
how put this verification
in one function?!
It´s better create one more strcut for movements (transaccions) cards?
I´m confused...


Jeff Mullen said:
Please be more specific. What is the balance of a card? What do you
need to verify? How are you representing the aggregation of cards?
As an array? A singly-linked list? Many singly-linked lists?

P.S.

http://www.nls.net/mp/413/sea.ZIP

Self-expanding arrays. (This is the code your college professor
warned you about.) :) :) :)
Thank you for the additional information, but you still didn't answer
the first question I asked: what is the "balance" of a card?

ASSUMING that you are storing your cards in a singly-linked list,
and that you have a header pointer around somewhere, you can do
your computations using a FOR loop. Assuming that the header
pointer, called "hand," is in your code somewhere, and that you
have a function called computer_balance that takes a pointer to
card as its argument and computes the balance for that card:

Card *scrutiny;
int balance = 0;

for (scrutiny = hand; scrutiny != NULL; scrutiny = scrutiny->next)
{
balance += compute_balance(scrutiny);
} /* for */

should be close to what you're asking for, as I understand it
based on the fragmentary information that you have given.
 
J

Jeff Mullen

Flash said:
Jeff Mullen wrote, On 22/01/07 07:47:



Extracts from the code in the archive above...

from sea.h:

| #ifndef _SEA_H_
|
| #define _SEA_H_

Names starting with an underscore followed by an upper case letter are
reserved for use by the implementation. Don't do that.

from sea.c:
| else if ((ST_sea = malloc(sizeof(SEA))) == NULL)

Better to use:
else if ((ST_sea = malloc(sizeof *ST_sea))) == NULL)
Then you do not have to worry about getting the type correct. Very
useful if the type ever gets changed.

| {
| ST_sea = NULL;
| } /* if */

Isn't assigning NULL to ST_sea a bit pointless when you have just found
that it is equal to NULL? This smells like it has not been properly
thought out to me.

| if ((ST_sea->V_items = realloc(ST_sea->V_items,
ST_sea->i_num_alloc
| * ST_sea->i_item_size)) == NULL)

If realloc fails here you have just lost your pointer to the old, still
valid memory. You should always assign the result of realloc to a
temporary variable for testing and then, only if realloc succeeded
overwrite your old pointer.

Also you use something called WITHIN the definition of which is not
provided. Possibly it is defined in friendly.h, but you don't provide
that in the ZIP you provide that in the ZIP you pointed people at.

I've not looked any further because it currently is not in a compilable
state.

Thank you for looking my code over. I hope that all the people
whom you provide with unsolicited reviews will be as forthright,
and in as much a position to make changes regarding your constructive
criticism, as I am.

As a matter of internet etiquette, you should have sent me a private
email about this--not because the message might air some "dirty
laundry" of mine (I don't care about that if it improves the code),
but because, as I am rather busy, the message on a usenet list might
expire before I can get back to it. Thankfully, that was not the
case this time. You should also consider asking people whether
they would like their code reviewed when they do not explicitly
request this service. As I pointed out, I appreciate your efforts,
but you should be more sensitive to the possibility of other points
of view.

Jeff
 
B

Ben Pfaff

Jeff Mullen said:
As a matter of internet etiquette, you should have sent me a private
email about this--not because the message might air some "dirty
laundry" of mine (I don't care about that if it improves the code),
but because, as I am rather busy, the message on a usenet list might
expire before I can get back to it.

It's not normal practice to send email in response to a Usenet
article, unless the email is not in line with the topic of the
group. Generally it's people who view Usenet as "write-only" who
ask for email replies.
You should also consider asking people whether they would like
their code reviewed when they do not explicitly request this
service.

It's also not normal practice to ask about this in comp.lang.c.
Generally, we assume that if you're posting code here, or posting
about code here, you'd like us to look it over.
 
K

Keith Thompson

Jeff Mullen said:
Flash said:
Jeff Mullen wrote, On 22/01/07 07:47:

Extracts from the code in the archive above...
from sea.h:
[snip]

Thank you for looking my code over. I hope that all the people
whom you provide with unsolicited reviews will be as forthright,
and in as much a position to make changes regarding your constructive
criticism, as I am.

As a matter of internet etiquette, you should have sent me a private
email about this--not because the message might air some "dirty
laundry" of mine (I don't care about that if it improves the code),
but because, as I am rather busy, the message on a usenet list might
expire before I can get back to it. Thankfully, that was not the
case this time. You should also consider asking people whether
they would like their code reviewed when they do not explicitly
request this service. As I pointed out, I appreciate your efforts,
but you should be more sensitive to the possibility of other points
of view.

If you don't want your code publicly reviewed, I suggest not making it
publicly available. If you don't want it reviewed in this newsgroup,
don't post a link to it in this newgroup. The review was as much for
the benefit of other readers who saw the link as it was for you.
 
R

Richard Heathfield

Jeff Mullen said:

Thank you for looking my code over. I hope that all the people
whom you provide with unsolicited reviews will be as forthright,
and in as much a position to make changes regarding your constructive
criticism, as I am.

As a matter of internet etiquette, you should have sent me a private
email about this

Wrong. Post here, read here.
 
F

Flash Gordon

Keith Thompson wrote, On 02/02/07 02:13:
Extracts from the code in the archive above...
from sea.h:
[snip]
Thank you for looking my code over. I hope that all the people
whom you provide with unsolicited reviews will be as forthright,

If you don't want your code publicly reviewed, I suggest not making it
publicly available. If you don't want it reviewed in this newsgroup,
don't post a link to it in this newgroup. The review was as much for
the benefit of other readers who saw the link as it was for you.

For the record, I consider any posting of code here (or links to such
code) to be, amongst other things, a request for it to be reviewed so no
review can be considered unsolicited. This includes any code I post, of
course, and whilst I don't like having my errors pointed out I like even
less having the errors left uncorrected.

I agree with the rest of what Ben, Keith and Richard posted as well.
 
M

Mark McIntyre

As a matter of internet etiquette, you should have sent me a private
email about this--

This is usenet: write here, read here.
because, as I am rather busy, the message on a usenet list might
expire before I can get back to it.

So you mean that because /you're/ busy, the dozens of people reading
your posting have to send you individual messages. Don't you think
thats a bit absurd?
case this time. You should also consider asking people whether
they would like their code reviewed when they do not explicitly
request this service.

One of the things that happens to code here in CLC is that it gets
reviewed. If you don't want your code reviewed, the fix is - don't
post it or link to it.

Of course, if you don't post it, then its awfully hard for people to
help you with any problems with it. You're likely to get lots of
replys along hte lines of "my crystal ball is broken, but I think its
line 42".


--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 

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
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top