0

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?

Chris
  • 26,361
  • 5
  • 21
  • 42
  • 2
    [I can't replicate your problem](https://godbolt.org/z/r5ebEY7bv). Please [edit] your questions to show us a [mre]. And also tell us how you know that `new_one` becomes `155` instead of `107`. Also please copy-paste a full session running your minimal example, to show us the actual input and output. – Some programmer dude Oct 14 '22 at 05:48
  • @Jared Lee: This really looks like you should read up on C++ a little more first (Like how to do loops, for example). Here is a tutorial that i found really helpful: https://www.learncpp.com/ – nick Oct 14 '22 at 06:00
  • 1
    Are you using a [good c++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). These are available as PDFs for free. – Jason Oct 14 '22 at 06:08
  • 1
    `155` is the sum of `104+51`. `104` is the ASCII code for the letter `'h'` and `51` is the ASCII code for the digit `'3'`. Which means you must be reading in your `number` as a `char` instead of as an `int`. But that is not what the code shown is doing, which means the code shown is not your real code that you are having trouble with. – Remy Lebeau Oct 14 '22 at 06:13
  • Thank you for the answer. Do you know how to let '3' stay as '3' when adding to 'h'? Thank you. --------Actually, I found out how. THanks – Jared Lee Oct 14 '22 at 06:30

1 Answers1

0

This is my solution. Hope it helps!

#include <iostream>

using namespace std;

int main()
{
    int offset = 0;
    int num_with_offset;

    int size;
    
    // Gets offset from user
    do{
        cout << "You chose to encypher a message\nPlease choose an integer between 1-25: ";
        cin >> offset;
    } while (offset < 1 || offset > 25);
    
    // Gets letters in word
    do{
        cout << "Letters in word: ";
        cin >> size;
    } while(size < 0);
    
    // Given size, init arrays
    
    int number[size];
    char one[size];
    
    // Conversion from char to int
    for(int i = 0; i < (sizeof(one)/sizeof(one[0])); i++)
    {
        cout << "Enter character " << (i + 1) << ": ";
        cin >> one[i];
        num_with_offset = one[i] + offset;
        // Converts ASCII to integer and stores it into array
        number[i] = static_cast<int>(num_with_offset);
    }
    
    // Prints out the new encrypted message
    for(int j = 0; j < (sizeof(number)/sizeof(number[0])); j++)
    {
        cout << "Your encrypted message is: "
             << number[j] << " , with the code number: "
             << offset << "." << endl;
    }
    cout << endl << endl;
    return 0;
}
daman540
  • 23
  • 7
  • `nt number[size];` this is invalid in C++ and is just an extension in some compilers – phuclv Nov 18 '22 at 16:32
  • I compiled with gcc in a unix environment. Didn't know it wasn't valid. Is there anything I could've done to fix what I did? – daman540 Nov 18 '22 at 20:10
  • the keyword is [VLA](https://stackoverflow.com/q/1887097/995714). Replace with `std::vector` instead – phuclv Nov 20 '22 at 15:29