struct problem

J

jesse

alo. i'm learning c at the moment, and i'm attempting to make little
experiments with struct types. here's my code:

#include <stdio.h>

struct bin {
char name[30];
int quantity;
float cost;
};

int main()
{
struct bin cablebin;

cablebin.name = "Printer Cables";
cablebin.quantity = 25;
cablebin.cost = 11.95;

printf("A bin of %d %s at $%f a piece will cost $%f.\n",
cablebin.quantity, cablebin.name, cablebin.cost,
cablebin.cost*cablebin.quantity);

return 0;
}

for some reason, i get the following error:
Error E2277 struct.c 13: Lvalue required in function main

line 13 is: cablebin.name = "Printer Cables";

i'm a bit confused; my code should be correct, so i don't know what's going.

jesse.
 
M

Martin Dickopp

jesse said:
alo. i'm learning c at the moment, and i'm attempting to make little
experiments with struct types. here's my code:

#include <stdio.h>

struct bin {
char name[30];
int quantity;
float cost;
};

int main()
{
struct bin cablebin;

cablebin.name = "Printer Cables";
cablebin.quantity = 25;
cablebin.cost = 11.95;

printf("A bin of %d %s at $%f a piece will cost $%f.\n",
cablebin.quantity, cablebin.name, cablebin.cost,
cablebin.cost*cablebin.quantity);

return 0;
}

for some reason, i get the following error:
Error E2277 struct.c 13: Lvalue required in function main

line 13 is: cablebin.name = "Printer Cables";

i'm a bit confused; my code should be correct, so i don't know what's going.

You cannot assign to an array. Use

strcpy (cablebin.name, "Printer Cables");

instead and don't forget to include <string.h>.
 
P

Peter Pichler

jesse said:
alo. i'm learning c at the moment, and i'm attempting to make little
experiments with struct types. here's my code:

#include <stdio.h>

struct bin {
char name[30];
int quantity;
float cost;
};

int main()
{
struct bin cablebin;

cablebin.name = "Printer Cables";
cablebin.quantity = 25;
cablebin.cost = 11.95;

printf("A bin of %d %s at $%f a piece will cost $%f.\n",
cablebin.quantity, cablebin.name, cablebin.cost,
cablebin.cost*cablebin.quantity);

return 0;
}

for some reason, i get the following error:
Error E2277 struct.c 13: Lvalue required in function main

line 13 is: cablebin.name = "Printer Cables";

i'm a bit confused; my code should be correct, so i don't know what's
going.

C is not BASIC. Your cablebin.name is an array of char. If you want to fill
it, you must do it explicitly by strcpy or some such, a plain assignment
does not work.

There is a concession, but only for initializations, not assignments. You
could rewrite your program like this:

/* ...copy your declarations and stuff, they are OK. */

int main()
{
struct bin cablebin =
{
"Printer Cables",
25,
11.95
};

printf("A bin of %d %s at $%f a piece will cost $%f.\n",
cablebin.quantity, cablebin.name, cablebin.cost,
cablebin.cost*cablebin.quantity);

return 0;
}
 
L

Lewis Bowers

jesse said:
alo. i'm learning c at the moment, and i'm attempting to make little
experiments with struct types. here's my code:

Read faq: http://www.eskimo.com/~scs/C-faq/q8.3.html
#include <stdio.h>



struct bin {
char name[30];
int quantity;
float cost;
};

int main()
{
struct bin cablebin;

cablebin.name = "Printer Cables";

Replace the above with
strcpy(cablebin.name,"Printer Cables");

As an alternative you can initialize the struct during declaration like this:

struct bin cablebin = {"Printer Cables", 25, 11.95f};
thus eliminating the need to use assignments.
 
E

E. Robert Tisdale

jesse said:
I'm learning C at the moment and I'm attempting to [conduct] little
experiments with struct types. Here's my code:

#include <stdio.h>
#include said:
typedef struct bin {
char name[30];
int quantity;
float cost;
} bin;

inline
bin bin_create(const char* n, int q, float c) {
bin b;
b.name[29] = '\0';
strncpy(b.name, n, 29);
b.quantity = q;
b.cost = c;
return b;
}

inline const
char* bin_name(const bin* pBin) {
return pBin->name;
}

inline
int bin_quantity(const bin* pBin) {
return pBin->quantity;
}

inline
float bin_cost(const bin* pBin) {
return pBin->cost;
}

inline
void bin_destroy(const bin* pBin) {
}

inline
int bin_fprintf(FILE* fp, const bin* pBin) {
return fprintf(fp, "%s %d %f",
pBin->name, pBin->quantity, pBin->cost);
}
int main(int argc, char* argv[]) {
const bin cablebin = bin_create("Printer Cables", 25, 11.95);
printf("A bin of %d %s at $%f a piece will cost $%f.\n",
bin_quantity(&cablebin), bin_name(&cablebin), bin_cost(&cable_bin),
bin_cost(&cablebin)*bin_quantity(&cablebin);

bin_destroy(&cablebin);
 
C

CBFalconer

jesse said:
alo. i'm learning c at the moment, and i'm attempting to make
little experiments with struct types. here's my code:

#include <stdio.h>

struct bin {
char name[30];
int quantity;
float cost;
};

int main()
{
struct bin cablebin;

cablebin.name = "Printer Cables";
cablebin.quantity = 25;
cablebin.cost = 11.95;

printf("A bin of %d %s at $%f a piece will cost $%f.\n",
cablebin.quantity, cablebin.name, cablebin.cost,
cablebin.cost*cablebin.quantity);

return 0;
}

for some reason, i get the following error:
Error E2277 struct.c 13: Lvalue required in function main

line 13 is: cablebin.name = "Printer Cables";

i'm a bit confused; my code should be correct, so i don't know
what's going.

You want:

strcpy(&(cablebin.name), "Printer Cables");

because arrays cannot be assigned, although structures can. You
can, however, assign pointers to char. So that if the definition
of the name field had been:

char *name;

your original assignment would have worked. Confused yet?
 
C

Clark Cox

CBFalconer said:
jesse said:
alo. i'm learning c at the moment, and i'm attempting to make
little experiments with struct types. here's my code:

#include <stdio.h>

struct bin {
char name[30];
int quantity;
float cost;
};

int main()
{
struct bin cablebin;

cablebin.name = "Printer Cables";
cablebin.quantity = 25;
cablebin.cost = 11.95;

printf("A bin of %d %s at $%f a piece will cost $%f.\n",
cablebin.quantity, cablebin.name, cablebin.cost,
cablebin.cost*cablebin.quantity);

return 0;
}

for some reason, i get the following error:
Error E2277 struct.c 13: Lvalue required in function main

line 13 is: cablebin.name = "Printer Cables";

i'm a bit confused; my code should be correct, so i don't know
what's going.

You want:

strcpy(&(cablebin.name), "Printer Cables");

What's wrong with:

strcpy(cablebin.name, "Printer Cables");
 
J

Jack Klein

jesse said:
alo. i'm learning c at the moment, and i'm attempting to make
little experiments with struct types. here's my code:

#include <stdio.h>

struct bin {
char name[30];
int quantity;
float cost;
};

int main()
{
struct bin cablebin;

cablebin.name = "Printer Cables";
cablebin.quantity = 25;
cablebin.cost = 11.95;

printf("A bin of %d %s at $%f a piece will cost $%f.\n",
cablebin.quantity, cablebin.name, cablebin.cost,
cablebin.cost*cablebin.quantity);

return 0;
}

for some reason, i get the following error:
Error E2277 struct.c 13: Lvalue required in function main

line 13 is: cablebin.name = "Printer Cables";

i'm a bit confused; my code should be correct, so i don't know
what's going.

You want:

strcpy(&(cablebin.name), "Printer Cables");

No you don't, surely. Incorrect pointer type. The type of
&(cablebin.name) is pointer to array of 30 characters, not pointer to
char.

Either:

&(cablebin.name[0])

....or even simpler,

cablebin.name
 
L

Lewis Bowers

E. Robert Tisdale said:
jesse said:
I'm learning C at the moment and I'm attempting to [conduct] little
experiments with struct types. Here's my code:

#include <stdio.h>
#include said:
typedef struct bin {
char name[30];
int quantity;
float cost;
} bin;

printf("A bin of %d %s at $%f a piece will cost $%f.\n",
bin_quantity(&cablebin), bin_name(&cablebin), bin_cost(&cable_bin),
bin_cost(&cablebin)*bin_quantity(&cablebin);

Missing a ')' and since the float represents dollars and cents use %.2f

printf("A bin of %d %s at $%.2f a piece will cost $%.2f\n",
bin_quantity(&cablebin, bin_name(&cablebin, bin_cost(&cable_bin);
bin_cost(&cablebin)*bin_quantity(&cablebin));
 
P

Peter Shaggy Haywood

Groovy hepcat jesse was jivin' on Wed, 18 Feb 2004 19:29:21 GMT in
comp.lang.c.
struct problem's a cool scene! Dig it!
alo. i'm learning c at the moment, and i'm attempting to make little
experiments with struct types. here's my code:

#include <stdio.h>

struct bin {
char name[30];
int quantity;
float cost;
};

On a different issue, float is a bad choice for a monitary amount.
It may not be able to accurately enough represent all the values
you're likely to need. It's better to use some sort of integral type
representing the number of cents rather than the number of dollars.
The conversion from cents to dollars and cents is trivial. Something
like this should do:

int dollars;
int cents;
....
dollars = cost / 100;
cents = cost % 100;

printf("The cost is $%d.%d\n", dollars, cents);

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 
J

Joe Wright

Peter said:
Groovy hepcat jesse was jivin' on Wed, 18 Feb 2004 19:29:21 GMT in
comp.lang.c.
struct problem's a cool scene! Dig it!
alo. i'm learning c at the moment, and i'm attempting to make little
experiments with struct types. here's my code:

#include <stdio.h>

struct bin {
char name[30];
int quantity;
float cost;
};

On a different issue, float is a bad choice for a monitary amount.
It may not be able to accurately enough represent all the values
you're likely to need. It's better to use some sort of integral type
representing the number of cents rather than the number of dollars.
The conversion from cents to dollars and cents is trivial. Something
like this should do:

int dollars;
int cents;
...
dollars = cost / 100;
cents = cost % 100;

printf("The cost is $%d.%d\n", dollars, cents);
Not quite. The % operator cannot be applied to float or double. Maybe..

cents = cost * 100;
dollars = cents / 100
cents %= 100;
 
P

Peter Nilsson

Groovy hepcat jesse was jivin' on Wed, 18 Feb 2004 19:29:21 GMT in
comp.lang.c.
struct problem's a cool scene! Dig it!
alo. i'm learning c at the moment, and i'm attempting to make little
experiments with struct types. here's my code:

#include <stdio.h>

struct bin {
char name[30];
int quantity;
float cost;
};

On a different issue, float is a bad choice for a monitary amount.
It may not be able to accurately enough represent all the values
you're likely to need. It's better to use some sort of integral type
representing the number of cents rather than the number of dollars.
The conversion from cents to dollars and cents is trivial. Something
like this should do:

int dollars;
int cents;
...
dollars = cost / 100;
cents = cost % 100;

printf("The cost is $%d.%d\n", dollars, cents);

I'd shoot for %d.%02d, but as well as that, bare in mind negative
amounts are not unheard of, so it's important to consider them and
also the effects of division on negative operands. For instance, under
C90, a cost of -195 cents could print out as $-2.5 with the above
code.
 
P

Peter Shaggy Haywood

Groovy hepcat Joe Wright was jivin' on Sun, 22 Feb 2004 14:20:20 GMT
in comp.lang.c.
Re: struct problem's a cool scene! Dig it!
Peter said:
Groovy hepcat jesse was jivin' on Wed, 18 Feb 2004 19:29:21 GMT in
comp.lang.c.
struct problem's a cool scene! Dig it!
alo. i'm learning c at the moment, and i'm attempting to make little
experiments with struct types. here's my code:

#include <stdio.h>

struct bin {
char name[30];
int quantity;
float cost;
};

On a different issue, float is a bad choice for a monitary amount.
It may not be able to accurately enough represent all the values
you're likely to need. It's better to use some sort of integral type
representing the number of cents rather than the number of dollars.
The conversion from cents to dollars and cents is trivial. Something
like this should do:

int dollars;
int cents;
...
dollars = cost / 100;
cents = cost % 100;

printf("The cost is $%d.%d\n", dollars, cents);
Not quite. The % operator cannot be applied to float or double. Maybe..

What float or double? I said not to use float or double, but an
integral type. I guess I should have been clearer. I meant that cost
should be an int.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 
E

Eric Sosman

Peter said:
[...]
int dollars;
int cents;
...
dollars = cost / 100;
cents = cost % 100;

printf("The cost is $%d.%d\n", dollars, cents);

If `cost' is 109, this prints

The cost is $1.9

.... which might be a bit startling. I'd suggest changing
the second "%d" to "%02d".
 

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

No members online now.

Forum statistics

Threads
474,310
Messages
2,571,602
Members
48,419
Latest member
EstelaCout

Latest Threads

Top