ciphermagi (450) Feb 17, 2012 at 4:01pm
You still don't have anything to encrypt or decrypt. You're not taking any input
i figured as much which is why im missing the connector between the output from the text file and strings,for the lack of a better term. do i have to declare it as a variable or something else entirely?
I have no idea what you're asking. You have to provide something to be changed, whether you read it from a file or have the user input it manually. Otherwise, you won't get any results.
To get a line of text. It will read up to a newline or the end of file depending on which comes first.
Also:
inputstream.get(char* s, streamsize n );
Or
inputstream.get (char c);
To read character by character.
My preference always leans toward line by line for text files that are structured. If there is no structure (Blender's lightwave .obj export is one) then it would have to be character by character.
You're missing the point. A key concept of programming (or anything complex, I guess) is to beak a task into smaller pieces. You clearly can't wrap your head around all of it at once, so at least figure out that one line you need to make my code work.
ok, I made some overhaul to my program and did what I should have done from the get go and just printed out the long strings. Now I can see what I figured from the start that my functions suck.
This is just a once over on your code, I didn't really look in depth on it, but there are a few problems that just jump out at me right from the start.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
string affineA(string message1) // Should be a void function
{
int a=1;
int b=15;
int i;
char c; // Not a pointer, only holds one character
for(i=0;i<=message1.length();i++)
{
c=((a*i+b)%26);
cout<<char(c); // This line doesn't do anything. Try cout << c;
// plaintext1 never does get changed, so you never have the up to date text in a variable
}
return plaintext1; // plaintext1 does not need to be returned - it's global (bad idea)
}
1 2 3 4
else
{
cout<<"Program terminated"; // This doesn't do anything except say terminated. Program still runs.
}
string affineA(string message1)
{
int a=1;
int b=15;
int i;
char c;
for(i=0;i<=message1.length();i++)
{
c=((a*i+b)%26);
cout<<char(c);
}
return plaintext1;
}
Since these three functions are very similar let me just dissect one. First, valid array indexes are from 0 to array_size-1. This is true for strings as well, so you might want to adjust your for continuation expression.
In all of these functions you return a string you never modify. Returning a string looks like the right thing to do, but you probably want it to be one that you built in the function. I don't see any reason for a global one.
You don't actually read from the message passed to the functions, either. In this one, it appears you're manipulating the value of the loop counter instead.
Name your functions after what they do.
The two affine functions (almost) look like affine encryption algorithms. I'm at a loss as to why you're trying to decrypt messages with them.
That would be closer, but it wouldn't be right. Once again, i is your loop counter. You don't need to decode your loop counter.
In the affine cipher the letters of an alphabet of size m are first mapped to the integers in the range 0..m − 1
1 2 3 4 5 6 7 8 9 10 11
#include <cctype>
inlinechar encode_affine(char to_encode, int a, int b)
{
if (!std::isalpha(to_encode)) // not for encoding non-letters.
return to_encode ;
to_encode = toupper(to_encode) ; // also can't preserve case.
return'A' + (a*(to_encode-'A'))%26 ;
}