| CodeGuru Home | VC++ / MFC / C++ | .NET / C# | Visual Basic | Newsletters | VB Forums | Developer.com |
|
|||||||
| C++ (Non Visual C++ Issues) Ask or answer C and C++ questions not related to Visual C++. This includes Console programming, Linux programming, or general ANSI C++. |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Encryption Program Problem
Having trouble with this code that resembles a Caesar cipher (alphabet letter shift).
The task is to: (All lowercase letters) 1. Design a function, encrypt, that when applied to an unencrypted string un_str, transforms un_str to an encrypted string en_str using a right shift of length s to the letters of the alphabet with wraparound. 2. Design a function decrypt that when applied to an encrypted string en_str, transforms en_str to a decrypted string. The string en_str will have been encrypted with a cipher of length s. Input: Shift length s, unencrypted string un_str, encrypted en_str Output: String un_str and its encryption using function encrypt, String en_str and its decryption using function decrypt Finished Program should be able to: - Prompt for and get shift length x - Prompt for and get unencrypted string un_str - Encrypt un_str into en_str - Prompt for and get encrypted string en_str - Decrypt en_str into un_str This is what I have so far, it's obviously amateur's work and I know there are SEVERAL errors as it is (though I'm not sure where ~). I'm trying to have it so that the program asks for a shift length, followed by offering a decrypt or encrypt option, followed by prompting for an entered string, and have the ability to perform those processes. Unfortunately I'm stuck. The string part, I really need help on. Thanks in advance Code:
Title: 9.cpp
#include "9f.h"
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
int main(void)
{
char un_str[100];
int shift;
int crypt;
std::cout << "Enter a desired shift length -> ";
std::cin >> shift;
std::cout << "Press 1 to encrypt or 0 to decrypt: ";
std::cin >> crypt;
while (getchar() != '\n');
if (crypt == 1)
encrypt(shift);
else
{
decrypt(shift);
}
std::cout << "Enter a string -> ";
std::cin.getline(un_str, 100);
return 0;
}
Code:
Title: 9f.cpp
#include "9f.h"
#include <iostream>
void encrypt(int shift) // prototypes of functions used in the code
{
char en_str;
std::cout <<
en_str = getchar();
while(en_str != '\n')
{
if (ch == ' ')
putchar(en_str);
else
{
if(shift == 1)
putchar (en_str + shift);
else
putchar (en_str - shift);
}
en_str = getchar();
}
putchar(en_str);
return 0;
}
void decrypt(int shift)
{
shift = -1 * shift;
encrypt(shift);
return 0;
}
Code:
Title: 9f.h void encrypt (int shift); void decrypt (int shift); |
|
#2
|
||||
|
||||
|
Re: Encryption Program Problem
You are on the right track. One of your problems is the successive calls of getchar. Remember that not only does it read the last letter of your input, it also REMOVES that letter from your input.
when you write: Code:
while (getchar() != '\n'); If I were you, I would modify your encrypt and decrypt functions to not require user input. Keep all the user input management inside your main. I my opinion, this is what your encrypt should look like: Code:
char encrypt(char input, int shift) //Takes a char, a shift, and returns the encrypted char
{
char encrypted;
encrypted= input + shift;
if (encrypted > 122)
{
encrypted -= 26;
}
return encrypted;
}
I'm also confused about your main. You call the encrypt/decrypt functions before asking the user for a string. What is up with that? Try something like this: Code:
int main(void)
{
char un_str[100];
char en_str[100];
int shift;
int crypt;
char current_letter;
std::cout << "Enter a desired shift length -> ";
std::cin >> shift;
std::cout << "Press 1 to encrypt or 0 to decrypt: ";
std::cin >> crypt;
std::cout << "Enter a string -> ";
std::cin.getline(un_str, 100);
//Loop begin
//Validate input, ie not space etc
en_str[i] = encrypt(current_letter, shift);
//loop end
cout << en_str;
return 0;
}
If you want my personal advice, don't worry about bad user input in the beginning, as it is distracting you from thinking about the real problem. Solve the real problem, then worry about correcting user input. By the same logic, concentrate on encoding only right now. You'll worry about the if logic later. |
|
#3
|
|||
|
|||
|
Re: Encryption Program Problem
Is there a way to do it so that a 122 and +- 26 isn't necessary? As far as that main function, yeah I'm confused, not sure where to go with the strings
|
|
#4
|
||||
|
||||
|
Re: Encryption Program Problem
Quote:
If you want to avoid the if, use this formula, it will always work. encrypted = ((letter - 97 + shift) % 26) + 97; Another solution would be doing giant switch statement, or using while loops or other. But I don't think they would be as effective. Here is a solution, but I wouldn't use it. Code:
shift = shift%26;
encrypt = non_encrypt;
for (int i=0; i<shift; i++)
{
switch encrypt
{
case 'a': encrypt = 'b'; break;
case 'b': encrypt = 'c'; break;
case 'c': encrypt = 'd'; break;
...
case 'z': encrypt = 'a'; break;
}
}
|
|
#5
|
|||
|
|||
|
Re: Encryption Program Problem
Quote:
Quote:
Code:
encrypted = ((letter - 'a' + shift) % 26) + 'a'; Quote:
__________________
C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way Kindly rate my posts if you found them useful
|
|
#6
|
||||
|
||||
|
Re: Encryption Program Problem
Quote:
Quote:
Agree too, but I didn't want to bring in portability, as this was just a simple homework assignment. |
|
#7
|
|||
|
|||
|
Re: Encryption Program Problem
thanks, I'm updating my program now and hopefully it works. in the meantime, I've learned that this is a skeleton of what it's supposed to look like:
You need to be writing a global function to get the shift and another to get the string. Code:
int main()
{
// Get shift
int shift = get_shift();
// Get input string
std::string input = get_string();
// Encrypt. Save the original string
std::string encrypted;
encrypt(input, encrypted);
// Display results
display(input, encrypted);
......... get_string, decrypt, and display
return 0;
}
Also I'm getting errors from this: //Loop begin //Validate input, ie not space etc en_str[i] = encrypt(current_letter, shift); //loop end pa4.cpp:34: error: `i' was not declared in this scope pa4.cpp:34: error: invalid conversion from `char' to `char*' pa4.cpp:34: error: initializing argument 1 of `void encrypt(char*, int)' Last edited by mikesnuggets22; July 16th, 2009 at 06:44 PM. |
|
#8
|
||||
|
||||
|
Re: Encryption Program Problem
Quote:
Quote:
|
|
#9
|
|||
|
|||
|
Re: Encryption Program Problem
Quote:
__________________
C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way Kindly rate my posts if you found them useful
|
![]() |
| Bookmarks |
|
||||||
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|