tried to make this encryption program but it doesn't work, I know I must have done something wrong, but I don't know how to correct it. A more effective solution to my "if dWord is higher than 118" would be nice, thx for help:)

here's the code!

``````#include <iostream.h>
#include <string.h>

using namespace std;

int main()
{
string Word;
int x;
cout << "Enter Word You want to be encrypted >> ";
cin >> Word;
string dWord[] = "Hello World!";
int a = 0;
int number;
number:
cout << "Enter a number between 0-10 >> ";
cin >> number;
if (number > 10 | number < 1) {
goto number;
}

for (int i = 0; dWord[a] > 0; ++a) {
dWord[a] = (int)dWord[a];
if ((int)dWord[a]==118) {
(int)dWord[a] = 32;
}
if ((int)dWord[a]==119) {
(int)dWord[a] = 33;
}
if ((int)dWord[a]==120) {
(int)dWord[a] = 34;
}
if ((int)dWord[a]==121) {
(int)dWord[a] = 35;
}
if ((int)dWord[a]==122) {
(int)dWord[a] = 36;
}
if ((int)dWord[a]==123) {
(int)dWord[a] = 37;
}
if ((int)dWord[a]==124) {
(int)dWord[a] = 38;
}
if ((int)dWord[a]==125) {
(int)dWord[a] = 39;
}
if ((int)dWord[a]==126) {
(int)dWord[a] = 40;
}
dWord[a] = dWord[a] + number;
cout << (char)dWord[a];

}
cin.get();
cin.get();
return 0;
}
}``````

## All 7 Replies

In for loop, you used condition "dword[a] > 0" ? What you meant by that?

You using an old compiler? inlcuding <iostream.h>
You should not use goto, it creates bad code instead use do while.

A few things: <iostream.h> and <string.h> have long been standardized to <iostream> and <string>.
Also, try to avoid goto if at all humanly possible. Look into a do/while loop for a cleaner way of doing what you did there.

Rather than going through a system like you have there consider using modular arithmetic with the % operator. So for example if you character was 123 and you had a shift of 10 you would get 123+10 = 133 % 127 (the last character in the set) and you'd arrive at +5 since we are counting zero.
To do the reverse you simply subtract the character number - (magnitude of shift) % (character number). If the value is less than 0 add 127 on to it.
Double check my math on those. There may be fancier ways of doing it but these should get the job done.

In for loop, you used condition "dword[a] > 0" ? What you meant by that?

While I don't know if it buys the OP anything it's as legal as saying a<10, it's still a condition that returns true or false.

In for loop, you used condition "dword[a] > 0" ? What you meant by that?

You using an old compiler? inlcuding <iostream.h>
You should not use goto, it creates bad code instead use do while.

Well I actually don't know, it should read

``while(dWord[a])``

but it still doesn't work.

I would just loop over your string:

``````for (int i = 0;i<dWord.length();i++)
{
//figure out shift from number and the criteria I gave you
dWord[i] +=shift;
}``````

While I don't know if it buys the OP anything it's as legal as saying a<10, it's still a condition that returns true or false.

But dword[a] > 0 will compare the ASCII value of the character at 'a' index of dword, which will always be greater than 0, right? Also, its error acc. to my compiler.

@kangarooblood-
Since you are not using 'i' anywhere in the loop why declare it?
If you want to loop till the end of string, the for loop can be like

``for(;dword[a]!='\0';++a)``

or as jonsca said.

But dword[a] > 0 will compare the ASCII value of the character at 'a' index of dword, which will always be greater than 0, right?

Also, its error acc. to my compiler.

Yes you are correct. I wasn't saying it would be an effective loop just that it was by some means "valid." I'll have to try it out. I don't think that's the approach to be taking anyway so I wasn't going to worry about it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.