Thread: Help~!~

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    14

    Help~!~

    Hey guys. I'm making an encoding tool for messaging but the problem is, I don't know how to change the letters in the inputed message and I'm too lazy to look it up.... I just started it like 2 min ago and here's my code so far:


    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    int main()
    {
          int wtd;
          int en;
          int randnm;
          char messagete['500'];
          sao:
          cout<<"1- Decode   2- Encode"<<endl;
          cin>>wtd;
          if(wtd == 2) {
          cout<<"What message would you like to encode? Max: 500 letters + spaces!"<<endl;
          cin>>messagete;
          randnm = rand();
          messagete = messagete + randnm;
          cout<<"The encoded message is:  ||| " << messagete << " ||| and the secret number is " << randnm <<endl;
          } else if(wtd == 1) {
    
          }
          goto sao;
          system("PAUSE");
          return 0;
    }
    basically it generates a random number, changes the message depending on the number (for now increases each letter like it was a number), and gives the person the encrypted message and the secret number which changes it back.
    Last edited by Salem; 09-29-2004 at 01:29 AM. Reason: Use code tags for code, not php

  2. #2
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    >>I'm too lazy to look it up<<

    thats your problem...if you feel that way, at least don't state it in your posts - put some effort in what you do. I hope that no one here gives you the answer now. Good thing for you that there are members here that are much more forgiving than I am.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    14
    maybe lazy was a wrong word. busy is more of what I mean

  4. #4
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    we're all busy

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  5. #5
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Hmm.. I wont give you a complete answer yet.. but I will do this..

    1. Use standard headers.
    Code:
    #include <iostream>
    // not <iostream.h>
    
    //add
    using namespace std;
    // so you wont have to put std:: before cout/cin
    // do a search here and you'll see what it is about.
    2.
    Goto.. is a no-no..

    put that code into a function and do a loop to call it.

    Code:
    void Function ()
    {
    
        // code here
    
    }
    
    int main ()
    {
    
        char choice;
    
        while ( choice != 'q' ) {
    
            Function (); // This will do the code in "Function"        
    
            cout << "q to quit" << endl;
            cin  >> choice;
    
        }
    
        return 0;
    
    }
    3.
    Code:
    char message ['500']; 
    // no
    
    // its
    char message [500];
    I dont even think that will compile.

    4.
    you dont need stdlib in there.

    cin.get () will wait for the user to hit enter. system () commands are not really good.

    But fix up these problems then post again.

    Also read the faq section

    http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

    lots of good info.
    Last edited by Vicious; 09-28-2004 at 11:59 PM.
    What is C++?

  6. #6
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    >>Goto.. is a no-no..<<

    lol, I haven't even looked at his code...hehe. goto is powerfull if you know how to use it... gotta love directed breaks in java

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  7. #7
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Wasnt there a faq entry about goto? I looked but didnt see it.
    What is C++?

  8. #8
    Registered User
    Join Date
    Jul 2004
    Posts
    14
    ty for that info but u still haven't solven my problem. I need to change the message depending on the random number

  9. #9
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Go look at Axon's first post and take a wild flying guess why that is. If you know how to look it up, look it up. I am, as you say, too busy to help people who can't even think.

  10. #10
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Quote Originally Posted by axon
    >>Goto.. is a no-no..<<

    lol, I haven't even looked at his code...hehe. goto is powerfull if you know how to use it... gotta love directed breaks in java
    goto is powerful even if you don't know how to use it... powerful at turning your program into mush and confusing the hell out of you
    hello, internet!

  11. #11
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>ty for that info but u still haven't solven my problem.
    Wrong wording again, I suppose What you meant to say was, "ty for that info but that still doesn't help me to solve my problem."

    Your code won't work, simply because wrapping is undefined for characters and rand() will almost certainly return a value that will overflow the char datatype. One solution is to store the result of rand() + (the char) in an unsigned long, use the mod operator (%) and some arithmetic to bring it down into the -127 to 128 range (or whatever it is), and then convert back to char. And you'll need to find some way to store each random number too, and then find a way to de-'encrypt' each character. I'll leave that up to you.

    **EDIT**
    If you're so 'busy' that you can't figure out something this simple for yourself, I don't see why you even started on this program. If you don't have the time to write it, why bother? It's not like it's useful or anything anyway.
    Last edited by Hunter2; 09-29-2004 at 04:02 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  12. #12
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Ok look

    For a random number to work like you want it you have to do something like so.

    Code:
    // seed the random function
    srand ( time ( NULL ) );
    
    // use the modulous operator to get a number between 0 and MAX_NUMBER
    your_value = rand () % MAX_NUMBER;
    That is how you do a random number.

    So to scramble a message.

    Code:
    char ScrambleChar ( char the_char )
    {
    
         char value = rand () % 20;
    
         return the_char + value;
    
    }
    Something like that is probably what your looking for. Althogh if you plan on decrypting it. random numbers like this is not a good idea.

    I showed you this code so you will realize even though I have given you the answer, your program will still not work right without the above info. We arent just mean people who dont want to help. Actually we gave you more help then you asked for
    What is C++?

  13. #13
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>rand () % MAX_NUMBER
    That would give a number from 0 to MAX_NUMBER - 1

    Code:
    //Encode
    int key = rand();
    unsigned long newVal = theChar + key;
    newVal %= 256;
    char convertedChar = newVal - 128;
     
    //Decode:
    newVal = (convertedChar + 128) << 24;
    newVal -= key;
    theChar = newVal % 256;
    Last edited by Hunter2; 09-30-2004 at 12:25 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  14. #14
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Quote Originally Posted by Hunter2
    >>rand () % MAX_NUMBER
    That would give a number from 0 to MAX_NUMBER - 1

    Code:
    //Encode
    int key = rand();
    unsigned long newVal = theChar + key;
    newVal %= 256;
    char convertedChar = newVal - 128;
     
    //Decode:
    newVal = (convertedChar + 128) << 24;
    newVal -= key;
    theChar = newVal % 256;
    Yeah thats why I said "between" but then that might not have ben good wording since 0 is included.

    And I was under the assumption that he would eventually want to encode the message out to a file and then decode it back in. And in that case a random number wouldnt really work.
    What is C++?

  15. #15
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>Yeah thats why I said "between" but then that might not have ben good wording since 0 is included.
    *checks the post again* Oops

    >>And in that case a random number wouldnt really work.
    >and the secret number which changes it back.
    I imagine that means that the random number is saved to the file? Actually, I was thinking along the lines of a one-key-per-character format, where each character gets its own random number. Still, you're right - not very secure
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help~~
    By joy in forum C++ Programming
    Replies: 6
    Last Post: 11-24-2001, 07:59 AM