Need advice

Program for coding/decoding text from file. Problem is with decoding last symbol. First is good. Have no ideas where is wrong. Pehaps you do. Thanx a lot. :s
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
int decoding()       
{
    ifstream read; 
    ofstream write;   

//  opening input file.    
    cout << "\nReading code.txt. "; 
    read.open ("code.txt", ifstream::in);
    
    if (!read)   
    {
             cout << "read code.txt fail!" << endl;
             system ("PAUSE");
             return 1;
    
    }
    cout << "\nEncoded txt to answer.txt file. ";
    write.open ("answer.txt", ofstream::out);
    if (!write) 
    {
              cout << "Write answer.txt fail!" << endl;
              system ("PAUSE");
              read.close();
              return 1;
    }

    while (!read.eof())
    {
              char str[1024], s;
              int  n, i;
              cout << "\n\nCoded txt: " <<endl;    
              read.getline (str, 1024);             
              cout << str<< endl;
              n=strlen(str)+1;
      
              if (n >= 0)        
              {
              for (i=1;i<n;i+=2)
              {
              s=str[i];
              str[i]=str[i+1];
              str[i+1]=s;
              }
                     for (i=0;i<=n;i++)
                         {
                            switch(str[i])
                            {
                            case '^':
                                 str[i]='a';
                            break;
                            case '*':
                                 str[i]='s';
                            break;
                            case '~':
                                  str[i]='k';
                            break;
      		                case '#':
                                 str[i]='i';
                            break;
      		                case '|':
                                 str[i]='o';
                            break;
      		                case '`':
                                 str[i]='m';
                            break;
                            }

                          }
              cout<<"\nEncoded: \n"<<str<<endl;
              write << str<< endl;
              } //if end
    }//while end
              
    write.close();   
    read.close();
    cout << "encoded!" << endl;
    return 0;
}
Last edited on
Here's your code formatted nicely. I believe the error is also fixed.

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const char* FileIn  = "code.txt";
const char* FileOut = "answer.txt";

int decoding()
{
    ifstream read (FileIn);
    if (!read) {
        cerr << "Read " << FileIn << " fail!\n";
        return 1;
    }

    ofstream write (FileOut);
    if (!write) {
        read.close();
        cerr << "Write " << FileOut << " fail!\n";
        return 1;
    }

    string str;
    while (getline (read, str))
    {
        if (str.length() == 0) continue;

        cout << "\nCoded txt: " << str << endl;

        for (int i = 1; i < str.length(); i += 2)
        {
            char s = str[i];
            str[i] = str[i-1];
            str[i-1] = s;
        }

        for (int i = 0; i < str.length(); i++)
        {
            switch (str[i]) {
            case '^':  str[i] = 'a';  break;
            case '*':  str[i] = 's';  break;
            case '~':  str[i] = 'k';  break;
            case '#':  str[i] = 'i';  break;
            case '|':  str[i] = 'o';  break;
            case '`':  str[i] = 'm';  break;
            }

        }

        cout << "Decoded: " << str << endl;
        write << str << endl;
    }

    write.close();
    read.close();
    return 0;
}

really look nice. Thanx. I'll try it combine with program.

So. tried it. Its bugged - on writting to "answer.txt" program die...
Visible is reading coded text and no information about encoding :) so, I din't know decoding or not or still did not write :) can You explain why program die?
This error report includes: information regarding the condition of *.exe when the problem occured; the operating system version and computer hardware in use; your Digital Product ID, which could be used to identify your license; and the Internet Protocol (IP) address of your computer.
Last edited on
This happened when I put code for changing first and the last characters in place:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
if (str.length() == 0) continue;
        cout << "\nCoded txt: " << str << endl;
        {
              char s = str[0];      //|\ first become s
              str[0] = str[n];      //|- last become first
              str[n] = s;           //|/ value s become last


        for (int i = 1; i < str.length(); i += 2)
        {
            char s = str[i];
            str[i] = str[i-1];
            str[i-1] = s;
        }
Last edited on
I think problem is because end line symbol is not removing... and couting. Advice, pls, how to fix it? thanx.

str.length() I do not use, because it every time couting lenght of string. Its not nessesary for that is int n.
Last edited on
Mines not advanced but it's bigger and bulker
I just couldn't put in loop, tried but didn't work out
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
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <fstream>
using namespace std;

#include "supply.h"

int main()
{
	ofstream file1("Decode.txt");
	// Make a new file, test if ok
	if (!file1)
	{
		cout << "Error " << "file, ";
		cout << "Can not open file." << "\n";
		return (1);
	}
	// write the content to disk and close the file
	cout << "Type in the content you wish to write in file\n";
	char buffer[255];
	cin.getline( buffer, 255);
	file1 << buffer;
	file1.close();

	// read the file, edit letters in decode function
	char ch;
	cout << "\nReading the content of file...\n";
	ifstream code("Decode.txt");
	ofstream input("Encode.txt");
	
	while (code.get(ch) ) {
		decode(ch);			// editing letter

		input << ch;
		cout << ch;
	}
	cout << "\nEncode complete\n";
	code.close();
	input.close();
	
	system("Pause");
	return 0;
}

[supply.h]

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// My functions --
char decode( char & character)
{
	// first four characters opposite to the next four
	// check for matches
	if (character == 'a')
		return character = 'e';
	if (character == 'b')
		return character = 'f';
	if (character == 'c')
		return character = 'g';
	if (character == 'd')
		return character = 'h';

	if (character == 'e')
		return character = 'a';
	if (character == 'f')
		return character = 'b';
	if (character == 'g')
		return character = 'c';
	if (character == 'h')
		return character = 'd';
		// first
	if (character == 'i')
		return character = 'm';
	if (character == 'j')
		return character = 'n';
	if (character == 'k')
		return character = 'o';
	if (character == 'l')
		return character = 'p';

	if (character == 'm')
		return character = 'i';
	if (character == 'n')
		return character = 'j';
	if (character == 'o')
		return character = 'k';
	if (character == 'p')
		return character = 'l';
		// sceond
	if (character == 'q')
		return character = 'u';
	if (character == 'r')
		return character = 'v';
	if (character == 's')
		return character = 'w';
	if (character == 't')
		return character = 'x';
		// third
	if (character == 'u')
		return character = 'q';
	if (character == 'v')
		return character = 'r';
	if (character == 'w')
		return character = 's';
	if (character == 'x')
		return character = 't';

	// last are special
	if (character == 'y')
		return character = '-';
	if (character == 'z')
		return character = '+';
	
	if (character == '-')
		return character = 'y';
	if (character == '+')
		return character = 'z';

	return character = '#';		// default value 
}

If i did something wrong, tell me.
Last edited on
Topic archived. No new replies allowed.