0

I tried reading other questions to find the answer to my question but I got tired of seeing the answer being in a different coding language. I want to change the amount of numbers in my random error code. I am genuinely new to this so please no rude comments.

#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;
 
int main () {
   int i,j[3];
 
   
   srand( (unsigned)time( NULL ) );

I set the array for j to 3 so I could try and get a maximum of 3 numbers.

for( i = 0; i < 1; i++ ) {
      
      j[3] = rand();
      cout <<"Error code: " << j << endl;
   }

   return 0;
}

Here is where the error comes in, the output of the code only sends the variable address instead of the random number. I really need help with this before I could continue my project. Please help.

Edit: Variable address is "0x7ffc9b46ed5c"

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
Skyy Civil
  • 63
  • 8

2 Answers2

2

I can assume you want to set an array of size 3 to random numbers.

I set the array for j to 3

j[3] = rand();

You're not doing that, you're setting the 4th element in your array j as a random number, which happens to be out of bounds and invokes undefined behavior.

cout <<"Error code: " << j << endl;

Outputs the address of the first element in array j. Not the whole array.

DarthQuack
  • 1,254
  • 3
  • 12
  • 22
  • So how would I do this correctly? That's what I'm looking for. – Skyy Civil Jan 06 '21 at 13:37
  • 2
    Not to sound rude, but you seem to lack basic C++ syntax like loops and arrays. You should look up tutorials or a beginner's book to learn them first. [This is a very good and maintained list](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) you can take a look at. However, `for (int i = 0; i < 3; i++) j[i] = rand();` should do what you want. – DarthQuack Jan 06 '21 at 13:44
  • Change your `for` loop, so you can iterate over the array's elements. Then for each element `i`, call rand(). You can use either an array iterator or the traditional for-loop with the counter. – Arkoudinos Jan 06 '21 at 13:47
  • I'm pretty new at this stuff so thanks! I might close this question. I found out what I did wrong a few mins ago. – Skyy Civil Jan 06 '21 at 13:49
  • 1
    @SkyyCivil Best of luck :-) – DarthQuack Jan 06 '21 at 13:52
1

How i would do it:

#include <iostream>
#include <ctime>
using namespace std;

int main()
{
  srand(time(NULL));
  int j[3];
  for (int i = 0; i < 3; ++i)
    j[i] = rand(); //sets every index of the array to rand()
  cout << "Error code: ";
  for (int i = 0; i < 3; ++i)
    cout << j[i] << '\n'; //outputs all values from the array

  return 0;
}

When you declare an array of size 3 by int j[3] you can refer to the first value by j[0], second value by j[1], and the third value by j[2]. If you want to display every value from your array you can use a normal for loop (using j[i]) or a range based for loop:

for(int& i : j)
    cout<<i; //this loop will display every component from your array
Pete Becker
  • 74,985
  • 8
  • 76
  • 165
czarson
  • 125
  • 7