I'm trying to store random numbers generated from the function using a loop but I don't have any idea how to even store the values into the array from a loop.
I do know how to store values (like using cin), but I want to be able to store the values that the function returns into the array.
I have a void function that has the rand() and prints out a random number with some char symbols to make it look nice.
I want to take that random generated number and be able to store it into a string array (because of the char symbols).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
void function(){
//Do something
//rand() etc..
}
int main(){
constint SIZE = 100;
int userInput;
string arr[SIZE];
for(int i = 0; i < SIZE; i++){
//Storing function() into array
if(userInput == 0)
break;
}
I think you are headed in the right direction, but you do have some problems.
First you did not seed the RNG with "srand". I would use: srand(static_cast<unsignedint>(time(nullptr)));.
Because there is not much code in the function I would guess the the call to "rand" might produce numbers that would be non printable characters.
Using the function to get a random number you either need to pass the array and size to the function. You may also need to pass an index to the function to keep track of where you are at in the array. Or you may be able to make a static variable in the function.
What's the difference between that srand and srand(time(NULL)); ?
Also, inside of the function() I call another function that are printable. The function() is just for the randomizer. However, since the final answer will be from the void function() I believe that's what I need to use to store it into an array.
As for storing it into the array, I have no idea how to do it. I see you gave me an explanation, but I'm not that good at functions combined with arrays.
"srand(time(NULL));" is an old C way and worked fine back in its day. What I showed you is the more up to date form to use from C++11 standards on. You may want to take some time to watch this video https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful It does describe the problems with using "rand". It also talks about the more C++11 way of using the header file "random" and "chrono".
You did not say anything about another function earlier, so I do not know what you are trying to do.
Another thing I noticed: if(userInput == 0). You have defined the variable, but it is not initialized, so you are comparing a garbage value to (0) zero which may never be true. Also you never input a value for "userInput" and since the if statement will always be false except for that 1 time it makes the if statement kind of useless.
Hello Andy!
Thank you for the help. I'll take a loop at the video.
I was testing your program and it looks nice.
Although, I have a question that will probably give you an idea of what exactly I want.
When you remove the comment from the cout you get the random letters right?
I was trying to call the function without doing the loop to see if the same letters/chars generated are the ones stored into the array.
I managed to get something closer to what I want with my program.
However, it seems that the random generated numbers are different from that of the array when I store it like array[0] = function() etc...
Also, for some reason when I do exactly like that above example the function prints the random number. I just set the index 0 from the array equal to the function. I'm not sure why is it getting printed.
Basically I have two functions,
One function prints the array values that I have set. string arr1=[5]={"A","B","C","D","E"};
Second function prints a random element from the array by calling the 1st function.
function1(rand() etc..)
This second function also prints a single element from the array with some design, like
1 2 3
| |
- A -
| |
Then I would like to that random element into a new array and save it. Since every time you call the function a new element is printed. I want to be sure to save it.
I want to be able to store the values that the function returns into the array.
I have a void function ...
You want to store a value returned by a function, but you have a function that does not return any value.
If you want a value from function, then the function must return a value.
void function(); does not return a value. string function(); does return a std::string value. void function( string& arg ); may write a std::string value into the arg. Hence, "return a value".
Thank you all for the help. It was very helpful and I'm actually getting closer and closer to what I want. I think I will continue this a lone so I can understand it better.
Also, keskiverto,
that pretty much sums up what I want (the void problem). That's something new I just learned and I'm really happy because that's going to save a lot of spaces.