I have created a program in which the user inputs a string, then the program outputs the encrypted string(+ a gap, - a gap, + a gap...etc..). So if the gap is 1, AABC becomes B(-A)CB <- but thats the problem! The problem is, is that if the user inputs a value to high or to low, it will give out garbage(because from 0-32? in the ASCII table the values are non printable)(!).
I have tried many different ways to fix it, but none of them have succeeded.
So basically I'm asking if anyone could help me fix this problem(help me to understand how to code a bucleloop(a wrap)).
Here is the code:
#include <iostream>
#include <string>
#include <cstdlib>
#include "macros.h" // a header I made for functions I use allot, here it is only used for Pause() at the end of the program
using namespace std;
char restart;
int nCode;
int main()
{
do
{
if (restart == 'Y'| restart == 'y')
system("clear"); //CLS for a windows machine
cout << "Please enter the code: ";
cin >> nCode;
srand(nCode);
int gap = ((rand() % 4) + 1);
cout << "Please enter your message(Gap=" << gap <<"):\n";
Clear(); // in macros.h
string sString;
getline(cin, sString);
bool bChose;
cout << "\nEnter 0 to decrypt.\nEnter 1 to encrypt.\n";
cin >> bChose;
cout << "\n";
if (bChose == 1)
{
for ( int i=0; sString[i] != '\0'; ++i )
{
sString[i] -= gap;
++i;
if (!(i=='\0')) //Because the for loop doesnt check if its the null terninator
sString[i] += gap;
}
}
else
{
for ( int i=0; sString[i] != '\0'; ++i )
{
sString[i] += gap;
++i;
if(!(i=='\0'))
sString[i] -= gap;
}
}
for (int i=0; sString[i] != '\0'; ++i )
{
cout << sString[i];
}
cout << "\n";
cout << "\nWould you like to do it again? (y/n): ";
cin >> restart;
}
while (restart == 'y' || restart == 'Y');
Pause(); //all you need to know is that it is in macros.h and waits for the users input(it works)
return 0;
}
Any help would be appreciated,
Thanks in advance
kaaie
p.s.
Do you think this method would be easy to decipher/crack?
This post has been edited by kaaie: 15 July 2009 - 06:29 PM

New Topic/Question
Reply



MultiQuote






|