Thread: Newbie: Some help with ASCII

  1. #16
    Registered User
    Join Date
    Apr 2005
    Posts
    19
    Oh one thing, sorry about the thread name. Im working on two encryptions and forgot that i had written ASCII problem... got confused...

    I dont think i want to start changing the arrasy to strings. I dont know how to use strings like that and im pressed for time. And besides the encryption and decryption works the way its supposed to so, i got it working the way i wanted to! Well... except for the pause thing...

    I dont want to open it in using cmd, i want to be able to doubleclick the program. I read FAQ > How do I... (Level 1) > How do I get my program to wait for a keypress?, linked from the one joshdick gave me
    and the reason i wanted to use cin.get is that its the C++ way! No other reason... But still, i dont know how to get it working.

    Just for the sake of it (and becasue its alot smaler), here is the new code:
    Code:
    #include <iostream>
    #include <conio.h>
    
    using namespace std;
    
    char key[13]="ABCDEFGHIJKL";
    
    int main()
    {
        char input[13];
        cin.getline(input, 13);
    
        for(int x=0; x<12; x++)
        {
            input[x] = input[x]^key[x];
            cout << input[x];
        }
        cout << endl;
      
        for (int x=0; x<12; x++)
        {
            input[x] = input[x]^key[x];
            cout << input[x];
        }
      cout << endl << endl;
      
    /*cin.ignore();
      cout << "Press Enter" << endl;
      cout << endl;
      cin.get();*/
      getch();
      return(0);
    }

  2. #17
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Did you try cin.get() again? As I explained, your problem was that some of your input reamained in the stream, and when there is input remaining in the stream, cin.get() does not have to wait for input.

    If you are still having problems, this should work in most cases:
    Code:
    cin.ignore(2000, '\n');
    cin.get();
    The first line skips any characters remaining on the line. Then cin.get() will hang waiting for input.
    Last edited by 7stud; 05-03-2005 at 02:29 PM.

  3. #18
    Registered User
    Join Date
    Apr 2005
    Posts
    19
    Alrighty then ive made some changes. Fist of all i descided to use strings instead of arrays after all. Alot easier once i got the hang of it. cin.get works now too since there will never bee excess input, still in the stream. Also, it now handles a file instead of a direct user input. So you have to make a file for the program to work.

    The problem now is that the encryption doesnt work properly. I made a file containing the text:

    Exclusive-OR (XOR).
    Encryption and decryption.

    The problem is that the probgram doesnt seem to encrypt all the character. I suppose it has something to do with the key, but how do i work around or fix it?

    The program code:
    Code:
    #include <iostream>
    #include <conio.h>
    #include <string>
    #include <fstream>
    using namespace std;
    
    char key[14]="ABCDEFGHIJKLM";
    
    int main()
    {
        char ch;
        
        ifstream in("c:\\XOR.txt");
        string input;
    
        while(1) 
        {
            char ch = in.get();
            if(ch == EOF)
                break;
            input.push_back(ch);
        }
    
        cout << endl << "Encypted: \n";
        for(int x=0; x<input.size(); x++)
        {
            input[x] = input[x]^key[x];
            cout << input[x];
        }
        cout << endl;
    
        cout << endl << "Decrypted: \n";  
        for (int x=0; x<input.size(); x++)
        {
            input[x] = input[x]^key[x];
            cout << input[x];
        }
        cout << endl << endl;
    
        
        cout << "Press Enter" << endl;
        cout << endl;
        cin.get();
        return(0);
    }

  4. #19
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Wow.. this is very similar to a program I wrote not too long ago..

    http://cboard.cprogramming.com/showt...t=60243&page=1
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  5. #20
    Registered User
    Join Date
    Apr 2005
    Posts
    19
    Thats all well and good but it doesnt realy help me... or am i missing something?

  6. #21
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > input[x] = input[x]^key[x];
    Try:
    input[x] = input[x]^key[ x%sizeof(key) ];

  7. #22
    Registered User
    Join Date
    Apr 2005
    Posts
    19
    Sweet! Thanks Swoopy! It worked like a charm! Seems like im finaly nearing the end of this little program! Now i just have to fine tune it... i hope

  8. #23
    Resident nerd elnerdo's Avatar
    Join Date
    Apr 2005
    Location
    Northern NJ
    Posts
    51
    Do you really need to make the caeser cipher roll back?

    (a = d, z = c)

    Why not simply let it go so you end up with some crazy cool symbols instead of letters, those are usually a lot harder to crack anyway.

    Also, here's an interesting trick that could help you with encryption.


    'A'(ascii char 65) XOR 50 = 115
    115 XOR 50 = 'a'

    (This works with all letters..)
    'b' XOR 435 = x
    x XOR 435 = 'b'
    etc..
    nerds unite!

    I'm using windows XP.
    I'm using dev-C++ by bloodshed.

  9. #24
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Code:
    Thats all well and good but it doesnt realy help me... or am i missing something?
    I'd suggest you read the Brain's post again. It is very similar to what you are doing. You might even get a few ideas. Of course the encryption he has written is easy to crack nevertheless, it demonstrates encryption methods closer to the real world- something you should think about if you intend to take this project further.


  10. #25
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    1)
    The problem is that the probgram doesnt seem to encrypt all the character.
    Does your program even compile? Try running this simple program:

    Code:
    string input = "test";
    input.push_back('X');
    cout<<input<<endl;
    With strings you can simply add characters with the operator+ or operator+=.

    2)It doesn't make any sense to read in a file character by character and then just store each character in a string. Reading from a file is expensive, and you could read in the data a whole line at a time instead:

    getline(in, input);

    (I'm not sure what the best method would be for reading in a whole file at one time.)

    3)
    Code:
    while(1) 
    {
    	char ch = in.get();
    	if(ch == EOF)
    		break;
    	input.push_back(ch);
    }
    You should never declare variables in a loop because that means you redeclare the variable every time through the loop.
    Last edited by 7stud; 05-05-2005 at 04:47 AM.

  11. #26
    Registered User
    Join Date
    Apr 2005
    Posts
    19
    Well like i said earlier i am pressed for time so for now im more or less gonna go with what i have. Once i have turned in the program and the report at school im probably going to look into the other stuff. The char ch declaration is noted and changed however.

    The problem i have now is that i want the user to be able to choose the file to be encrypted and decrypted. I thaught i could just go ahead and say
    Code:
     cin >> file_name;
    ifstream enc_file (file_name);
    but that doesnt seem to be possible, not just like that anyway. Any ideas on how to go about that?

  12. #27
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >ifstream enc_file (file_name);
    Well, if it's declared as a string, you can just:
    ifstream enc_file (file_name.c_str());

    You should never declare variables in a loop because that means you redeclare the variable every time through the loop.
    I would be surprised if that's true. I think it's just a scope issue. The compiler probably only declares it one time, the first time it encounters the declaration.

  13. #28
    Registered User
    Join Date
    Apr 2005
    Posts
    19
    Hmm i dont understand... so i write like
    string file_name
    ifstream enc_file (file_name.c_str())
    ...?

  14. #29
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    If filename is a char array, you can simply:
    Code:
    ifstream enc_file (file_name);
    But if it's a string, you have to pass the char array portion of it, which you do by calling c_str(), so yes:
    Code:
    ifstream enc_file (file_name.c_str());

  15. #30
    Registered User
    Join Date
    Apr 2005
    Posts
    19
    Code:
    cin >> file_name;
        ifstream in(file_name.c_str());
    im supposed to write more than that right? Cause tat doesnt work...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  2. Office access in C/C++ NOT VC++!! :)
    By skawky in forum C++ Programming
    Replies: 1
    Last Post: 05-26-2005, 01:43 PM
  3. TEXT FILE TO ASCII - newbie
    By skwattakamp in forum C Programming
    Replies: 18
    Last Post: 12-23-2004, 10:01 AM
  4. ascii values for keys
    By acid45 in forum C Programming
    Replies: 2
    Last Post: 05-12-2003, 07:13 AM
  5. Checking ascii values of char input
    By yank in forum C Programming
    Replies: 2
    Last Post: 04-29-2003, 07:49 AM