Thread: generate a random password

  1. #16
    C++ beginner
    Join Date
    Jun 2004
    Posts
    66
    Right, I haven't been working long and I've come up with this so far, no compile errors - just logic problems.
    Code:
    // 48 - 122
    
    #include <cstdlib>
    #include <ctime>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        srand(time(NULL));
        int character = 0;
        
        
        for(int i = 1; i >= 8; i++){
                character = rand() % 75 + 48;
                
                char letter= 0;
               
                //checks to see if our random character is a number or letter
                
                if(character >= 48 && character <= 57){ 
                             letter = static_cast<char>(character);
                              cout << letter;
                              }
               
                if(character >= 65 && character <= 90){
                                letter = static_cast<char>(character);
                                 cout << letter;
                                 }
                if(character >= 97 && character <= 122){          
                                           letter = static_cast<char>(character);
                                           cout << letter;
                                           }        
                                          
                 else{
                    continue; // should continue doing the loop...
                     }
                     
               } //end of loop        
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    It goes straight to the system call and exits. I'm not sure the random number is woking correctly, but I'm not sure -- I've tried outputting the value of 'character' after the random value is asigned to it - but nothing gets put on the screen. Any ideas?

    Also, how is the program's logic - the if statements I mean. Is that set up to do what it should? Am I using the continue statement correctly - so I can ignore bad numbers and continuegetting the good ones?
    Oh my goodness.

  2. #17
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    for(int i = 1; i >= 8; i++){
    1 is never greater than or equal to 8, so the loop is not entered.
    Last edited by Dave_Sinkula; 02-23-2006 at 03:11 PM. Reason: I must have broken fingers.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #18
    C++ beginner
    Join Date
    Jun 2004
    Posts
    66
    Quote Originally Posted by Dave_Sinkula
    Code:
    for(int i = 1; i >= 8; i++){
    1 is never greater than or equal to 8, so the loop is not entered.
    Boy do I feel silly! Thanks!
    Oh my goodness.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 05:51 PM
  2. Generate random numbers in Lucky7 project using C#
    By Grayson_Peddie in forum C# Programming
    Replies: 1
    Last Post: 04-11-2003, 11:03 PM
  3. auto generate password
    By Coconut in forum C Programming
    Replies: 4
    Last Post: 09-29-2002, 03:55 AM
  4. Random Password Generator v1.0 - download it
    By GaPe in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 08-18-2002, 01:27 AM
  5. Best way to generate a random double?
    By The V. in forum C Programming
    Replies: 3
    Last Post: 10-16-2001, 04:11 PM