Skip to main content
gamedev.net
🔒 Locked

Encrypting - Decrypting (STRING)

Started by TPPROG Mar 5, 2008 at 5:41 PM 5 replies 750+ views
Original Post
TPPROG
TPPROG
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 #include 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 = int(encrypt) + offset; i++; } } else (choice =2); { while(i = int(encrypt) - offset; i++; } } cout << encrypt <
chairthrower
chairthrower
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
Pzc
Pzc
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 =)
TPPROG
TPPROG
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.
streamer
streamer
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;}
swiftcoder
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);
Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]
Zahlman
Zahlman
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)'.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.