Sign in to follow this  

Encrypting - Decrypting (STRING)

This topic is 3573 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey I'm quite new to this site and programming itself. I was wondering if anyone could help me fix this problem. Basically user inputs a string of characters and it encrypts it by moving each character up 3 in the alphabet. The current program spits aload of symbols after it encrypts and I don't know why. Appreciate if anyone can help. #include "stdafx.h" #include <iostream> #include <string> using namespace std; int main () { string str; char encrypt[40]; int i = 0; int offset; int choice; cout << "Enter a '1' to encrypt a message and a '2' to decrypt a message.\n"; cin >> choice; cout << "What message would you like to encrypt/decrypt?\n"; cin >> str; cout << "What do you want your offset number to be?\n"; cin >> offset; for(int i = 0; i < str.length(); i++) { encrypt[i] = str.at(i); } if (choice = 1) { while(i<str.length()) { encrypt[i] = int(encrypt[i]) + offset; i++; } } else (choice =2); { while(i<str.length()) { encrypt[i] = int(encrypt[i]) - offset; i++; } } cout << encrypt <<endl; return 0; }

Share this post


Link to post
Share on other sites
There's a few problems with the code as it is.

if(choice=1) and if(choice=2) assigns the value 1 and 2 to choice, to compare you need two of them = ( it's if( choice == 1) and if(choice==2) )

your else(choice=2) should be else if(choice==2) (also note that you've got a ; too much, on the else-line)

And then finally to your question: you need to null-terminate the string, easiest way to do that would be to add

encrypt[str.length()] = '\0';

after your for-loop that copies the string.

I think that's all =)

Share this post


Link to post
Share on other sites
Thank you very much, that seemed to fix my problem.

How would I go about on trying to encrypt a few words at the same time? at the moment it just encrypts the one word. But if you type several words it goes all funny.

Share this post


Link to post
Share on other sites
first of all, there are some possible problems with your code. What if your string is more than 40 chars long? You will get buffer overrun in:

for(int i = 0; i < str.length(); i++)
{
encrypt[i] = str.at(i);
}






also after for loop you need to set it to zero again, for encoding/decoding (at least in VS2005)

You can change the code like this to get rid of that problem (and simplify the code):

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

int main ()
{

string str;
int i = 0;
int offset;
int choice;


cout << "Enter a '1' to encrypt a message and a '2' to decrypt a message.\n";
cin >> choice;
cout << "What message would you like to encrypt/decrypt?\n";
cin >> str;
cout << "What do you want your offset number to be?\n";
cin >> offset;

if(choice == 2) offset = -offset;

do
{
str.at(i) = (char)(str.at(i)+offset);
}
while(++i<str.length());

cout << str <<endl;

return 0;
}




Share this post


Link to post
Share on other sites
Quote:
Original post by TPPROG
How would I go about on trying to encrypt a few words at the same time? at the moment it just encrypts the one word. But if you type several words it goes all funny.

Reading a std::string like that only reads up to the next whitespace, i.e. a single word. Instead you want to use the free function getline(), which reads until it encounters the terminating character ('\n' by default):
std::string str;
std::getline(str);

Share this post


Link to post
Share on other sites
Quote:
Original post by swiftcoder
Quote:
Original post by TPPROG
How would I go about on trying to encrypt a few words at the same time? at the moment it just encrypts the one word. But if you type several words it goes all funny.

Reading a std::string like that only reads up to the next whitespace, i.e. a single word. Instead you want to use the free function getline(), which reads until it encounters the terminating character ('\n' by default):
std::string str;
std::getline(str);


Just in case the poor guy doesn't have any initiative for looking up documentation, std::getline needs at least two parameters - the stream to read from, and the string to read into. Thus, 'std::getline(std::cin, str)'.

Share this post


Link to post
Share on other sites

This topic is 3573 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this