My 1st C++ App (text encryptor)

Hi every one!
I'm a C++ beginner
I wrote some apps. using pascal,VB6 but this is my first (real) app in C++ 6
It's a simple text encryptor (console application)
hope you like ;)

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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include "stdafx.h"
#include <iostream>
#include <conio.h>//for two functions: getch (Get Char) and putch (Put Char)
using namespace std;


char crypt(int i,char op,char c)
{
	int key;
	//This is my favorite way of encryption...the key is related to the position of the char ;)
	//so here every char has 4 codes...
	switch (i % 4)
	{
	case 0:
		{
			key=4;
			break;
		}
		
	case 1:
		{
			
			key=2;
			break;
		}
	case 2: 
		{
			key=3;
			break;
		}
	case 3:
		{
			key=1;
		}
	}
	
	if  ((op=='d')||(op=='D')) key=-key;
	
	if ((int(c)+key)<=255)//because of the low values of the keys this condition will never occur but keep it anyway...
		return char(int(c)+key);
	else
		return char((int(c)+key)-223);
}




void main()
{
	//How to use:
	cout<<"Input a text to encrypt/decrypt...\n"
		<<"(numbers from 0 to 9,letters a..z,A..Z)\n"
		<<"When you finish writing press Esc\n"
		<<"Please note that BackSpace is allowed only in the same line!\n"
		<<"============================================================\n";
	char c[10000];//I know it's not ideal but I had no choice since I wasn't able to add a char to a string :(
	for (long i=0;int(c[i-1])!=27 && i<=10000;i++)// 27=escape (our stop condition is pressing the Esc key)
	{
		c[i]=getch();//This function is to read a single char without displaying it on screen...
		if (int(c[i])==13) //  Enter
		{
			cout<<endl;	
		}
		else if (int(c[i])==8)// BackSpace
		{
			if (i>=1)//this section needs much more work to make it perfect...but..no time for that (exams)!
				// i>=1 means:the user has typed at least on char so we'll allow him to use BackSpace
			{
				if (int(c[i-1]!=13))// if last char isn't (Enter) we allow BackSpaces
				{
					cout<<"\b"<<' '<<"\b";   //if only I know a procedure similar to gotoxy :( ...
					i-=2;
				}
				else
				{
					cout<<"\a";// make a beep when BackSpace is not allowed (cannot move from a line to a previous one)
				}
			}
			else 
			{
				cout<<"\a"; //Sorry dear user...no chars to delete!
			}
		}
		else 
		{
			if  ((int(c[i])>=32) && int(c[i]<=255))
				
			{
				putch(c[i]); // prints a single char
			}
			else
			{
				if (int(c[i])!=27) i--;//if char is illeagle ignore it (Esc is an expetion!);
			}
		}
	}  //for
	
	cout<<endl<<endl;
	
	char q;
	cout<<"(e)ncrypt/(d)ecrypt?\n"
		<<'>';
	cin>>q; // 'd' or 'D' means decrypt..any thing else means decrypt
	cout<<endl;
	
	for (int j=0;j+1<i;j++)
		if (int(c[j])==13)
			cout<<endl;
		else
			putch(crypt( j,q,c[j])) ;// printing encrypted chars

		cout<<endl<<endl;
		getch();// just like pascal's crt ReadKey
		
		cout<<"=====================================================\n"
			<<"programmed by Hash-Hash for tutorial purposes...\n"
			<<"(Learning in progress....)\n"
			<<"Press any key to continue...";
		
		getch();
		
} // End of program... 
uhhmmm.. excuse me... can u help me do a c++ program about the chinese zodiac signs???
Alright. I assume you posted this to get our opinion, so:

1. Standard compliance
Line 3: Unlearn conio.h. It's non-standard, it's old, and different compilers implement it however they want, if at all.
Line 48: void main() is illegal. Never use it. The only legal forms of main() are int main() and int main(int argc,char **argv). Chose the former if you don't need command line arguments, choose the latter if you do.

2. Coding style.
Unlike most newbies I've seen, you seem to already have a more or less consistent style, but I have some things to nitpick.
Line 37: You should learn the precedences of operators to avoid having to use all those unnecessary parentheses. They don't hurt, but they could potentially make your expressions harder to read. The condition could be rewritten as (op=='d' || op=='D').
Avoid whenever possible (which is to say, always) for if, while, and for, having the block on the same line as the structure header. It makes code considerably harder to read. Compare
if ((op=='d')||(op=='D')) key=-key;
to
1
2
if ((op=='d')||(op=='D'))
    key=-key;

Line 39: This is less serious, but avoid unnecessary casts. The condition can be rewritten as (c+key<=255). The code compiles to exactly the same, but it's easier to read. Likewise, the return statement could be return c+key-223;
I'm a little dichotomic on the next point. On one hand, checking for impossible conditions is absurd, but on the other, a bug in the algorithm can'r be ruled out. There are things that is of course absurd to check, such as whether a%b<b, bu there are many others, particularly when it comes to user input, that isn't. You should use your judgement for each case.
Lines 106-120: You should always brace non-trivial blocks. For example, can you tell which if the else belongs to?
1
2
3
4
5
6
if ()
    for ()
        if ()
            //...
        else
            //... 
There's also a problem with your indentation. It would seem to suggest that you meant for all those lines to be inside the for, but the syntax says otherwise.
Last edited on
Thank you very much helios...
you taught me lots of things!

I used conio.h becuase I wanted to use putch and getch but as you say they're old..I'll find something better the next time

I didn't use (c+key<=255) becuase it's not allowed in pascal,VB6 so I didn't know it's legal...so C++ automatically converts chars to its ascii value!

I think I should read the Documentation of this web site to improve my skills..
thank you for your time.
Last edited on
Your coding is quite nice and neat. The only problems are the ones Helios pointed out.

I was going to do a text encription program once. I never thought to do it this way. So, good job on that note. C++ was and is my first programming language... probably not a good idea to go for one so complicated first and I think I've suffered for it. But never mind :)

Anyway, yeah, keep going.
It's not that chars are automatically converted to their ASCII values. chars are the ASCII values. Technically speaking, though, chars don't store any information about the code page they represent; it all depends on how the information is interpreted. For example, char(78) could be interpreted as 'N' in ASCII, and as '+' in EBCDIC.
chars are nothing but numbers, like everything else.
thank you chrisname!

helios wrote:
It would seem to suggest that you meant for all those lines to be inside the for, but the syntax says otherwise.

if you're talking about the second for (Line 106), this for contains only one if and its else (only one statement so no need for {} is that right?) the other lines are just to tell the user that he reached the end of the app.

It seems I need more syntax practice cuz I'm still finding some difficulities in C++...
by the way,I have a question: can we use the API functions just like in VB6??
e.g: private declare function Beep lib.... can this function be used in C++??
I know we can use cout<<"\a"; to make a beep but we can't control its frequency or duration,any ideas???
thanks...
Last edited on
Topic archived. No new replies allowed.