Hello. I am fairly new to C++ so i apologize in advance if this is a dumb question.
I am trying to write a program that will generate a random number, check to see if that number is located within an array, and if not to place it in the array. I have managed to get it to work when the checking code is located in the main function but not when it is enclosed in it's own void function. More detailed explanation of problem after code segments.
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
|
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int hand [45];
int c, x, y, d, a, b, f;
int main()
{
srand((unsigned)time(0));
for (int a=0; a<45; a++)
{
c = (rand()%108)+1;
for (int f=0; f<45; f++)
{
if (hand [f] == c)
cout << "duplicate" << endl;
else if (hand [f] == 0)
hand [a] = c;
}
cout << hand [a] << endl;
}
cout << "number ";
cin >> x;
cout << hand [x-1] << endl;
return 0;
}
|
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
|
void check ()
{
for (int f=0; f<45; f++)
{
if (hand [f] == c)
cout << "duplicate" << endl;
else if (hand [f] == 0)
hand [a] = c;
}
}
int main()
{
srand((unsigned)time(0));
for (int a=0; a<45; a++)
{
c = (rand()%108)+1;
check ();
cout << hand [a] << endl;
}
cout << c;
cout << "number ";
cin >> x;
cout << hand [x-1] << endl;
return 0;
}
|
As I said the code is supposed to generate a random number, check it against an array, and if it is not present place it in the array and print it on the screen. If there is a duplicate entry it is supposed to print duplicate on the screen and start over until it generates 45 unique random numbers. After which it prompts you to chose a position within the array and display the number.
The first code works the way it is supposed to. However the project i am working on requires that i use my "check" function multiple times and in the interest of reducing code length to save space i would like to be able to run it from it's own separate function that can be called. Hence the Void check function in the second code segment.
When i compile and run the second code it prints out one random number followed by nothing but zero's and an occasional "duplicate" line. However when i enter address 1 at the end of the program it displays the last random number that was generated. Any address other than one retrieves a zero from the array.
I have a feeling that this is happening because I am not terminating the void check function properly. I have tried to terminate it with return and break but am still unable to make it work properly. Any assistance that anyone would be willing to give me with this problem is greatly appreciated.
EDIT: The issue with printing one random number and nothing but zero's after that is a minor issue since that is merely there for testing purposes while the project is in development. Also the output problem has been solved thanks to Albatross.
The bigger problem of the void check() function not storing data correctly within the array is still withstanding.