implementing rot13

Hi,
I have to write a programm that reads a text from one .txt translates it using rot13 and writes the new text into another .txt file
i have to use rot13 this way:
char rot13 [] = { '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',
};


My code so far

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;


int main() {
ifstream file;    
file.open("klar.txt");  
 ofstream outfile;           
 outfile.open("geheim.txt");     

char c ;
char text;   


while( ( c = file.get(text) ) != EOF )  
{

	outfile<<c;    
}

file.close();  
outfile.close();



return 0;
}
Last edited on
Line 13: You're simply writing the character you read. You're not translating it.

You need the rot13 array from your problem description included in your program.
Then you can change line 22 as follows:
1
2
 
  outfile << rot13[c -'A'];  // Convert char read to a zero based subscript. 


Some things to keep in mind.:
- What if the character read is not 'A'-'Z'? ie. punctuation, numeric or lower case.
- What if the open of your file fails at line 11?
Last edited on
I now have an error in line 19
and the output is wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww for some reason
NVM its now in chinese
I now have an error in line 19

What error?

There are multiple forms of istream::get. It looks like you're trying to mix two forms.
http://www.cplusplus.com/reference/istream/istream/get/
1
2
3
4
  //  form 1
  c = file.get();  // Return next char from file.  Note:  No argument
  //  form2
  if (file.get (&c))  // pointer to char passed as argument.  Returns reference to stream 

There is no form that takes a char by value and also returns a char as you have done.

and the output is wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww

Show your current code.
What input are you using?

If you're getting an error on line 19, how are you getting it to even run?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  #include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>


using namespace std;

int main()
{
    ifstream ifs;
    ifs.open("klar.txt");
    ofstream ofs;
    ofs.open("geheim.txt");
    char c;
    char rot13 [] = {'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',
                    };

    while( ( c = ifs.get() )!= EOF )
    {
    ofs<<rot13[c - 'A'];
    }
    ifs.close();
    ofs.close();

    return 0;
}


i changed the code a bit there is no more error in line 19 but the output is in chinese for some reason
Input:DIES IST EIN GEHEIMTEXT MIT 39 ZEICHEN!
(its german if you wonder)
Output:噑䙒噰䝆剰䅖呰啒噒䝚䭒灇噚灇䵰噒啐䅒þ



would anyone mind trying the code on his Programm of choice?
As I pointed out in my first post, you're going to have problems if your input is anything other than 'A'-'Z'. Given the input in your previous post, you're going to have undefined behavior (out of bounds reference) because of the spaces and numerics in your message.

Space (32), 3 (51) and 9 (57) all result in a negative index to the rot13 array. I'm surprised your program didn't crash.
Topic archived. No new replies allowed.