Can anyone please, please show me what's going wrong here. (Full code
listed at end of post)
I'm trying to access a structure from within another structure but am
receiving errors. Unfortunately, I need to build this on a windows
machine, but I think I have made the code fully portable for Linux. It
compiles with the mingw compiler anyway.
Structures are global and are as follows:
struct StockItem
{
int code;
char desc[20];
float price;
}stock[NUM_OF_ITEMS];
struct BillLine
{
StockItem stock;
float weight;
float cost;
}bill_line[50];
The 'stock' array is already populated.
All I'm trying to do is fill the 'BillLine bill_line' array in the first
function, and then print out the array values in the second. My error
seems to be coming from accessing one structure from another. My problem
code is as follows, I've commented the line numbers flagged up by the
compiler:
BillLine bill_item(void)
{
BillLine bill;
int item;
cls();
cout << "Enter item number purchased: ";
cin >> item;
item -= 1;
/*line 213*/ bill.stock = stock[item];
cout << "Weight: ";
cin >> bill.weight;
bill.cost = stock[item].price * bill.weight;
return (bill);
}
void print_bill ( BillLine bill_line[50], int num_bill_items ) {
for (int i=0; i
Structures are global and are as follows: <snip> struct BillLine { StockItem stock; float weight; float cost; }bill_line[50]; <snip> My full program code is as follows <snip> struct BillLine { StockItem stock[NUM_OF_ITEMS]; float weight; float cost; }bill_line[50];
notice the difference between the two declarations of BillLine. the problem you're having is becuase in the full code, you're trying to assign a single StockItem to an array of them without referencing a single array location. the second error has the same cause. you're trying to access the desc value but treating it as a single StockItem, not an array. -- trey
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On Mon, 12 Jan 2004 18:56:41 -0000
"ged.suse"
Structures are global and are as follows:
struct StockItem { int code; char desc[20]; float price; }stock[NUM_OF_ITEMS];
struct BillLine { StockItem stock; float weight; float cost; }bill_line[50]; <snip> /*line 213*/ bill.stock = stock[item]; cout << "Weight: ";
(+(i * 3508)))->BillLine::stock', which is of non-aggregate type ` StockItem[125]'
On Mon, 12 Jan 2004 14:06:09 -0500 (EST)
Trey Gruel
problem you're having is becuase in the full code, you're trying to assign a single StockItem to an array of them without referencing a single array location. the second error has the same cause. you're trying to access the desc value but treating it as a single StockItem, not an array.
I think that Trey's comments are legitimate, but not the solution.
bill.stock is a single instance of a stock item.
I think you might be able to cast this:
bill.stock = *
{ StockItem *stock; float weight; float cost; }bill_line[50];
Then:
bill.stock = &stock[item];
Now, bill.stock will point to the appropriate instance,
Also, I did not check you code, but item must be in the range from 0 >=
item < NUM_ITEMS.
If item == NUM_ITEMS, then you are exceeding the bounds of the array.
But, it appears that your code is really C being compiled with a C++
compiler.
- --
Jerry Feldman
I think that Trey's comments are legitimate, but not the solution. bill.stock is a single instance of a stock item. I think you might be able to cast this: bill.stock = *
(&stock[item]); What you apparently want to do is to copy a single instance. Or you can use the memcpy function.
looking at the full code, i don't see why bill.stock is an array. the only places it's used, it's always treated as a single instance. setting it to a pointer as you describe is probably the most memory efficient, but he would have to check in his delete_data function if the stock he's deleting is referenced by bill. this seems to be a program for a beginning level class and i'm not sure how far they've gotten. as such, i would suggest copying the data instead of pointing to it.
But, it appears that your code is really C being compiled with a C++ compiler.
that's pretty much the way most colleges are teaching it nowadays. stuff that could be taught in c (or even more simple langauges) all being taught in c++ or java. as it is, he is using c++ iostream for is user io. note: i didn't give a specific solution out of old habit. when i was a ta, i found that most students remembered solutions better if they found it themselves. because of that i tended to only help them find the problem and set them loose to find the solution. if they were still having trouble, i'd give them a nudge or two in the right direction, but would never simply give the solution to the problem at hand. -- trey
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On Mon, 12 Jan 2004 15:17:40 -0500 (EST)
Trey Gruel
that's pretty much the way most colleges are teaching it nowadays. stuff that could be taught in c (or even more simple langauges) all being taught in c++ or java. as it is, he is using c++ iostream for is user io. He wasn't in his original version.
Colleges do tend to teach some of the basics as C. But, at Northeastern,
C is a prerequisite for C++.
- --
Jerry Feldman
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On Mon, 12 Jan 2004 15:17:40 -0500 (EST)
Trey Gruel
looking at the full code, i don't see why bill.stock is an array. It's not. struct BillLine { StockItem stock; It is a single element. bill.stock = stock[item]; should be a simple structure copy. Actually, structure copies are bad things in C++. All structures are classes, complete with constructors and destructors. I wonder if the value of item is >= NUM_ITEMS, and what he is really seeing is a bounds error.
Or, as I mentioned earlier, his intent may be to simply use a pointer.
In his code he is passing structures as arguments to functions.
Also I noticed that my static cast has a typo:
bill.stock = *
Thanks Trey / Charly / Jerry.
I have got the code running now.
Your right about the C thing. I'm currently wrestling with the fact that
my lecturer probably grew up with C and is pushing all his C habbits
into our C++ class. I am constantly trying to alter the way he teaches
to follow the C++ standard, but as I have only been learning programming
for about 3-4 months, its difficult to manage.
Anyway, I have a very strange bug in my program which I can't seem to
iron out. When I add more stock via the number1 in the menu, everything
is fine and I can build the stock up, however, if I then try and save
this to file (via menu num 4) is doesn't always save my new stock, it
stays as it was.
e.g. I currently have 11 items in stock. If I go to add another item, it
doesn't seem to save them.....I thought maybe it wasn't updating
'num_stock_items' but am unsure.
I've tried messing about with gdb and I seem to be getting strange
values for the integers I tried
int ans, ctr;
int num_stock_items = 0;
int num_bill_items = 0;
I've had no real experience with gdb, only from reading through the man
file, so I may be doing something wrong. Can anyone see why it may not
be saving correctly?
Here is my updated program (excuse the bad layout in the 'print bill'
function, I have not mastered text formatting yet) I'll continue when
I've got this bug sorted, as I have already done twice the work for this
assignment required. I'm just playing to further my knowledge now. (plus
the shot of a distinction...heh)
#include <iostream>
#include <fstream>
#include I think that Trey's comments are legitimate, but not the solution.
bill.stock is a single instance of a stock item.
I think you might be able to cast this:
bill.stock = * What you apparently want to do is to copy a single instance.
Or you can use the memcpy function. looking at the full code, i don't see why bill.stock is an array. the
only places it's used, it's always treated as a single instance.
setting
it to a pointer as you describe is probably the most memory efficient,
but
he would have to check in his delete_data function if the stock he's
deleting is referenced by bill. this seems to be a program for a
beginning level class and i'm not sure how far they've gotten. as such,
i
would suggest copying the data instead of pointing to it. But, it appears that your code is really C being compiled with a C++
compiler. that's pretty much the way most colleges are teaching it nowadays.
stuff
that could be taught in c (or even more simple langauges) all being
taught
in c++ or java. as it is, he is using c++ iostream for is user io.
note: i didn't give a specific solution out of old habit. when i was a
ta, i found that most students remembered solutions better if they found
it themselves. because of that i tended to only help them find the
problem and set them loose to find the solution. if they were still
having trouble, i'd give them a nudge or two in the right direction, but
would never simply give the solution to the problem at hand.
--
trey
Thanks Trey / Charly / Jerry.
no prob
Anyway, I have a very strange bug in my program which I can't seem to iron out. When I add more stock via the number1 in the menu, everything is fine and I can build the stock up, however, if I then try and save this to file (via menu num 4) is doesn't always save my new stock, it stays as it was. e.g. I currently have 11 items in stock. If I go to add another item, it doesn't seem to save them.....I thought maybe it wasn't updating 'num_stock_items' but am unsure.
i had to make a couple small changes to get it to compile on the redhat 7.3 box i'm sitting at right now (get rid of conio.h/getch and no call to system(cls)), but i was unable to reproduce the problem. i tried a couple runs of: 1) enter 2-3 items 2) display them 3) save to disk 4) quitting 5) restart the prog 6) load from disk 7) display them 8) goto 1 every time, all records were saved and loaded. one comment on your file io: it's a good idea to close the file after you're done with it. -- trey
It seems to stick on 11. It won't always let me input item number 12.
The items i'm trying to enter are as follows.
Code Description price
------ -------------- ------
1000 Salt 0.56
1001 Rock Salt 0.89
1010 Peppercorns 1.59
1020 Plain Flour 1.11
1021 Wholemeal Flour 1.35
1022 Self Raising Flour 1.40
1030 Cane Sugar 1.20
1031 Icing Sugar 1.30
1032 Brown Sugar 1.35
1040 White Rice 1.10
1041 Brown Rice 1.15
1050 Yeast 4.50
1051 Dried Yeast 3.65
If I follow it in that order, I am not able to get past item number 11.
If I enter random data however, (just tapping any old input) I don't have any
problems.
I've been chasing this bug for ages now...heh.
If anyone gets a spare 20 mins, I would be greatfull if you could enter this
data and see if you find the same bug. I have only tried this on a Windows
machine, but I think if
Thanks Trey / Charly / Jerry.
no prob
Anyway, I have a very strange bug in my program which I can't seem to iron out. When I add more stock via the number1 in the menu, everything is fine and I can build the stock up, however, if I then try and save this to file (via menu num 4) is doesn't always save my new stock, it stays as it was. e.g. I currently have 11 items in stock. If I go to add another item, it doesn't seem to save them.....I thought maybe it wasn't updating 'num_stock_items' but am unsure.
i had to make a couple small changes to get it to compile on the redhat 7.3 box i'm sitting at right now (get rid of conio.h/getch and no call to system(cls)), but i was unable to reproduce the problem.
i tried a couple runs of:
1) enter 2-3 items 2) display them 3) save to disk 4) quitting 5) restart the prog 6) load from disk 7) display them 8) goto 1
every time, all records were saved and loaded.
one comment on your file io: it's a good idea to close the file after you're done with it.
-- trey
-- -=SuSE Linux 9.0=-
It seems to stick on 11. It won't always let me input item number 12. The items i'm trying to enter are as follows.
Code Description price ------ -------------- ------ 1000 Salt 0.56 1001 Rock Salt 0.89 1010 Peppercorns 1.59 1020 Plain Flour 1.11 1021 Wholemeal Flour 1.35 1022 Self Raising Flour 1.40 1030 Cane Sugar 1.20 1031 Icing Sugar 1.30 1032 Brown Sugar 1.35 1040 White Rice 1.10 1041 Brown Rice 1.15 1050 Yeast 4.50 1051 Dried Yeast 3.65
If I follow it in that order, I am not able to get past item number 11. If I enter random data however, (just tapping any old input) I don't have any problems. I've been chasing this bug for ages now...heh.
hate to say it, but it works for me with gcc 2.96 and 3.2 (both redhat machines)
If anyone gets a spare 20 mins, I would be greatfull if you could enter this data and see if you find the same bug. I have only tried this on a Windows machine, but I think if
is swapped for (for getch()) and if is added for the system call (obviously the function needs swapping from 'cls' to 'clear') then everything should work OK.
coulda done that, but for seeing what was going on, i felt that streaming the output was simpler.
I have attached the file just in case there is a good samaritan :) (i'm also stuck on formating the print_bill() output)
you should look at the justification of the output and where you're doing your 'setw's.. as it is right now, the weight is right justified with the field width starting at the end of the product description. -- trey
It seems to stick on 11. It won't always let me input item number 12.
The items i'm trying to enter are as follows.
Code Description price
------ -------------- ------
1000 Salt 0.56
1001 Rock Salt 0.89
1010 Peppercorns 1.59
1020 Plain Flour 1.11
1021 Wholemeal Flour 1.35
1022 Self Raising Flour 1.40
1030 Cane Sugar 1.20
1031 Icing Sugar 1.30
1032 Brown Sugar 1.35
1040 White Rice 1.10
1041 Brown Rice 1.15
1050 Yeast 4.50
1051 Dried Yeast 3.65
If I follow it in that order, I am not able to get past item number 11.
If I enter random data however, (just tapping any old input) I don't have any
problems.
I've been chasing this bug for ages now...heh.
If anyone gets a spare 20 mins, I would be greatfull if you could enter this
data and see if you find the same bug. I have only tried this on a Windows
machine, but I think if
Thanks Trey / Charly / Jerry.
no prob
Anyway, I have a very strange bug in my program which I can't seem to iron out. When I add more stock via the number1 in the menu, everything is fine and I can build the stock up, however, if I then try and save this to file (via menu num 4) is doesn't always save my new stock, it stays as it was. e.g. I currently have 11 items in stock. If I go to add another item, it doesn't seem to save them.....I thought maybe it wasn't updating 'num_stock_items' but am unsure.
i had to make a couple small changes to get it to compile on the redhat 7.3 box i'm sitting at right now (get rid of conio.h/getch and no call to system(cls)), but i was unable to reproduce the problem.
i tried a couple runs of:
1) enter 2-3 items 2) display them 3) save to disk 4) quitting 5) restart the prog 6) load from disk 7) display them 8) goto 1
every time, all records were saved and loaded.
one comment on your file io: it's a good idea to close the file after you're done with it.
-- trey
-- -=SuSE Linux 9.0=-
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On Tue, 13 Jan 2004 20:33:38 +0000
Ged
It seems to stick on 11. It won't always let me input item number 12. The items i'm trying to enter are as follows.
I definitely won't have a chance to do anything until possibly tomorrow
afternoon. But, just to mirror Trey's sentiment,
getch()/conio.h is a Windows function call, not Unix/Linux.
cls is a Windows command line command. The Unix/Linux equivalent is
clear. This is supposed to be a SuSE Linux programming list.
On Linux you can easily write the equivalent of the getch() function.
This function is written for C, not C++. You will need to change the
includes to #include <cstdio>, #include <cerrno>.
Note that the file descriptor for input is normally 0, but there is a
C++ function in the iostream library that is equivalent to the filenum()
function in C, but I don't recall it at the moment.
#include
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On Tue, 13 Jan 2004 20:33:38 +0000
Ged
It seems to stick on 11. It won't always let me input item number 12. The items i'm trying to enter are as follows.
<snip>
I tested your code with the changes I listed below so it works on SuSE
Linux. I was able to enter all the stock items in your list and list all
13 out.
Here are the changes I made to your program (only to replace the Windows
specific issues with Linux compatible code).
1.
#include <cerrno>
/***************** include unistd.h and termios.h for getch() ***/
#include
Hiya, I noticed the C++ thread, so I thought I would jump in and ask a question. I have a application that uses win32 MAPI to extract contact information. When I compile I need to link it to MS Mapi 32 API. However when I configure, I linked a free mapi 32 lib because I didn't have microsoft compiler. If I recall correctly the syntax of casting return value is a little different. *************************************** rc = (*mapiaddr)(0L, 0L, "Add to Vanquish Allow List. (Press Cancel to abort, use the Shift key to select multiple addresses.)", 1L, "Add to Allow List", 0L, NULL, MAPI_LOGON_UI | MAPI_NEW_SESSION, 0L, &lpnNewRecips, &lppNewRecips); ************************************** I need to make a call to make a window show up so that one can select addresses. later in a loop temp = env->NewStringUTF(lppNewRecips[i].lpszAddress); sets the address so that java can retrive it. Any help/direction would be wicked appreciated Cheers! Suzanne
participants (5)
-
Ged
-
ged.suse
-
Jerry Feldman
-
Suzanne McSweeney-DesRochers
-
Trey Gruel