Random number problem; No output

I want this program to show a random row consisting of seven numbers between one and 35. It worked great until I added a bool to make sure the same number didnt show twice. Thats when the problems started.

The problem:
Nothing happens when I start the program. No numbers, no message, no nothing.
I cant figure out what Im doing wrong.

The code:
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
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int main(){
    
   srand(time(0));        //first number should be random, not fixed

   int arr[7];            //my array of random numbers

   bool same=false;       //make sure the same numbers dont show up twice

   //first loop to fill in the numbers in the array
   for(int i = 0; i < 7; i++)
   {
      
      arr[i] = rand() % 35 + 1;

      //look at the array to make sure the same number dont appear twice
      for(int j=0; j<7; j++)
      {

          //if they do, set same to true
          if(arr[i]==arr[j])
          {
              same=true;

          }//END if

      }//END nested loop

      //if the same number appears twice, change the number and continue
      if(same)
      {
          i--;
          same=false;

      }//END if

      //if every number is unique, print them to the screen
      else
      {
          for(int i = 0; i < 7; i++)
          {
            cout << arr[i] << " ";

          }//END nested loop

      }//END else

   }//END for loop

   return 0;

}//END main 


EDIT
Changed if(arr[i]=arr[j] to [code]if(arr[i]==arr[j] as James2250 suggested. Thanks!

But the problem still remains
Last edited on
if(arr[i]=arr[j])

This should be == (for comparison) not = (for assignment)
Thank you, but it still wont work. Must be something else to
closed account (3qX21hU5)
1
2
3
4
5
6
7
8
9
10
11
12
arr[i] = rand() % 35 + 1;

      //look at the array to make sure the same number dont appear twice
      for(int j=0; j<7; j++)
      {

          //if they do, set same to true
          if(arr[i]==arr[j])
          {
              same=true;

          }//END if 


You are trying to compare variables that are undefined. Let me explain, When you go through the first for loop the first time you assign a random number to arr[i]. Then right after that you try and compare the first element in your array to all the other elements in the array. The problem is the other elements in the array have not been initialized and they are undefined. That is one problem that I seen in your code it always bad to do anything with undefined elements and variables.
same will always be set to true because arr[i]==arr[j] is true when i == j. You should probably change the inner loop so that you only check the array elements with an index less than i.
Change
1
2
3
4
5
6
7
8
9
10
11
12
for(int j=0; j<7; j++)
      {

          //if they do, set same to true
          if(arr[i]==arr[j])
          {
              same=true;

          }//END if

      }//END nested loop

to
1
2
3
4
5
6
7
8
9
10
11
12
for(int j=0; j<i; j++)
      {

          //if they do, set same to true
          if(arr[i]==arr[j])
          {
              same=true;

          }//END if

      }//END nested loop


you check until you reach your curent inserted number.
Example:
arr[0] = 5;
arr[1] = 7;
arr[2] = 4;
you add arr[3] to 5 again.
when you test if(arr[i]==arr[j]) you will get it true for the first(j = 0);
You can also add a break after the same = true; so you don't continue to the next numbers.
Also like this(for(int j=0; j<i; j++)) you evoid the posibility of using an uninitialized variable since they are initialized 1 by 1 after each loop.
Topic archived. No new replies allowed.