Ceaser Cipher

This is a discussion on Ceaser Cipher within the C++ Programming forums, part of the General Programming Boards category; I have to write a program using the Ceaser Cipher, I think I have a way to find the letters, ...

  1. #1
    The Pantless Man CheesyMoo's Avatar
    Join Date
    Jan 2003
    Posts
    262

    Ceaser Cipher

    I have to write a program using the Ceaser Cipher, I think I have a way to find the letters, but the problem is, I don't know how to open up a .dat file to use in the program. I have to open the file decode it and then save it in a new file... I haven't any idea on how to do this, any help would be apreciated with including files or for the code.

  2. #2
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    Sorry I can't help with the reading the file. But isn't the Ceasor cipher something like X += 3? Saving to a new file is no problem.

    Code:
    ofstream fout("new.txt");      //create a new file and assign fout as its 'alias'
    fout << "This is on line one\n";    //writes to the file, same as the next line
    fout << "This is on line two";
    fout.close();    //not nessicary but very good practice to close the file after reading/writing in it.

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    opening a file can be done as so:

    Code:
    ifstream infile("file.dat");
    then you can do what you want, ex. read letters into an array, etc.

    edit: with ifstream and ofstream, you want to #include fstream.

  4. #4
    The Pantless Man CheesyMoo's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Here's my code, instead of getting stuff from the .dat file it makes the user input, I am confused and it never decodes correctly it simply prints a black rectangle.


    Code:
    // Written by Phillip Hermans
    // Ceasar Cipher
    // Feburary 13, 2003
    
    #include <iostream.h>
    #include <fstream.h>
    
    int main()
    	{
    	char coded[13];
    	int decode[13],i;
    
    	ifstream infile("ceas.dat"); // Opens the file with the coded text
    	cin.getline(coded,13);
    
    		for(i = 0; i < 13; i++)
    		{
    		 decode[i] = coded[i];
    		}
    
    		for(i = 0; i < 13; i++)
    		{
    		 decode[i] += 3;
    
    		 if(decode[i] > 90)
    			{
    			decode[i] = (decode[i] - 90) + 65;
    			}
    		 }
    
    	ofstream fout("unco.dat"); // Opens a new file with the decoded text
    	fout << char(decode);
    	fout.close();
    
    	cout << char(decode);
    
    	return 0;
    
    	}

  5. #5
    The Pantless Man CheesyMoo's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Well does anybody know? I'm sorry for being annoying but I really need help!

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    This
    cout << char(decode);
    only prints one character to the screen.

    The decode array should be of type char, not int. Then you can output it like:
    cout <<decode;

    This bit of code has done bad things with your data:
    >>decode[ i ] += 3;
    It adds 3 to any character in the array, including the nul terminator (meaning its no longer a usable string).
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    The Pantless Man CheesyMoo's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Can you add a value to a string such as 3? I didn't know that it would work...

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >Can you add a value to a string such as 3?
    Not sure exactly what you mean, but here goes... you can do arithmatic on a char just like you can an int. Remember though that it will hold a much smaller value, but in your program that shouldn't be a problem.

    If the user enters a 3, that'll be stored as 0x33 (assuming ASCII). So, if the first byte in the array held this value, and you did this:
    decode[0] += 3;
    its value would be upped to 0x37, which is a character 7.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    The Pantless Man CheesyMoo's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Okay I updated my code, the only problem is, it doesn't read from the file it decides to make the user input data, and then the input is completely messed up and is not usefull.

  10. #10
    The Pantless Man CheesyMoo's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Code:
    // Written by Phillip Hermans
    // Ceasar Cipher
    // Feburary 13, 2003
    
    #include <iostream.h>
    #include <fstream.h>
    
    int main()
    	{
    	char coded[13];
    	char decode[13],i;
    
    	ifstream infile("ceas.dat"); // Opens the file with the coded text
    	cin.getline(coded,13);
    
    
    
    		for(i = 0; i < 12; i++)
    		{
    		 coded[i] += 3;
    
    		 if(coded[i] > 90)
    			{
    			coded[i] = (coded[i] - 90) + 65;
    			}
    		 }
    
    	ofstream fout("unco.dat"); // Opens a new file with the decoded text
    	fout << coded;
    	fout.close();
    
    	cout << decode;
    
    	return 0;
    
    	}
    Also do you think I space my code well?

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    cin.getline() get from standard input. To input from the file, use infile.getline(coded, 13);

    The variable i should really be an int, in your latest attempt you've made it a char.

    You should check that the file open worked successfully too, before trying to input from it. Something like:
    Code:
      ifstream  infile("ceas.dat"); // Opens the file with the coded text
      
      if (!infile)
      {
        perror("ceas.dat");
        return (0);
      }
    As for the layout, personally I'd lay your code out like this, but it's only my preference.
    Code:
    // Written by Phillip Hermans
    // Ceasar Cipher
    // Feburary 13, 2003
    #include <iostream.h>
    #include <fstream.h>
    
    int main(void)
    {
      char      coded[13];
      char      decode[13];
      int       i;
    
      ifstream  infile("ceas.dat"); // Opens the file with the coded text
    
      if (!infile)
      {
        perror("ceas.dat");
        return(0);
      }
    
      infile.getline(coded, 13);
    
      for (i = 0; i < 12; i++)
      {
        coded[i] += 3;
    
        if (coded[i] > 90)
        {
          coded[i] = (coded[i] - 90) + 65;
        }
      }
    
      ofstream  fout("unco.dat");   // Opens a new file with the decoded text
      fout << coded;
      fout.close();
    
      cout << decode;
    
      return(0);
    }
    hmm... you still don't seem to be checking for 0x00 when you do the +3 bit?!

    Let me get this straight, you have a file that contains encoded text, you want to open it, decode the text and write that out to another file. Right?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  12. #12
    looking for the truth moemen ahmed's Avatar
    Join Date
    Feb 2002
    Location
    Egypt
    Posts
    161

    Smile

    CheesyMoo,
    in fact im not sure about what are you exactly trying to do , but i think this code will help you
    my code uses different way for treating with files,depending on"stdio.h"

    IMPORTANT: this code needs some additions to be neat , just like examine the file pointer doesnt equal NULL or its unavailable to open the file,....

    hope it helps

    Code:
    // Ceasar Cipher
    
    #include <stdio.h>
    #include <iostream.h>
    #include <conio.h>
    
    int main()
    	{
    
    	char coded[13];
        int decode[13],i;
        FILE * p;
    	p=fopen("ceas.dat","r"); // Opens the file with the coded text
    
        for (int c=0;c<13;c++)
        {
        coded[c]=getc(p);
        cout<<coded[c];
        }
    
    
    		for(i = 0; i < 13; i++)
    		{
    		 decode[i] = coded[i];
    		}
    
    		for(i = 0; i < 13; i++)
    		{
    		 decode[i] += 3;
    
    		 if(decode[i] > 90)
    			{
    			decode[i] = (decode[i] - 90) + 65;
    			}
    		 }
    
    	p=fopen("unco.dat","w"); // Opens a new file with the decoded text
        for (int cc=0;cc<13;cc++)
        {
        putc(char(decode[cc]),p);
        cout << char(decode[cc]);
        }
        getche();
    	return 0;
    
    	}
    Last edited by moemen ahmed; 02-14-2003 at 07:35 PM.
    Programming is a high logical enjoyable art for both programer and user !!

  13. #13
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >> but i think this code will help you
    And now we're confusing C with C++, and using some non-standard functions that CheesyMoo may or may not have. This might be OK for someone with a bit of experience, but for a newbie I'd suggest sticking with one or the other to start with
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  14. #14
    The Pantless Man CheesyMoo's Avatar
    Join Date
    Jan 2003
    Posts
    262
    I don't know what 0x00 means, but yes I have to read from one file then write to another, it's rather odd but that's my job.

  15. #15
    looking for the truth moemen ahmed's Avatar
    Join Date
    Feb 2002
    Location
    Egypt
    Posts
    161

    Lightbulb

    I agee with you hammer, may be it wasnt right to post this code, I know its C-style , just i wanted to help
    Programming is a high logical enjoyable art for both programer and user !!

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with ceaser cipher , and charcater counting
    By princeyeni in forum C Programming
    Replies: 2
    Last Post: 06-18-2009, 09:36 AM
  2. Simple Cipher Program
    By PunchOut in forum C Programming
    Replies: 7
    Last Post: 11-22-2008, 01:12 PM
  3. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  4. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 04:42 PM
  5. My little Caesar cipher emulator
    By dead_cell in forum C++ Programming
    Replies: 3
    Last Post: 01-16-2004, 01:05 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21