I am new to C++, learned it for more than a month. I have a beginner-level knowledge of Python, like creating a list, modifying it, loops, etc. I don't know some codes for C++ that I know in python.
I am making a program for a school class (creative program). This is a part of my code (description at the bottom):
int number, new_one, num_letter;
char one;
cout << "You chose to encypher a message\nPlease choose an integer between 1-25:";
cin >> number;
cout << "How many letters are in your word?";
cin >> num_letter;
if (num_letter == 1)
{
cout << "Enter the first letter";
cin >> one;
new_one = one + number;
cout << "Your encrypted message is '"
<< static_cast<char>(new_one)
<< "' with the code number of "
<< number;
I am making a program where it enciphers and deciphers a message. The user chooses the number of letters of their message (maximum of 10 because I don't know how to use a for
-loop in C++ yet). Then, they choose an integer. Then, they enter the letter, hit Enter, enter the letter, and hit Enter for the number of letters in their message (I don't know how to separate strings to chars in C++ yet).
When the user enters their letter and hits Enter, I cin >>
that letter into the variable one
, which is a char
. Then, I add that one
to the number
the user chose, so the ASCII code of the one
increases by the value of the number
.
For example, when I enter 3
for number
and h
for the value of one
, 104
(the ASCII code of h
) should add up with 3
, resulting in 107
, which I then would static_cast
to a char
value.
But, when I add h
and 3
, instead of creating 107
, it creates 155
. Same for other variables. I tried cout
'ing static_cast<int>(one)
(in this case, the letter h
) and number
(which is 3
). They printed 104
and 3
.
But, when I add those two values, it prints 155
. Why is this happening?