Jump to content
  • Advertisement
Sign in to follow this  
TPPROG

Encrypting - Decrypting (STRING)

This topic is 3933 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 = str.at(i); } if (choice = 1) { while(i<str.length()) { encrypt = int(encrypt) + offset; i++; } } else (choice =2); { while(i<str.length()) { encrypt = int(encrypt) - offset; i++; } } cout << encrypt <<endl; return 0; }

Share this post


Link to post
Share on other sites
Advertisement
else (choice =2); // <-- there is a stray semicolon here

also the encrypt array is never terminated with a NULL to indicate the end of the string

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 = 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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!