array manipulation of messages

Basically i need to encrypt a message into ascii values & i also need to allow the user type some ascii values thus decrypting the values into characters that are placed within the code. I'm still a beginner so maybe some help with this code would do. Thank you.

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
  #include <iostream>
#include <iomanip>
#include <cstring>
#include <string>


using namespace std;
#define Q 2000

void main(){

char array1[Q]={"A gentle, dull flickering flame, burns in the marble hearth. Its dim light scarcely illuminates the small, cozy room with its quiet elegance. The dismal light plays softly causing shadows over the solitary figure of a wooden desk at which Allen was roaming through his memories. Thinking back in the past where he once had a friendship which was out righteously incredible. She was the girl of his dreams, in a way which she had everything he had ever sought out in a beautiful and clever girl. Most of all she had his heart. Her style was incredible in the way the outfits she would wear would match perfectly giving a deep vibrant lively feeling."};

int S[Q];
char S1[Q];
int x;
int i;
char e, d;

cin>>x;

if(x=d){


	cin>>Q;
	for(i=0;i<array1[Q];i++)
		cout << S[i]<<endl;
for(i=0;i<array1[Q];i++)
	cout << S[i]<<endl;





}
Put the code you need help with here.
Last edited on
You seem to have a few misconceptions. In computers, everything is a number - even characters in text are numbers. There is no mechanism for determining if a number is just a number or if it is a character, because it could be either. It is up to the programmer to decide how to treat and interpret data.

Your characters are already in ASCII format, you just need to interpret them as numbers instead of characters. I don't understand why your program is taking input, so maybe I have misunderstood something.
Using arbitrary characters to represent your variables is one of the worst things you can do to yourself especially this early on. How about we start again by deleting all of the muck you have here and try using variable names that mean something to you. EDIT: Backing up your current progress is suggestible anytime someone recommends something this extreme.

Also, 'N', as used on Line 25, is undefined and 'Q' is redefined on Line 17. That's what I noticed without trying to compile anything.
Last edited on
@L B: It takes the input as into when the user inputs specific values it counts up till that value in the paragraph then it retrieves whatever letter is in it. thus output might be some type of secret message gathered up by the letters in the paragraph.


@Computer Geek: sorry the N was supposed to be a Q silly mistake. well what sort of logic should i use for such a program as the array1[] is a must not a choice of mine. but then after such how may tackle this array thus having the users enter values having the program gather which character it is in the array.



the encrypt part is different as it asks the user to input a line and i return it all in ascii values according to the ascii table but that part i can tackle later
@Line8 #define Q 2000
@Line17 int i, a=0, Q=0;
"Q" is being redeclared here. First as macro, Second as int variable. This is incorrect.

Post the full source after making the source compile successfully.

The array logic will not work for the reason that it does not have unique characters. I assume the encrypt logic is just indexing into this array. If so, the encrypted text/data cannot be decoded correctly.
@kannanmj: i fixed the Q part but you misunderstood the situation. see they input a value of the array and i am supposed to retrieve the letter or character at that value in the array. thats for the decrypt part the part of the program i actually wrote some of it.
Can you post the full source (with explanations/comments)? It will help in understanding what has been done so far and what help is needed in the remaining part of it?
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
#include <iostream>
#include <iomanip>
#include <cstring>
#include <string>


using namespace std;
#define N 1000

void main(){

char array1[N]={"A gentle, dull flickering flame, burns in the marble hearth. Its dim light scarcely illuminates the small, cozy room with its quiet elegance. The dismal light plays softly causing shadows over the solitary figure of a wooden desk at which Allen was roaming through his memories. Thinking back in the past where he once had a friendship which was out righteously incredible. She was the girl of his dreams, in a way which she had everything he had ever sought out in a beautiful and clever girl. Most of all she had his heart. Her style was incredible in the way the outfits she would wear would match perfectly giving a deep vibrant lively feeling."};

int S[N];
int z;
int i, a, Q=0;
char e;
char d;
cin>>z;
while(z=='d'){
	cin>>S[i];
	for(i=0;i<=array1[N];i++)
		cin>>S[a];
		cout << i<<endl;
for(i=0;i<array1[N];i++)
	cout << i<<endl;
}}



the "cin>> S[i]" won't work basically i'm trying to have the user input an araay of numbers.
Last edited on
the "cin>> S[i]" won't work basically


That might be because i has an indeterminate value. You don't set i to anything before attempting to use it, so it very likely is not even in the bounds of the S array. I'm not sure how you know that it doesn't work though, since you never do anything with the value stored thusly.

Since z doesn't ever change inside your while loop, the loop is infinite.

array1[N] is not a valid element of array1.

The return type of main must be int by the way.
Well then any suggestions on what i might be able to do to make my code better and more efficient other then the ones you have already mentioned. like how should the for loops look like and so on?
It would help if we knew exactly what you were trying to accomplish. This doesn't even look like an encrypter/decrypter it looks like you are trying to have the user manually type in each character for your "decrypter."

These might help you for encrypt/decrypting
https://www.khanacademy.org/math/applied-math/cryptography
http://www.vectorsite.net/ttcode.html
umm i am trying to have the user type out an array of numbers which fetches the characters from the paragraph array above and forms like random messages
I don't get it. So you want the user to enter arbitrary numbers and then output the letter that the number represents?

ex:

 
65 66 67 68 69 70 32 65 65 65 65
abcdefg aaaa
exactly what i meant i guess i mis wrote it and explained giving the wrong sense. so sorry
Last edited on
So you probably want something like:

1
2
3
4
5
6
int input = 0;
while( input >= 0 && input < 256)
{
    std::cin >> input;
    std::cout << static_cast<unsigned char>(input);
}


If you want utf instead of ascii consider using char16_t or char32_t.
http://en.cppreference.com/w/cpp/language/types
Last edited on
thanks alot man
but i need to use the d function so probably i will use and if function with it?
May i ask what exactly is static_cast<unsigned char>
and as well how can i add that d or e option to it so it may work?
Not sure what you are trying to do with d and e. As far as the cast. It converts it from an integer to a character(unsigned). http://www.cplusplus.com/doc/tutorial/typecasting/
Topic archived. No new replies allowed.