Hello! I am extremely new to programming and have a question about some code I've been working on. For my homework, I need to simulate a lottery draw of five number (using random numbers 0-9), ask the user for their numbers, compare and display both sets of numbers and count the number of matches. The issue I'm having is when it comes to counting the matches. I just can't get the counter to work properly. I've tried for loops and writing the code literally (if (lottery[0]==user[0])…)and nothing has worked properly. Also, I am aware that using namespace is frowned upon but my instructor insists upon it for this beginner course. Here is the full code:
After looking at your code for a while I figured what your problem was:
You're creating two instances of the arrays lottery and user. You have for both, one global variable and one variable in main.
To fix your code and make it work I simply did that:
1 2 3 4 5 6
int main()
{
//int lottery[number];
//int user[value];
int i = 0;
//[...]
EDIT: Some people are probably going to mention to you that you should avoid using global variables. So what you could do in this case is delete your globals and keep the two arrays you defined in your main function. You would then need to pass them as argument in your functions generateNumbers, getUser and displayValues.
Also I don't know why you included "stdafx.h" but I also removed it to compile your program and it works just fine.
You fixed every part of my code that was already working fine( except for the for loop used to generate the numbers, thanks for pointing that one out). The part I'm trying to sort out is the match counter. When I enter numbers the program says there are no matches even if there are.
There's nothing wrong with "using namespace std" in a short program, especially for beginners.
I disagree.
A standard library header must introduce certain names, but it may also introduce names from anywhere else in the standard library. If a user borrows an unqualified name from the standard library, it's quite possible that the result works on the student's machine but not on the instructor's.
At best the program won't compile. At worst, the program silently uses the wrong type or calls the wrong function. IMO, it's best to just use unambiguous names, and leave using x to mean "I want ADL here".
Well that’s excessively rude. So I’m a piece of human garbage for what reason exactly? This stuff doesn’t come easy to me so it does make me a little edgy. Especially so when I’m getting responses that are, for the most part not working out or are not really helpful. My last comment only meant that I figured it out on my own but I still appreciate people taking the time to look at it.