Re: lottery number thing problem
From: Centurion (spam_this_at_nowhere.com)
Date: 11/12/03
- Next message: Niklas Borson: "Re: Passing Structure to a function"
- Previous message: Freddy Flares: "Is it ok to inherit from vector?"
- In reply to: moi: "lottery number thing problem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 13 Nov 2003 09:45:33 +1100
moi wrote:
> im getting a recurring
> error regarding the syntax of my main() function argument calling in
> lottery_numbers[] array. so any help as to whats glaringly going wrong
> here?? oh and some brief guidance on whats the easiest way to sort the
> numbers into ascending order in the array before i cout << them.
What error exactly? I'm not going to cut, paste, compile this either - an
error message would be useful (keep reading).
As for the sort, you can do a bubble sort in about 4 lines of code, or you
could use a vector and then leverage some of the STL magic for sorting etc.
Not sure if you're allowed to use the STL in your assignment tho :-/
> p.s. i am aware from some replies to earlier posts that some of the things
> like the includes' etc is a bit archaic but for the purposes of this
> assignment, i dont really need it pointing out. cheers.
<PEDANT>
So *when* do you plan on writing your code correctly? Get into good habits
and it will serve you well. :) If you're aware of the "include" errors
below, then why not just fix them?
</PEDANT>
> here's the code as it stands....
**Snipped - lets just look at "main"**
> void main(int lottery_numbers[5])
oops. try:
int main(void)
Your "main" declaration is telling the compiler to expect integers passed
from the command line - not what you are wanting. Also "void main()" is
generally accepted as B.A.D. :)
> {
initialise your array here like this:
int lottery_numbers[6];
Also, replace any "lottery_numbers[5]" with "lottery_numbers[6]". You want
SIX elements in your array, not 5. Sure the array is indexed from 0 to 5,
but you need to define your array properly otherwise your "for (int x=0; x
<= 5; x++)" will overflow the array; index 5 doesn't exist in your original
code.
Hint: I always define my arrays and loops like this:
int myArray[10]; // an array with 10 elements
for(int i=0; i<10; i++) // iterate through the array
{
myArray[i]=i*i; // store the square of the index at the index.
std::cout << myArray[i] << std::endl;
}
Notice "int myArray[MAX_SIZE]" and "for(...; i<MAX_SIZE;...)". Just keeps
your brain on the right track :) Notice I've used "i LESS THAN" not "<=".
> DrawNumbers(lottery_numbers[]); //'draw' the numbers into the array
> for (int i = 0; i <=5; i++) // for each of the six elements of the
> array...
> {
> cout << lottery_numbers[i] << " "; //print out the contents to screen.
> }
> getche();
insert here:
return 0; // main returns an int now, so let's give it one :)
> }
No doubt others far more skilled than I will point out my own short-comings
:P Good luck with your studies!
--James
__________________________________
A random quote of nothing:
Fatal Error: Found [MS-Windows] System -> Repartitioning Disk for Linux...
(By cbbrown@io.org, Christopher Browne)
- Next message: Niklas Borson: "Re: Passing Structure to a function"
- Previous message: Freddy Flares: "Is it ok to inherit from vector?"
- In reply to: moi: "lottery number thing problem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|