Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

Need some help with while rand() function

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;
}
do you initialize the rand with srand(time(NULL)); ?
https://pastebin.com/WfrAbjKD
[Image: bitcoin.png] Be Nice For Once 15hVUVz7xA4kazHdk2BuErENGCskVwjYXP
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.

(Feb 17, 2021 09:59 AM)SIM-574702 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!
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;
}

Hey there,
If I have helped you or if you just enjoy my posts, please consider donating bitcoin to me 1DpL2kcxuidEsjt2MMN2AYufcVoXbi3v2X

[Image: FtwuQgr.gif]


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by FhackMar 2, 2022 03:52 AM 4 307 Mar 2, 2022 03:52 AM
Last Post: Fhack
[C] by F.A.S.TDec 18, 2018 10:59 PM 9 419 Dec 18, 2018 10:59 PM
Last Post: Sajuuk
[C] by puppetrocksFeb 5, 2016 09:28 PM 7 292 Feb 5, 2016 09:28 PM
Last Post: 0xDEAD10CC