help on this program

Pages: 12
I have a program in which i cant seem to display out the numbers to the screen.

Last edited on
Couple things I can see right off the bat:

1. You're declaring your arrays incorrectly - there is no reason to put parenthesis around your values. It should be declared like:

int lotto[55] = {0};

This will initialize all values in the lotto array to 0, as you were intending to do.

2. There is no reason to be declaring global variables in this program. You'll need to break this habit. Put them below int main().

3. Why did you put the entire body of main() in a while statement using the argument (doit) when there's no way doit will ever be false? You do not reference this value anywhere else in your code. If you want it to run continuously, just use while(1) and save yourself the extra coding.

4. You need case 2: and a default case for your switch statement. If you don't want to put them in there, why even use a switch statement? It would be completely useless. I'm sure you're trying to be clever, allowing your user to tell the program whether or not they want to play, but consider what will happen if your user enters something other than "1" into the program.

5. There's no reason to use two variables on your last for loop when they're both starting at the exact same number and incrementing at the same time. Just use one variable for each array...

6. Ask yourself what you're actually trying to keep track of with the lotto[] array. Then, ask yourself what lines 49-51 are doing. Imagine luck gets set to 49. Then imagine what happens in line 49. What is lotto[luck] going to be set to? Then imagine what you were trying to do with lines 50+51, and tell me if that looks right to you.

Fix those things and look over the rest of your code and you'll probably be able to clean it up enough to get it working. :) Repost the fixed code and I'll check over your logic.

Happy coding!
Well, yeah. You're stepping through the array 1 by 1, starting at the number 1 and going to 55. Of course it's going to give them to you in numerical order. :P

This is why I said you need to look what you want your array[] array to hold. You have it set to hold 56 integers, which implies you want to use it as a tally of how many times a number was chosen, or as a way to compare the LOTTO array. If you want to see which numbers you entered in order then you need to store them in a separate array and read back that array (such as, an array with size 6). That array would store those 6 numbers, in the order they were given, meaning it would be very easy to read them back out in the original order as well.

The arrays are fine as they are. However, your for statement logic needs some work. Currently your array[] values are being set to the value of the variable 'numbers', however in your for loop's if statement you're looking for array[] values that are equal to 1. Well, the same thing is happening with your luck array. So, when your for loop goes through looking for 1's in these arrays, it's only going to find this to be true at array[1] and lotto[1], if you both chose 1 and rolled one. Otherwise, array[45] will = 45, and lotto[30] will = 30. See the issue here?

All you'll need to do is slightly adjust your if statement logic. I would suggest something like this:

1
2
if ((array[p] == lotto[p]) && array[p]!=0 && lotto[p]!=0)
         match++;


match++ is a much better way to write match = match +1;

Also, you do not need return statements AND break statements in your cases. And, for the love of God, get rid of the Z variable on your last For loop. :P The z and p values serve the exact same purpose.

Hope that helps.
FIRST: since you placed the numbers that the user inputted into array[number] = number they are now lined up by value.
example;
input = 6
array[6]=6
input = 53
array[53]=53

Go back to the line array[numbers] = numbers and change it to array[n1] = numbers
that should make it print out how the user input their numbers.

SECOND: there is something screwy with what's going on when the user guesses a right number, your program isn't outputting how many answers were correct. I'm not sure what you were planning with the line: if ((array[z] == lotto[p]) && (array[z] == 1) && (lotto[p] == 1)); but it needs to be changed to run properly. I'll leave that up to you.




THIRD; actually I just saw that the way you set up the if(arrays match) test, the first problem can't be changed without causing a second problem. consider using a while loop to test instead of (or inside of) your for loop. (Or just be content to have the numbers print out in numerical order.)


LAST; Your case 2: test is outside of your switch argument, so it basically does nothing. Place it in your switch {}.
Last edited on
Thanks for the great comments.I dont know what happened to my previous post.
Last edited on
Yeah, your post was there when I replied, then it poofed. Really odd, lol.

Anyway, here's what I would do:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include<iostream>
#include<stdlib.h>
#include<conio.h>
#include<time.h>
using namespace std;


int main ()
{
		int array[55] = {0}, lotto[55] = {0}, picked[6]={0}, numbers, choice;
		int match = 0, luck = 0, money = 0;

		system("cls");
		srand((unsigned)time(NULL));

		cout << "Play California LOTTO -- 1\n\n";
		cout << "Quit                  -- 2\n\n";
		cout << "Choose Option 1 or 2: ";
		cin >> choice;
		system ("cls");

		switch (choice)
		{
            case 1:
            {
                for (int n1 = 1; n1 <= 6;n1++)
                {
                    cout << "Pick any Number between 1-55: ";
                    cin >> numbers;
                    array[numbers] = numbers;
                    picked[n1-1]=numbers;
                    //system ("cls");
                }
                cout << "These are the 6 Numbers You have Picked \n";

                for (int w = 0;w < 6;w++)
                {
                    cout << picked[w] << " ";
                }
                getch();

                cout << "The Ball is Rollin'....Good Luck!!....\n";

                system ("pause");

                for (int a = 1;a <= 6;a++)
                {
                    luck = rand() % 55 + 1;
                    lotto[luck] = luck;

                }

                cout << " The California LOTTO Drew\n";
                for (int u = 1;u <= 55;u++)
                {
                    if (lotto[u] > 1)
                        cout << lotto[u] << " ";
                }

                for (int p = 1;p <= 55;p++)
                {
                    if ((array[p] == lotto[p]) && array[p]!=0 && lotto[p]!=0)
                        match++;

                }
                cout << " Yo have" << match << " matches ";
                system("pause");
                break;
            }

            case 2:
                return 0;
            default:
                return 0;

	}
return 0;
}


Look at lines: 31, 36, 38, and 62. If you have specific questions about what I'm doing, please ask and I'll go into more detail. :)
Last edited on
each index location instead its matching the numbers if i am right.


Last edited on
Ah, yes, that's easy to do then. We'll just need to change a few minor things and you'll be all set. I have class now though, so this'll have to wait a bit. :)

To give you a hint though... you'll be using array[numbers]++ instead of array[numbers]=numbers. The latter sets the value of array[numbers] to whatever value you typed in, whereas the former just increments the value by 1 (ie 0 -> 1). You'll also need to put some sort of extra error proofing in to make sure the person does not choose the same number again. Something along the lines of if(array[numbers]!=1)

Hope that helps!
Could you try to help me after class, i know i will add a while statement that gives a error to user if the number is bellow 1 or over 55 and if they enter the same number i will put a defense statement for that also. but i dont really understand by the thing that you suggested but i will try my best understand figuring out the logic you are hinting.
Last edited on
Well, since I'm just waiting on the professor to come answer a few questions, I have a moment to get back to you on this. :)

Okay, let's think of it this way:

You enter in 45 for the variable 'numbers'.

So, array[numbers] is equivalent to array[45].

array[45] is a pointer to a memory location that has a value of what? zero, at least at first.

So, incrementing array[45] by 1 (ie array[45]++;) is going to take that zero value and increase it by one. Now, array[45] is equal to 1.

So, you now have an array of all zeros, except for array[45] which is equal to 1. If you went through the array and searched for all elements that were equal to 1, you would come across array[45] and your program would say "bingo!". If you were using a variable such as 'i' to go through your array (starting at 1 and going through 55), when i = 45 you'll come across this scenario, and can then use the variable 'i' to tell the program (or the user) that this number was found in the array.

So, if you do the same thing for lotto[], then all you'll need to check is if both elements are equal to 1. If they're not, then the user didn't get a match.

Make more sense? If not, I can try to find a different way to explain it. :)
i understand its like matching and this makes complete sense
Last edited on
Ugh, I hate it when my session times out.

Anyway, the pointers he is talking about are not the same thing that I'm referring to when I say your array is a pointer. Arrays by definition are pointers to blocks of memory. So, it's physical impossible to not use an array in a fashion that will "use pointers".

whether we use the number 1 for an array element value or the number you've chosen you will end up being safe with your professor.
ccould you give me an example so i can understand this concept that you are talking about a little more better. If its ok with you..
Last edited on
I tried the code with the array[numbers]++; instead of array[numbers] = numbers;
but it just displays all values entered as 1... how could i then display the numbers and do i have to also change up lotto[luck] = luck;
sorry but could you please explain this to me again?
Last edited on
@OP: This is just a small tip, but you don't need to keep using random names for your forloop variables.

1
2
3
4
5
6
7
8
9
10
11
for(int i = 0; i < 10; i++)
{
	cout << "Number Up: " << i << "!";
}

// some more code stuff.

for(int i = 10; i > 0; i--)
{
	cout << "Numbers Down " << i << "!";
}


These names won't clash or cause problems because the scope of each int i is local to the forloop it's used in.
You didn't look at line 31 of my code. Look again, and also look at what I changed on line 10. Hint: "picked".

Here's the logic broken down:

Your arrays need to store some sort of comparable value so that when you go to compare each element later on, you can accurately say that two numbers match up. Whether you store a "1" or the actual number that you rolled is inconsequential - the script will work in the same way no matter which you choose to go with.

array[numbers]++; is the way you'd increment that specific element of "array[]" in order to mark that the number has been chosen, since 'numbers' will correspond with the number the person chose. Think of it like a boolean value... 1 is true, 0 is false. If you go through the array later on and look for all the elements that are set to 1, you'll have found all the numbers the person chose.

array[numbers] = numbers; will simply set that element to the number the person chose instead of using 1. Either way, as long as you can compare this to your other array for lotto, it doesn't matter, as long a the same logic is used on both arrays. Comparing 1 to 1 or say, 45 to 45 doens't matter - either way they're going to be equal. All elements of your array that didn't get changed (aka, all the numbers the user DID NOT choose) are going to still be zero.

If you wanted to display the numbers chosen in order (<--- key phrase...) you will have to keep track of them in order. This is done very simply by storing your values in an array just for this purpose. There's no reason you can't store the same number in multiple arrays at the same time.

Or, as you go through your for loop, since your variable (let's say 'i') corresponds with the element of the array that was chosen, all you have to do is something like cout << "i"; whenever an element is either equal to 1 (if you went the array[numbers]++ route) or if it's equal to whatever number you chose (if you went with the array[numbers] = numbers; route).
I understand the array you added "picked" and that makes sense if i want the user to see them in order. but i still dont understand the logic
Last edited on
User is prompted to enter a number, 'number'.
User enters 45. 'number' now == 45.
array[number] is the same as array[45].
array[45] is currently == to 0.
array[45]++; increments the value by one.
array[45] now == 1.

Lotto ball picker is asked to pick a ball, 'picked'.
Lotto random ball picker chooses 45. 'picked' now == 45.
lotto[picked] is the same as lotto[45].
lotto[45] is currently == to 0.
lotto[45]++; increments the value by one.
lotto[45] now == 1.

So...
array[45] == 1
lotto[45] == 1

Guess what - they're the same, and you have a match!
Increment 'match' by 1 using match++;

Let's say the user chose 45, but the lotto picker didn't. So, using the same logic:

array[45] == 1
lotto[45] == 0 (Why? Because all values of this array are set to 0 by default, and we didn't change the value since the lotto picker didn't choose ball 45).

Are the values of array[45] and lotto[45] the same? Nope. Therefore, you don't have a match.

If you chose to enter in the number, it still wouldn't matter.

If you both chose and picked 45, you'd end up with the following results:

array[45] == 45
lotto[45] == 45

If you chose 45 but didn't pick 45, you'd end up with these results:

array[45] == 45
lotto[45] == 0

In the first example, we have a match. In the second, we do not.

Again, all we're doing is comparing the value stored in these two arrays. Hell, you could choose any number you wanted to, as long as you have one value that represents "no number chosen" (typically 0) and one value that represents a chosen number (either 1 or the actual number chosen in these examples).
So would this be the right way to do it do i need to add a bool statement ?


Last edited on
Yes, lines 31, 32, 39, 64, and 65 are great. You do not need to add any additional boolean statements though.

You also need to check our your logic on line 57 (should you be using '>' here?) as well as line 58. lotto[u] will not turn out to be the number the lottery picker chose, so you'll need to use a variable that will correspond with this number (hint: you declared it in the for loop!).

Other than that you should be all set!
Pages: 12