Stupid C++ question...

Put your programming skills to the test in these challenges.

Stupid C++ question...

Post by PaCkEtPiRaTe on Tue Aug 19, 2008 9:32 pm
([msg=10086]see Stupid C++ question...[/msg])

I'm trying to code a random password generator in C++.
I don't understand what I'm doing wrong... keep in mind I'm still a noob...
It's supposed to take a random character from the characters charset and print it, doing this 20 times.

Here's the code.

Code: Select all

// Random Password Generator by -------------------.
// Copyright © 2008-2013. All rights reserved.
#include <iostream>
#include <stdlib.h>
using namespace std;

int main() {
    srand(time(NULL));
    int pass_Length = 0;
    char password [20];
    char characters [] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '\0' };

   
    while (pass_Length.length < 20) {
          cout << characters[rand()%characters.length;] << endl;
          pass_Length++;
          }
    system("PAUSE");
    return 0;
}



I'd also appreciate learning resources if anyone has any good ones to offer.
PaCkEtPiRaTe
New User
New User
 
Posts: 9
Joined: Tue Aug 19, 2008 9:30 pm
Blog: View Blog (0)


Re: Stupid C++ question...

Post by AbaxHacker on Tue Aug 19, 2008 11:47 pm
([msg=10097]see Re: Stupid C++ question...[/msg])

Can't help bit notice no ctime include.

I tried, re making your code in Vista and for some reason the last char is a group of weird symbols, I suggest that you, do rand() with a +1 at the end, thats how I know random all i can say is use a for loop.
AbaxHacker
New User
New User
 
Posts: 17
Joined: Tue Aug 19, 2008 6:31 pm
Blog: View Blog (0)


Re: Stupid C++ question...

Post by rlo on Wed Aug 20, 2008 2:29 am
([msg=10104]see Re: Stupid C++ question...[/msg])

Code: Select all
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;

int main()
{
    srand(time(NULL));
    int pass_Length = 0;
    char characters [] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '\0' };
   int characters_len = sizeof(characters) / sizeof(char) ;   
   while (pass_Length < 20)
   {
          cout << characters[rand()%characters_len] << endl;
          pass_Length++;
    }
//   system("PAUSE");
    return 0;
}
rlo
New User
New User
 
Posts: 1
Joined: Wed Aug 20, 2008 2:17 am
Blog: View Blog (0)


Re: Stupid C++ question...

Post by AbaxHacker on Wed Aug 20, 2008 2:57 pm
([msg=10134]see Re: Stupid C++ question...[/msg])

I haven't check mainly in my compiler, what seems to be the problem, I guess i could re-write or make one.
AbaxHacker
New User
New User
 
Posts: 17
Joined: Tue Aug 19, 2008 6:31 pm
Blog: View Blog (0)


Re: Stupid C++ question...

Post by PaCkEtPiRaTe on Wed Aug 20, 2008 3:53 pm
([msg=10136]see Re: Stupid C++ question...[/msg])

New question around the same program...
Now I want to add the result to a text file... I can already do the File I/O but what I don't get is how can I put all 20 random characters into a string so that I could insert that string into the text file?
PaCkEtPiRaTe
New User
New User
 
Posts: 9
Joined: Tue Aug 19, 2008 9:30 pm
Blog: View Blog (0)


Re: Stupid C++ question...

Post by AbaxHacker on Wed Aug 20, 2008 5:55 pm
([msg=10141]see Re: Stupid C++ question...[/msg])

Well strings are very cool because you can them like an int for example:

int1+int2=int3
string1+string2=string3

Or It adds after so:

Try adding the character to the end of the string or basically instead of cout:

cout << characters[rand()%characters_len] << endl;
to:
string password += characters[rand()%characters_len];

Then write that to a file like: passwordlog.txt with current time so:
Time: Password
AbaxHacker
New User
New User
 
Posts: 17
Joined: Tue Aug 19, 2008 6:31 pm
Blog: View Blog (0)


Re: Stupid C++ question...

Post by PaCkEtPiRaTe on Wed Aug 20, 2008 6:14 pm
([msg=10144]see Re: Stupid C++ question...[/msg])

It's not working... here's the code now...

Code: Select all

// Random Password Generator by ----------------------.
// Copyright © 2008-2013. All rights reserved.
#include <iostream>
#include <fstream.h>
#include <stdlib.h>
using namespace std;

int main() {
    srand(time(NULL));
    int pass_Length = 0;
    char characters [] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '\0' };
    int characters_len = sizeof(characters) / sizeof(char);
    string password;
   
    while (pass_Length < 20) {
          string password += characters[rand()%characters_len];
          pass_Length++;
          }
    ofstream fout("password.txt");
    fout << "The password generated was: " << password << "\n";
    fout << flush;
    fout.close();
    cout << endl;
    system("PAUSE");
    return 0;
}



And the compiler is giving me these errors...

Compiler: Default compiler
Executing g++.exe...
g++.exe "C:\Users\PaCkEtPiRaTe\Documents\password_generator.cpp" -o "C:\Users\PaCkEtPiRaTe\Documents\password_generator.exe" -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Dev-Cpp\include\c++\3.4.2\backward" -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C:\Dev-Cpp\include\c++\3.4.2" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib"
In file included from C:/Dev-Cpp/include/c++/3.4.2/backward/fstream.h:31,
from C:\Users\PaCkEtPiRaTe\Documents\password_generator.cpp:4:
C:/Dev-Cpp/include/c++/3.4.2/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated.
C:\Users\PaCkEtPiRaTe\Documents\password_generator.cpp: In function `int main()':
C:\Users\PaCkEtPiRaTe\Documents\password_generator.cpp:16: error: expected primary-expression before "password"
C:\Users\PaCkEtPiRaTe\Documents\password_generator.cpp:16: error: expected `;' before "password"

Execution terminated

It will run, but it won't write the file.

Edit: I just realized this is on the wrong board... can someone please move this to the normal Programming board? Didn't realize this was the missions programming board.
PaCkEtPiRaTe
New User
New User
 
Posts: 9
Joined: Tue Aug 19, 2008 9:30 pm
Blog: View Blog (0)


Re: Stupid C++ question...

Post by AbaxHacker on Wed Aug 20, 2008 9:10 pm
([msg=10157]see Re: Stupid C++ question...[/msg])

Can't help but your not showing a requested file operating like: read or write, go back to the tutorial or search it up on w3schools.com. Also try to add the string.h header when using strings.
AbaxHacker
New User
New User
 
Posts: 17
Joined: Tue Aug 19, 2008 6:31 pm
Blog: View Blog (0)


Re: Stupid C++ question...

Post by PaCkEtPiRaTe on Wed Aug 20, 2008 11:00 pm
([msg=10162]see Re: Stupid C++ question...[/msg])

AbaxHacker wrote:Can't help but your not showing a requested file operating like: read or write, go back to the tutorial or search it up on w3schools.com. Also try to add the string.h header when using strings.


There was nothing like that mentioned in my tutorial.

http://www.gamedev.net/reference/articl ... le1127.asp

Besides, I tried to create a file in the same way in another project and it worked.
PaCkEtPiRaTe
New User
New User
 
Posts: 9
Joined: Tue Aug 19, 2008 9:30 pm
Blog: View Blog (0)


Re: Stupid C++ question...

Post by AbaxHacker on Thu Aug 21, 2008 2:01 am
([msg=10168]see Re: Stupid C++ question...[/msg])

You either were using a clr console and it didn't work or a windows, vice versa or you had no pre-compiled header this time.
AbaxHacker
New User
New User
 
Posts: 17
Joined: Tue Aug 19, 2008 6:31 pm
Blog: View Blog (0)


Next

Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests

cron