Hack Forums
[C] Need some help with while rand() function - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C] Need some help with while rand() function (/showthread.php?tid=6140353)



Need some help with while rand() function - omy11 - Feb 16, 2021

So i am currently trying to create something where it randomly generates 13 letters depending on how many the user wants to generate.

Ex: User inputs they want to generate 5 codes (13 letters each)

program then generates 5 codes.

I can't seem to figure out how to set a max number of generated codes and with a limit of 13 letters each. Whenever i run the program i just get this:
https://gyazo.com/055750c49c3d3cff4fb0132a7de139e2


This is what i have so far for that, any help is appreciated!
Code
int amount_codes;

cout << "Enter amount of codes: ";
cin >> amount_codes;
}

cout << "You have chosen " << amount_codes << " codes to generate! (If it says 0 please input an integer). " << endl;
while (true) {
int letter, count = 0;

while (count < 100)
{
letter = rand() % 26; //0, 1, 2, 3, 4, 5, 6, ....25

cout << static_cast<char>(letter + 65);


}
cout << letter;
}



RE: Need some help with while rand() function - SIM-4717218 - Feb 17, 2021

do you initialize the rand with srand(time(NULL)); ?


RE: Need some help with while rand() function - HFDone - Feb 17, 2021

https://pastebin.com/WfrAbjKD


RE: Need some help with while rand() function - SIM-574702 - Feb 17, 2021

You're not using amount_codes to limit loop.
You have outer while loop that never ends.
Also inner loop has "< 100" and you're generating N codes of 13 so where does 100 come from.

I see you're trying to do it yourself so I'm not going to spoon feed you the code but if you're still stuck after these tips I can make you fixed version.


RE: Need some help with while rand() function - omy11 - Feb 17, 2021

(Feb 17, 2021 09:59 AM)matemisic Wrote:  You're not using amount_codes to limit loop.
You have outer while loop that never ends.
Also inner loop has "< 100" and you're generating N codes of 13 so where does 100 come from.

I see you're trying to do it yourself so I'm not going to spoon feed you the code but if you're still stuck after these tips I can make you fixed version.

I'm going to take a look at again and see if i can figure it out now. Super new to c++ so some things i do does not make sense lol.

Thank you!


RE: Need some help with while rand() function - tux1 - Feb 17, 2021

I had a quick play around with this, if I understand your question correctly. The main issue with your code and why you were getting loads of random letters was because you did while(true) with no end so it just went on forever. Have a look at the below and read the comments, hope this helps


Code
/******************************************************************************


*******************************************************************************/

#include <iostream>
#include <stdlib.h>     /* srand, rand */
#include <time.h>       /* time */

using namespace std;

int main()
{
    int amount_codes;
    char letters[] = "abcdefghijklmnopqrstuvwxyz"; //char array, we will use this to select random letters

    srand (time(NULL)); //seed the random, otherwise it will be the same each time
    cout << "Enter amount of codes: ";
    cin >> amount_codes;

   
    cout << "You have chosen " << amount_codes << " codes to generate! (If it says 0 please input an integer). " << endl;
    int i = 1; //counter for the codes
    while (i <= amount_codes) { //loop through the amount of codes
        cout << "Code " << i << ": ";
        i++; //increase the counter
        for(int j=0; j<13; j++){ // this generates a random letter, one by one (select from char array)
            cout << letters[rand() % 26]; //print char one by one
           
        }
        cout << endl; //print new line after you make the random string
    }

    return 0;
}