Reputation: 63
I am working on a lottery application where the computer generates 5 random numbers and stores them into subscripts. For some reason, my array is outputting letters and numbers in my code. Can anyone give me an idea as to why this may be happening?
#include <iostream>
#include <iomanip>
#include <random>
#include <ctime>
#include <time.h>
using namespace std;
int main(){
const int digits = 5;
int winningDigits[digits];
srand(time(0));
winningDigits[0] = rand() % 10 + 1;
winningDigits[1] = rand() % 10 + 1;
winningDigits[2] = rand() % 10 + 1;
winningDigits[3] = rand() % 10 + 1;
winningDigits[4] = rand() % 10 + 1;
cout << winningDigits;
Upvotes: 1
Views: 575
Reputation: 304
To display your array you can use:
for (int i = 0; i < digits; i++)
cout << winningDigits[i];
cout << winningDigits; doesn't access every member of your array. It gives you the address.
You can also simplify your program by putting everything in a for loop:
for (int i = 0; i < digits; i++)
{
winningDigits[i] = rand() % 10 + 1;
cout << winningDigits[i] << " ";
}
Upvotes: 0
Reputation: 180935
cout << winningDigits;
Is printing the address of the array and not the contents of the array. The reason you are seeing letters and numbers is the address is being displayed in hexidecimal
You can print out your array with a ranged based for loop like:
for (const auto& e : winningDigits)
cout << e << " ";
Upvotes: 3