-2

I am trying to make a password generator. Everything was going smoothly until I tried to repeat the main function at the end. The code goes like this:

I also would appreciate feedback on the code. I am still learning (when do we stop learning though?) and I want to improve as much as I can and as fast as I can.

// password generator
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

int main ()
{
    char answer;
    int c=0;
    int passlength;
    char randLWUPchar; // lowercase, uppercase, characters + numbers
    srand(time(NULL)); // seeding function


        printf("Type the wished password length:");
        scanf("%d", &passlength);

        while(c < passlength){
            randLWUPchar = "qwertyuiopasdfghjklzxcvbnm1234567890QWERTYUIOPASDFGHJKLZXCVBNM"[rand () % 65];
            printf("%c", randLWUPchar);
            c++;
        }
            printf("\n\n\t\t...if it fails to prompt as wished, please restart...\n");

        printf("\n\n\t\t   Please select Y to restart or N to close");
        scanf("%c", &answer);

        if (answer == 'Y'){
                return main();
        }
        else if (answer == 'N'){
                return EXIT_SUCCESS;
        }
}
anonymoose
  • 819
  • 2
  • 11
  • 25
Hersir
  • 1
  • 1
  • 1
    that's an unwanted recursive call to `main`... You should make that a subroutine, and call it from "main" when user types Y. – Jean-François Fabre Jun 12 '17 at 19:00
  • 4
    You're not *repeating* the `main` function, you're *calling* it. Recursively calling `main` is legal, but generally ill-advised. You haven't told us what went wrong; you need to include that information in your question. Did you get a compile-time error message? Did the program produce incorrect output? And your goal would almost certainly be better met by a loop than by a recursive call. – Keith Thompson Jun 12 '17 at 19:03
  • 2
    Your immediate problem (which I'm sure has been addressed elsewhere) is that in response to `scanf("%d", ...)` you typed a number and then Enter. The number was consumed by the `scanf` call, leaving the Enter (`'\n'`) to be consumed by the following `scanf("%c", ...)`. If you want to read a number from stdin, you'll need to read the number and then discard any following input up to and including the next `'\n'`. – Keith Thompson Jun 12 '17 at 19:06
  • 1
    Feedback? 1) *always* test the return value from `scanf` function family - the number of items successfuly scanned. 2) `scanf("%c", &answer);` will catch the newline which teminated `scanf("%d", &passlength);`. I suggest reading [`scanf()` leaves the new line char in buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-buffer). – Weather Vane Jun 12 '17 at 19:14

2 Answers2

2

I'd recommend refactoring your function to not use recursion.

With optimizer on full blast this tail call should be easy to optimize out, but you shouldn't be depending on the optimizer. Use a loop instead.

Try something like:

do
{
    // your code
} while(answer == 'Y');

return ERROR_SUCCESS;
Cory Nelson
  • 29,236
  • 5
  • 72
  • 110
1

Contrary to what is said in comments, yes, your main() can be recursive.

As mentionned by @Keith Thompson, you must flush the '\n' after reading your passlength. For example:

  scanf("%d", &passlength);
  fgetc(stdin);

Then

  scanf("%c", &answer);
  fgetc(stdin);
RaphaMex
  • 2,781
  • 1
  • 14
  • 30