Getting random numbers.

Hello. I am trying to do a Cesar crypt, this is what I have done 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
#include <cstdlib>
#include <iostream>

using namespace std;
char lol(){
     char word[200];
     int length;
     string lol;
     char word2[200];

     cout<<"Enter a word or a string of words: \n"; cin.get(word,(sizeof word)); cin.ignore(1000,'\n');
     lol=word;
     length=lol.length();
     cout<<length<<endl;
     for(int i=1; i<length; i++){
             if (word[i]=='A'){
                               word2[i]='M';}
             if (word[i]=='B'){
                               word2[i]='N';} //abcdefghijklmnopqrstuv
             if (word[i]=='C'){
                               word2[i]='O';}
             if (word[i]=='D'){
                               word2[i]='P';}
                              
             
             }
              cout<<word2;
     
}
int main()
{
    lol();
    system("PAUSE");
    return 0;
}

Now if I type ABCD I get -NOPXöCT , where do these letters come from?
THe loop should start at 0, not 1. The extra random characters is because the string is not null terminated. Just put add word2[length] = '\0';
Ty for answer. Just a question, should I add word2[length]= '\0' in the initialization or where?
At the end, before cout << word2.
Better yet, make word2 a string and use string operators.
Now I get MNOP on one line and two random numbers on the next :/

And if I make it to a string I dont know how to do, I mean how do I place a specific word on a specific place like a char? It's easy with a char to just do charname[i], but I don't know how to do so in a string :/ Yeah I am newb.
I agree with Gaminic. std::string is your friend. I try to avoid c-style strings if I can.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>

int main()
{
  std::string word, word2;
  std::cout << "Enter a word or a string of words: ";
  std::getline(std::cin,word);
  int length=word.length();
  std::cout << length<< std::endl;
  for(int i=0; i<length; i++)
  {
    char c = word.at(i);
    word2 += (c < 'M') ? c + 13 : c;
    std::cout << word2;
  }
  system("PAUSE");
  return 0;
}

Last edited on
I made the Caesar Cipher before. Is this the one you are trying to do:
http://en.wikipedia.org/wiki/Caesar_cipher

If it is, why are you trying to get random numbers? This shift should be a certain ammount.
else if
Like Lynx, not sure what you need random numbers.
And, why are you mixing string and char[]? Just use string.
Last edited on
The random numbers I am referring to is something that should not be a part of the output, it is the ö letter. I dont know why it is there.

Because I don't know how to hold a certain letter in string as char[] can do.
You can get elements from a string as you can a char[]:

string input = "hello"
1
2
3
4
5
6
7

string[ 0 ] - 'h'
string[ 1 ] - 'e'
string[ 2 ] - 'l'
string[ 3 ] - 'l'
string[ 4 ] - 'o'
string[ 5 ] - '\0'

I meant:
1
2
3
4
5
6
input[ 0 ] - 'h'
input[ 1 ] - 'e'
input[ 2 ] - 'l'
input[ 3 ] - 'l'
input[ 4 ] - 'o'
input[ 5 ] - '\0'


Here's a bit of my code, for the Caesar Cipher:
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
	//create an index counter
	unsigned int i = 0;

	//ENCRYPT the message

	//i < total size. if i == input.size()
	//that would be the element which holds the '\0' character
	//so we don't want to do anything with that element of the string
	while( i < input.size() )
	{
		//if the current character is NOT a space.
		//perform an action based on an upper
		//or lower case character
		if( input[ i ] != ' ' )
		{
			if( isupper( input[ i ] ) )
			{
				//if encryption called this function
				if( encryption )
					enUpper( input, i );
				//else it's decryption
				else
					deUpper( input, i );
			}

			if( islower( input[ i ] ) )
			{
				if( encryption )
					enLower( input, i );
				else
					deLower( input, i );
			}
		}
		
		++i;
	}


And one of the case functions:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void enUpper( std::string &input, int i )
{
	//Ascii table. upper case Z = 90
	const int upper = 90;

	int decimal = input[ i ];

	//add the shift ammount
	decimal += cs;

	//this performs a wrap-around
	//if the shift goes z + 1
	//take away 26 so that it is now 'a'
	if( decimal > upper )
		decimal -= 26;

	//save the 'encrypted' letter
	input[ i ] = decimal;
}


cs is in my .h header file as:
#define cs 3
Last edited on
aight, ty for help! :)
Topic archived. No new replies allowed.