How to wrap numbers between 0 and 9

Hi, I am writing an encryption program and I want there to be only numbers and letters in the encryption. My offset is going to be a random number between 8 and 15, so I need to figure out how to wrap numbers on the interval [0,9]. I have been playing around with this for a while now and can't seem to get it to work, although it seems like it should be fairly straight forward. Any help is much appreciated.

Here is what I have been trying:

#include <iostream>
#include <string>
#include <ctime>
#include <stdlib.h>

using namespace std;

int main()
{
//variables

string message, mutant = "";
int length;

//generate a random number between 8-15 for the offset

int offset;
srand(time(0));
offset = rand()%8 + 8;
cout << "Offset = " << offset << endl << endl;

//enter message

cout << "Enter your message: ";
getline(cin,message);
length = message.length();
cout << "\nYour message has " << length << " characters" << endl << endl;
//cout << message << endl;


//cast message to lowercase

for (int i = 0; i < length; i++)
{
message[i]=tolower(message[i]);

}
cout << "Message to be encrypted: " << endl;
cout << message << endl << endl;


for (int i = 0; i < length; i++)
{
//if value is between 48-57

if ( 48 <= message[i] <= 52)
{
mutant += char((message[i] + offset) - 10);
}
if ( 53 <= message[i] <= 57)
{
mutant += char((message[i] + offset) -20);
}



}
cout << mutant << endl << endl;
return 0;
}
Hi @big, don't forget to embrece your code with code-block in the Format. (:

I didn't understand these lines:
1
2
3
4
if ( 48 <= message[i] <= 52)
{
mutant += char((message[i] + offset) - 10);
}


Why "-10"?

But i do know what you want, it seems that you are messing numbers with characters. As in ASCII all numbers are characters, 1 is a number, but '1' is a character, "1" is a string. And luckily numbers and letters are consective in ASCII, so you don't have to do anything special with 0-9, just deal with them like you do with letters, add offset, and mod it to keep it in range.
@yueeng Thanks, I figured out how to solve my problem. I was subtracting 10 because I want the encryption to consist of only lowercase characters a-z, 0-9. Say the character you want to have encrypted is '9' and the offset is 15. I need it to count through the sequence of characters 0,1,2,3,4,5,6,7,8,9, and once it gets to 9 continue counting at 0. So for '9' with an offset of 15, count through that sequence 15 spaces and you get 4. That is why I needed to wrap the numbers. I was on the right track, just was not going about it in quite the right way. Here is what I ended up getting to work for a random offset value between 8-15:

note:
message[i] is the number corresponding to the ascii character between 0-9
offset is a random value between 8-15
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

if ( 48 <= message[i] <= 57 )
{ 
        if ( message[i] + offset > 57 )
	{
		if ((message[i] + offset) - 10  > 57 )
		{
			mutant += char(message[i] + offset - 20 );
		}
		else
		{
			mutant += char((message[i] + offset) - 10 );
		}
	}
	else
	{
		mutant += char((message[i] + offset) - 10);
	}
}
else
{
	mutant += char(message[i] + offset);
}
Last edited on
warning: comparisons like ‘X<=Y<=Z’ do not have their mathematical meaning [-Wparentheses]

1
2
if( '0'<=message[i] and message[i]<='9' )
if( isdigit(message[i]) )
Topic archived. No new replies allowed.