A simple Cipher

As an exercise to better understand c++ I want to make a simple cipher.
What I am trying to do is simple prompt the user to input two strings, convert the strings to hex values (or int values...), find the difference between the two numbers, and output that number.

After I can do this I want to convert the final number back to ascii text.

This is what I've come up with 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
#include <iostream>
#include <string>
#include <stdlib.h>
#include <sstream>
#include <stdio.h>


using namespace std;

string enc;
string key;
int num1;
int num2;
int num3;

int main()

{
    cout<<"Enter Text to Encrypt"<<endl;
    getline (cin, enc);
    cout<<"Enter Key"<<endl;
    getline (cin, key);

        for (int n = 1, n < 3, n++){
            if (n == 1)
                stringstream convert (enc);
                convert>> std::hex >> num1;
            else
                stringstream convert (key);
                convert>> std::hex >> num2;
        }

    num3 = num1-num2;
    cout<<num3;

    return (0);
}




Whenever I compile this I get two errors. The first being:
expected initializer before '<' token

At line 24


------------------------------


The second error I get is:
expected ')' before ';' token

At line 34

I cannot for the life of me figure out what it is complaining about. Anyone see the problem in my code?
On line 24 you need semicolons where you have commas
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
#include <iostream>
#include <string>
#include <stdlib.h>
#include <sstream>
#include <stdio.h>


using namespace std;

string enc;
string key;
int num1;
int num2;
int num3;

int main()

{
    cout<<"Enter Text to Encrypt"<<endl;
    getline (cin, enc);
    cout<<"Enter Key"<<endl;
    getline (cin, key);

        for (int n = 1; n < 3; n++){
            if (n == 1){
                stringstream convert (enc);
                convert>> std::hex >> num1;
		} else {
                stringstream convert (key);
                convert>> std::hex >> num2;
		}
        }

    num3 = num1-num2;
    cout<<num3;

    return (0);
}


1. line 24 should be "for (int n = 1; n < 3; n++)".
2. I think "if " should include "stringstream convert (enc);" and "convert>> std::hex >> num1;"

The two following code is the same:
1
2
3
4
5
6
7
8
9
      for (int n = 1;n < 3;n++){
            if (n == 1)
                stringstream convert (enc);
                convert>> std::hex >> num1;
            else
                stringstream convert (key);
                convert>> std::hex >> num2;
        }

and
1
2
3
4
5
6
7
      for (int n = 1;n < 3; n++){
            if (n == 1) stringstream convert (enc);
            convert>> std::hex >> num1;
            stringstream convert (key);
            convert>> std::hex >> num2;
        }
Last edited on
Topic archived. No new replies allowed.