Problem with creating an automatic message coder

Hi, I am a Noob programmer learning C++ to make my own games and am trying to make an automatic message coder (Inspired by my math lesson today :)) and can't seem to get it to work, I know that this is not how the code should have been done,I realized that to late :/. My problem is figuring out how to get a multi-word input and get a coded output using either random or set letters. please help :) here's the code:

#include <iostream>
using namespace std;

int main () {
int a , b , c , d , e , f , g , h , i , j , k , l , m , n , o , p , q , r , s , t , u , v , w , x , y , z;
cout << "ENTER YOUR MESSAGE: ";
cin >> a;
cin >> b;
cin >> c;
cin >> d;
cin >> e;
cin >> f;
cin >> g;
cin >> h;
cin >> i;
cin >> j;
cin >> k;
cin >> l;
cin >> m;
cin >> n;
cin >> o;
cin >> p;
cin >> q;
cin >> r;
cin >> s;
cin >> t;
cin >> u;
cin >> v;
cin >> w;
cin >> x;
cin >> y;
cin >> z;

if (a== a)
cout << "c";
else
cout << "_";
if (b == b)
cout << "f";
else
cout << "_";
if (c == c)
cout << "g";
else
cout << "_";
if (d == d)
cout << "p";
else
cout << "_";
if (e == e)
cout << "l";
else
cout << "_";
if (f == f)
cout << "y";
else
cout << "_";
if (g == g)
cout << "h";
else
cout << "_";
if (h == h)
cout << "e";
else
cout << "_";
if (i == i)
cout << "d";
else
cout << "_";
if (j == j)
cout << "x";
else
cout << "_";
if (k == k)
cout << "v";
else
cout << "_";
if (l == l)
cout << "z";
else
cout << "_";
if (m == m)
cout << "k";
else
cout << "_";
if (n == n)
cout << "r";
else
cout << "_";
if (o == o)
cout << "s";
else
cout << "_";
if (p == p)
cout << "a";
else
cout << "_";
if (q == q)
cout << "w";
else
cout << "_";
if (r == r)
cout << "o";
else
cout << "_";
if (s == s)
cout << "n";
else
cout << "_";
if (t == t)
cout << "n";
else
cout << "_";
if (u == u)
cout << "m";
else
cout << "_";
if (v == v)
cout << "j";
else
cout << "_";
if (w == w)
cout << "b";
else
cout << "_";
if (x == x)
cout << "q";
else
cout << "_";
if (y == y)
cout << "u";
else
cout << "_";
if (z == z)
cout << "t";
else
cout << "_";

system("PAUSE");
return 0;
}
I believe that I should have outcomes for each possible output letter for each input letter, right? not sure how without a hundred some lines of code though :/
Last edited on
Kudos for taking a shot at it. It looks like you're trying to get input from the user. Instead of collecting the letters one at a time, why not get them all at once?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;

int main ()
{
	string input;

	cout << "Please type your message:";
	cin >> input;

	cout << "You typed: " << input;

	return 0;
}


Then you can loop through the characters of the variable "input" and convert them to what you wish. Dig in to looping, give it a shot, and let me know what you come up with.
Thanks! will try as soon as I get the chance :)
i think i understand what you are trying to do, but either way. you should definitely use an array of characters then you can store, refer to and manipulate each character or element:
1
2
3
4
   char message [100]; //creates a char variable with 100 letters or //numbers
cin>> message;
//say, if they typed... "Hello". then you could go
cout << message [2]; //and that would print the 3rd letter of whatever they //typed. 

Because when you say: cin >> (char array);
Then each letter they type is stored in each element of the array. so you can automaticlly store letters in seperate indexes without declaring 25.
wait... so when someone typed a message it would sort of scramble the letters? or would it take random letters from a sort of "library" of letters? because I want to also make a decoder for the same code in which case I would need set outcomes for each letter wouldn't I?
That is for you to decide. You are the programmer! :o)

There are many ways to get what you are trying to do done. We're just coaching you through it.

Once you get the input from the user, you can then "encrypt" the message however you wish. You will then need a "un-encrypt" program to go the other way. We'll need loops for both, so let's just work on looping for a little bit, shall we?

There are a lot of ways to loop. There are the (in my opinion) the old-fashioned ways such as "for each" and "do while" loops. These are easy to learn. Do some research on these looping methods and build some example programs.

There are also newer ways of looping, called itterators. They are a bit harder to learn (again, my opinion), but powerful. You may want to take a stab at itterators after you learn the old fashioned looping methods.

So learn about loops, try to add it to your program, and throw it on here so we can see it.
so. if I use a for loop can I, for example, set it up so that each letter would maybe change into the letter 3 letters down in the alphabet? or maybe turn them into Binary or something?
You most certainly can. Being that each letter is actually represented by a number, you can even use addition / subtraction to get you there. The capital 'A' is also a 65. So adding 3 gives you 68, which is 'D'. If you wanted to subtract 3 then 'A' becomes '>'

See this link for a table of the letters and their values:

http://www.asciitable.com/

so if I wanted to type a code would I have to use the numerical values or just the letters?
Try this little piece of code:
1
2
3
4
char val1 = 'A';
cout << val1 << " is the same as " << (int)val1 << endl;
val1 = val1 + 1;
cout << "if I add 1 to val1, I get " << val1 << " which is the same as " << (int)val1 << endl;
ok... I think I get it so I will try and wright the code after school
Sorry I haven't gotten back to you for a while, i've been really busy with school but once finals are over in a few days I will get back on this!
Topic archived. No new replies allowed.