Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,565 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,883 people online right now. Registration is fast and FREE... Join Now!




Random String of Characters

 
Reply to this topicStart new topic

Random String of Characters

sno
6 May, 2008 - 06:12 AM
Post #1

New D.I.C Head
*

Joined: 4 May, 2008
Posts: 17


My Contributions
I was wondering why this wasnt giving me a string but what i think is hex?

CODE


#include <iostream>
#include <string.h>
#include <time.h>

using namespace std;

int main(){
    string message, encrypted[10]; //wont compile unless i keep that old message variable
    int i;

    srand( (unsigned int) time(NULL));

    for (i=0; i<=10;i++){
        encrypted[i] = (char) (rand() % ('z' - '0' + 1) + '0');
    }
    cout << encrypted;

    return 0;
}




whenever i run this it just gives me 0x22ff20
User is offlineProfile CardPM
+Quote Post

KYA
RE: Random String Of Characters
6 May, 2008 - 06:23 AM
Post #2

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 4,924



Thanked: 105 times
Dream Kudos: 1200
My Contributions
When I compile and run this I get an assortment of letter, shapes, etc... Is this what is intended? What type of random string are you looking for? :

cpp

#include <iostream>
#include <string.h>
#include <time.h>

using namespace std;

int main(){
char encrypted[10];

srand( (unsigned int) time(NULL));

for (int i=0; i<10;i++){
encrypted[i] = (char) (rand() % ('z' - '0' + 1) + '0');
}
cout << encrypted << endl;;

return 0;
}

User is offlineProfile CardPM
+Quote Post

sno
RE: Random String Of Characters
6 May, 2008 - 06:30 AM
Post #3

New D.I.C Head
*

Joined: 4 May, 2008
Posts: 17


My Contributions
yes. thats what im looking for but my compile apparently doesnt like it. im using minigw, any ideas how to get the same output?

edit: oops you changed stuff haha, i thought you just put my code back up. Thanks its works now. What is the difference between the char[] nad string[]?

This post has been edited by sno: 6 May, 2008 - 06:34 AM
User is offlineProfile CardPM
+Quote Post

KYA
RE: Random String Of Characters
6 May, 2008 - 06:31 AM
Post #4

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 4,924



Thanked: 105 times
Dream Kudos: 1200
My Contributions
Might be a computer specific thing. Although we could try ASCII code to get a standard.

Did you try an array of chars as in my code posting?

Try this: EDIT:

cpp

encrypted[i] = (char) (rand() % ('z' - 'c' + 1) + 'a');


Using only letters instead of zero gives only letters as output


EDIT AGAIN: In your original posting you were also writing past one value in your array and that wil cause problems.

This post has been edited by KYA: 6 May, 2008 - 06:34 AM
User is offlineProfile CardPM
+Quote Post

sno
RE: Random String Of Characters
6 May, 2008 - 07:14 AM
Post #5

New D.I.C Head
*

Joined: 4 May, 2008
Posts: 17


My Contributions
my compiler is complaining about

CODE

    for (int i=0; i<10;i++){
        encrypted[i] = message[i];
    }


its saying i cant have that for array subscripts?
User is offlineProfile CardPM
+Quote Post

KYA
RE: Random String Of Characters
6 May, 2008 - 10:08 PM
Post #6

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 4,924



Thanked: 105 times
Dream Kudos: 1200
My Contributions
You never declared a message array in the original code
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 11:47PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month