Reading an int from a string
Oct 6, 2016 at 9:38pm UTC
Hello, I am working on a decryption project, and I am trying to decode this message using a Caesar Cipher method.
13nggnpxongx3933&7#10
"13" being my shift number,
"nggnpxongx3933" being the message to be decoded,
and
"7" and "10" being the location of the spaces in the original message.
Now, my question is how would I make "13" an integer, so that I can have my shift number as an int?
any help would be great!
The original message is "attack at 0600"
code so far:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string message, decrypt, n;
int count, m, d;
ifstream input;
//read encrypted message from "secret.dat"
input.open("secret.dat" );
getline(input, decrypt, '&' );
getline (input, message);
input.close();
cout << "encrypted message is: " << decrypt << endl;
m = message.length();
d = decrypt.length();
for (count = 0; count < d; count++)
{
if (isalpha(decrypt[count]))
{
decrypt[count] = tolower(decrypt[count]);
for (int i = 0; i < 13; i++)
{
if (decrypt[count] == 'a' )
decrypt[count] = 'z' ;
else
decrypt[count]--;
}
}
if (isdigit(decrypt[count]))
{
for (int i = 0; i < 13; i++)
{
if (decrypt[count] == '0' )
decrypt[count] = '9' ;
else
decrypt[count]--;
}
}
}
cout << decrypt << endl;
cout << message << endl;
return 0;
}
Oct 6, 2016 at 10:14pm UTC
Last edited on Oct 6, 2016 at 10:30pm UTC
Oct 6, 2016 at 10:48pm UTC
create a function to check each char of a string and see if it's a '0', '1' and so on.
Topic archived. No new replies allowed.