Somebody Here Has To Know C++, Right?
7Bistromath
Join Date: 2003-12-04 Member: 23928Members, Constellation

<div class="IPBDescription">I am stuck. :(</div> My instructor's latest assignment says to build a program which simulates the rolling of a pair of dice, one red and one green, 1000 times each and then shows the number of times each result showed up. He wants us to use separate functions for stuff, which seems silly to me in this case, because the program is far too simple. It only does one thing, so it only needs one function.
Of course, being a horrible show-off, this means I have to make a better, more complicated program, rather than trying to use this as an excuse to skimp on the work. Being a gamer, doing something with dice that isn't a fullblown virtual dicebag just isn't good enough for me, so that's what I'm doing.
The problem: for part of what I've got so far, I need to use a multidimensional dynamic array. Multidimensionals are weird in the first place, and dynamics are five chapters ahead of where we're supposed to be, so I guess it serves me right to have problems from trying to fly too close to the sun.
My program <i>almost</i> works. The problem is, I don't know what's making my compiler sad with the last three errors I'm trying to kill. Can anybody help out?
I'm attaching the source I've got so far. You'll be able to tell straight off that it isn't complete, but that's fine. I'm just trying to get what I have working before I waste time on the rest.
Note: I didn't read the chapter for dynamic arrays in its entirety yet, so I only have a vague idea of what pointers are and how they work. If there's anything about them I need to know that could fix this, please put it in dummy's terms.
Further note: if any of these things matter, I'm compiling this with Visual C++ on an Athlon system running WinXP Pro.
Yet another note: here's the actual error messages I'm getting.
The first two are on the lines where I reference the array results inside those for loops. It says "subscript requires array or pointer type."
The last is on the second of those two, and it reads "'=' : left operand must be l-value."
Of course, being a horrible show-off, this means I have to make a better, more complicated program, rather than trying to use this as an excuse to skimp on the work. Being a gamer, doing something with dice that isn't a fullblown virtual dicebag just isn't good enough for me, so that's what I'm doing.
The problem: for part of what I've got so far, I need to use a multidimensional dynamic array. Multidimensionals are weird in the first place, and dynamics are five chapters ahead of where we're supposed to be, so I guess it serves me right to have problems from trying to fly too close to the sun.
My program <i>almost</i> works. The problem is, I don't know what's making my compiler sad with the last three errors I'm trying to kill. Can anybody help out?
I'm attaching the source I've got so far. You'll be able to tell straight off that it isn't complete, but that's fine. I'm just trying to get what I have working before I waste time on the rest.
Note: I didn't read the chapter for dynamic arrays in its entirety yet, so I only have a vague idea of what pointers are and how they work. If there's anything about them I need to know that could fix this, please put it in dummy's terms.
Further note: if any of these things matter, I'm compiling this with Visual C++ on an Athlon system running WinXP Pro.
Yet another note: here's the actual error messages I'm getting.
The first two are on the lines where I reference the array results inside those for loops. It says "subscript requires array or pointer type."
The last is on the second of those two, and it reads "'=' : left operand must be l-value."
Comments
<!--emo&???--><img src='http://www.unknownworlds.com/forums/html//emoticons/confused.gif' border='0' style='vertical-align:middle' alt='confused.gif' /><!--endemo-->
sorry I can only help you with Java or HTML
<!--emo&::nerdy::--><img src='http://www.unknownworlds.com/forums/html//emoticons/nerd.gif' border='0' style='vertical-align:middle' alt='nerd.gif' /><!--endemo-->
int total(0);
int choice(0);
I'm not sure about the reason for having those "(0)"s there but it seems to me that if they were removed the program should work. If they do something crucial, try changing the nested loop to:
total(0)+=results[i][j];
cout<<total(0)<<" ";
total(0)=0;
Hope this helps...
--Scythe--
[EDIT] So why did you post bonehead? [/EDIT]
...
Nope. Still got the same errors.
I can kinda fix it but you've really set this up akwardly.
First off you're making the 'results' array a [0][0] multidimensional array which gives you errors when you try to do the 'roll' function because you've basically made the array the size of a normal int =P
The fun part of this is that this is happenening because you're defining it when roll, faces, etc still = 0. Later on when they actually have values assigned to them by the user you kinda just ignore them completely (<b>edit: ok, not completely... you use them later to try access the [0][0] array as if you'd used them to size it. Just to clear that up =3 </b>), not that it matters because you've already defined the size of results before then anyways o.O
Secondly... arrays are fussy. If you don't have absolute values for it's size in either [] you have to pass it as a one-dimensional array and then refer to it with stuff like...
normal: array[5][6]
after casting as a single: array [5 * 6]
Oh, and you haven't updated the 'standard' function's definition so that it has 4 values instead of 3 ^^;
to be honest, the case system you won't work because it doesn't like you initiliasing variables inside of them (fussy C) but if you convert it into an if/else chain you can still do it.
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->void dice(int& menu)
{
int number=0, faces=0, repeat=0;
unsigned int seed(0);
if( menu = 1)
{
cout<<"This rolls xdy, where x is the number of dice, and y is the\n"
<<"number of faces on each die, and gives the sum of all results.\n\n";
setVars(number, faces, repeat);
IntPtr *results=new IntPtr[number];
for (int i=0; i<number; i++)
results[i]=new int[repeat];
standard(*results, number, faces, repeat);
}
else if ( menu = 2)
{
cout<<"This rolls xdy, where x is the number of dice, and y is the number\n"
<<"of faces on each die, and give the result on each individual die.\n\n";
setVars(number, faces, repeat);
}
else if ( menu = 3)
{
cout<<"If you are worried that your results may be skewed, you may test them\n"
<<"here. This presupposes two six-sided dice, one red, one green, and rolls\n"
<<"1000 times, displaying the number of times each face was rolled on each\n"
<<"die, and the number of times each combined result was rolled, allowing\n"
<<"to ensure that the results follow a proper curve. You may also reset the\n"
<<"pseudorandom seed from this option. By default, the seed is based on the\n"
<<"time of day. You may use any positive integer as your seed, randomly\n"
<<"selected if you wish to use the dicebag based on this seed, or chosen\n"
<<"intentionally if you want to run tests with repeatable results. To reset\n"
<<"the seed to the default, run this diagnostic again, and enter 0 for your\n"
<<"new seed.\n\n";
}
menu=0;
}<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
note that you'll have to put the initialisation of 'results' after setvars for your proggy to work properly, otherwise it'll just crash from illegal memory accesses when you try to run roll =P
gah! can't get the code tags to keep my tabs =s
anyways, that dice function should help, just remember to alter the other stuff I mentioned and you're rolling <!--emo&:p--><img src='http://www.unknownworlds.com/forums/html//emoticons/tounge.gif' border='0' style='vertical-align:middle' alt='tounge.gif' /><!--endemo-->
<b>edit:</b> oops! forgot to mention... the menu choice thingy in standard (yes/no/repeat) will cause problems too. I'll let you figure out the general jist of it, but you've definitely cut your work out for you ^^;
I can kinda fix it but you've really set this up akwardly.
First off you're making the 'results' array a [0][0] multidimensional array which gives you errors when you try to do the 'roll' function because you've basically made the array the size of a normal int =P
The fun part of this is that this is happenening because you're defining it when roll, faces, etc still = 0. Later on when they actually have values assigned to them by the user you kinda just ignore them completely (<b>edit: ok, not completely... you use them later to try access the [0][0] array as if you'd used them to size it. Just to clear that up =3 </b>), not that it matters because you've already defined the size of results before then anyways o.O
Secondly... arrays are fussy. If you don't have absolute values for it's size in either [] you have to pass it as a one-dimensional array and then refer to it with stuff like...
normal: array[5][6]
after casting as a single: array [5 * 6]<!--QuoteEnd--></td></tr></table><div class='postcolor'><!--QuoteEEnd-->I don't understand very well what you're telling me here. :/ It starts off as a 0x0, but it should have new values for number and repeat before it's ever referenced.<!--QuoteBegin--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> </td></tr><tr><td id='QUOTE'><!--QuoteEBegin-->Oh, and you haven't updated the 'standard' function's definition so that it has 4 values instead of 3 ^^;<!--QuoteEnd--></td></tr></table><div class='postcolor'><!--QuoteEEnd-->Bleargh. It shouldn't have looked like that, it's from an old fix that didn't work.
As for that code... It still doesn't work.
In the call to roll and the two calls to standard, it says that it can't convert parameter one from type int to int[]. I gather that this is what you were telling me about before, but I still don't know what to do about it. Am I making my formal parameters the right way?
In the two places where results is actually referenced, it's still telling me that the subscript requires array or pointer type, and in the second of those two, it's still giving me that l-value error.
<!--QuoteBegin--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> </td></tr><tr><td id='QUOTE'><!--QuoteEBegin--><b>edit:</b> oops! forgot to mention... the menu choice thingy in standard (yes/no/repeat) will cause problems too. I'll let you figure out the general jist of it, but you've definitely cut your work out for you ^^; <!--QuoteEnd--></td></tr></table><div class='postcolor'><!--QuoteEEnd-->
That isn't the proper way to do a recursive function call, is it? Blah.
Anyway, I'll be attaching the new code, with your fix in it.
I'll attach it so you can check it out and compare it with the notes on that page, but I knocked out your lil retry menu just to save myself some time and give you something to fiddle with for yourself ^~
int *results=new int [number*repeat];
and...
total+=results[i * repeat + j];
What exactly does this notation mean? Also, where'd the IntPtr stuff go? In the description of dynamic arrays I read in the book, that seemed to be a somewhat important part.
One other thing. This doesn't add the rolls. If I roll 4d6 six times, it shows the individual results for each die, which is what I want for the function separate, but not standard. Why doesn't it work right? From just looking at the equation, it seems like it should, unless there's something I don't understand about the notation you used to reference the array that prevents it.
int *results=new int [number*repeat];
and...
total+=results[i * repeat + j];
What exactly does this notation mean? Also, where'd the IntPtr stuff go? In the description of dynamic arrays I read in the book, that seemed to be a somewhat important part.
One other thing. This doesn't add the rolls. If I roll 4d6 six times, it shows the individual results for each die, which is what I want for the function separate, but not standard. Why doesn't it work right? From just looking at the equation, it seems like it should, unless there's something I don't understand about the notation you used to reference the array that prevents it. <!--QuoteEnd--> </td></tr></table><div class='postcolor'> <!--QuoteEEnd-->
int *results=new int [number*repeat]; creates a pointer to an array whose max elements is number*repeat
total+=results[i * repeat + j]; is equivilant to total = total + results[i * repeat + j];
The array created by pointer *results, results[], element i * repeat + j gets the value of total (which also adds the previous value of total)
Right, I understand that. The += was part of my original code. I'm having trouble figuring out what the stuff in the brackets means. Does the * indicate multiplication, or is it signaling that the data therein is a pointer type? What's the deal with +j?
cout << "C++, d00d" << endl;
i'm gonna try rewriting the thing to see what i can get (c++ is a little rusty, finished with java and moving to perl)
give it a few hours, i'll try get back to you
Unfortunately I don't really remember the 'how it works' of it, I just know how to use it ^^;
as for removing your IntPtr definition, well... I don't really know why it's needed to be honest. Looking around I've seen a few others use them for 'dynamic' arrays but none of the examples really seem truely dynamic... they're just doing what I'm doing =D
There are mentions of some 'resize_array' command though I don't know much about it nor have I seen any examples actually use it. True dynamic arrays are really things like linked lists, if it's possible using bog-standard arrays then it's outside my field of knowledge =s
Regardless, in this version or your version of the code the array doesn't magically resize to fit. Arrays are set to a size when you declare them. The only way (ignoring the enigmatic 'resize_array' for now) is to make a new array to replace it, or to get the correct sizes before you declare it =3
Once it's declared as a 0x0 array it stays that way until it's deleted (or in the case of your proggy, it crashes ^^; )
As for it not adding, that's part of your own code ~blink~
check it out for yourself: <!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->total+=results[i][j];
cout<<total<<" ";
total=0;<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
see, you reset total after every print <!--emo&:p--><img src='http://www.unknownworlds.com/forums/html//emoticons/tounge.gif' border='0' style='vertical-align:middle' alt='tounge.gif' /><!--endemo-->
I just left it in because I was more interested in helping you with the multi-dimensional arrays.
take the 'total = 0' part up to the top so it's outside and before the loop and it'll total them quite happily ^^
To be honest I can see why your teacher wanted multiple functions. If you rewrite the program with the order of what declarations you're doing in mind then it'd be a neat lil package =3
check it out for yourself: <!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->total+=results[i][j];
cout<<total<<" ";
total=0;<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
see, you reset total after every print <!--emo&:p--><img src='http://www.unknownworlds.com/forums/html//emoticons/tounge.gif' border='0' style='vertical-align:middle' alt='tounge.gif' /><!--endemo-->
I just left it in because I was more interested in helping you with the multi-dimensional arrays.
take the 'total = 0' part up to the top so it's outside and before the loop and it'll total them quite happily ^^[/quote]D'oh! Knew it was something simple in that case.[quote]To be honest I can see why your teacher wanted multiple functions. If you rewrite the program with the order of what declarations you're doing in mind then it'd be a neat lil package =3 <!--QuoteEnd--> </td></tr></table><div class='postcolor'> <!--QuoteEEnd-->
For what I'm doing, yes, you want multiple things. As I said though, what I'm doing is quite a bit more complicated than the assignment I was given. If I was just doing the work I was supposed to, I'd have finished two days ago without any help. ^_^
Why do a more complicated assignment when it doesn't look like you know how? I see guys like you at school, asked to write a fractal generating program and end up spending so much time calculating how much time is spent at the cpu level and how much at the video card level that the program never does the basic requirements. Yeah it can be fun to learn extra stuff on the side, but why not code your own hobby projects and just do what your assignments ask of you?
Also, not having looked at your problem too much, I'm confused by all this talk of dynamic arrays. I thought you needed to make malloc calls if you wanted an array of dynamic size at runtime.
Anyhow, good luck with that.
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->if (...)
{
...
}
else
{
...
}<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
...over the style ..
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->if (...) {
...
} else {
...
}<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
You can specify that too. What makes it really nice is that it keeps you're code written the same everywhere. So far this only works with C and C++.
Anyone want? It's free. <!--emo&:)--><img src='http://www.unknownworlds.com/forums/html//emoticons/smile.gif' border='0' style='vertical-align:middle' alt='smile.gif' /><!--endemo-->
[edit] oh, and you'd need a for function to do it my way too <!--emo&:0--><img src='http://www.unknownworlds.com/forums/html//emoticons/wow.gif' border='0' style='vertical-align:middle' alt='wow.gif' /><!--endemo--> <!--emo&:0--><img src='http://www.unknownworlds.com/forums/html//emoticons/wow.gif' border='0' style='vertical-align:middle' alt='wow.gif' /><!--endemo--> <!--emo&:0--><img src='http://www.unknownworlds.com/forums/html//emoticons/wow.gif' border='0' style='vertical-align:middle' alt='wow.gif' /><!--endemo-->
I love Object Orientated Programming ^^
<b>edit:</b> on a fun note, swift is right... you could've done it without touching arrays at all. At least for what you've got there already =o
[edit] Im not gonna fix anything, I just thought I'd mention that in the few weeks since I finished english 12 last semester I seem to have forgotten everything about sentence structure and linguistic grace, as evidenced by the above post <!--emo&:p--><img src='http://www.unknownworlds.com/forums/html//emoticons/tounge.gif' border='0' style='vertical-align:middle' alt='tounge.gif' /><!--endemo-->
Oh you will, OO programming is the best. I do understand that its hard to realize at first though.