How to store letters in a variable and display them all at the end by forming a phrase/word

Asked

Viewed 874 times

2

I’m holding up programming and I’m making a code where I’m supposed to turn a binary code into a phrase. I’m turning the binary value into decimal, and taking the decimal value and comparing the ASCII table. However, I don’t know how to keep the letters that appear in a variable or vector to display them all together at the end.

int main()
{
    setlocale(LC_ALL, "Portuguese");

    int dec=0,num,d=1;
    char charr[25];

    cout<<" Digite o numero binario de 8 dígitos (dígite 0 para parar): ";
    cin>>num;

    do {
    dec = dec+(num%10)*d;
    d = d*2;
    num = num/10;
    }

    while(num!=0);

    charr[0] = 'A', charr[16] = 'Q';
    charr[1] = 'B', charr[17] = 'R';
    charr[2] = 'C', charr[18] = 'S';
    charr[3] = 'D', charr[19] = 'T';
    charr[4] = 'E', charr[20] = 'U';
    charr[5] = 'F', charr[21] = 'V';
    charr[6] = 'G', charr[22] = 'W';
    charr[7] = 'H', charr[23] = 'X';
    charr[8] = 'I', charr[24] = 'Y';
    charr[9] = 'J', charr[25] = 'Z';
    charr[10] = 'K';
    charr[11] = 'L';
    charr[12] = 'M';
    charr[13] = 'N';
    charr[14] = 'O';
    charr[15] = 'P';

    if (dec == 65 or dec == 97)
    {
        cout<<charr[0];
    }

    if (dec == 66 or dec == 98)
    {
        cout<<charr[1];
    }

        if (dec == 67 or dec == 99)
    {
        cout<<charr[2];
    }

        if (dec == 68 or dec == 100)
    {
        cout<<charr[3];
    }

        if (dec == 69 or dec == 101)
    {
        cout<<charr[4];
    }

        if (dec == 70 or dec == 102)
    {
        cout<<charr[5];
    }

        if (dec == 71 or dec == 103)
    {
        cout<<charr[6];
    }

        if (dec == 72 or dec == 104)
    {
        cout<<charr[7];
    }


        if (dec == 73 or dec == 105)
    {
        cout<<charr[8];
    }

        if (dec == 74 or dec == 106)
    {
        cout<<charr[9];
    }

        if (dec == 75 or dec == 107)
    {
        cout<<charr[10];
    }

        if (dec == 76 or dec == 108)
    {
        cout<<charr[11];
    }

        if (dec == 77 or dec == 109)
    {
        cout<<charr[12];
    }

        if (dec == 78 or dec == 110)
    {
        cout<<charr[13];
    }

        if (dec == 79 or dec == 111)
    {
        cout<<charr[14];
    }

        if (dec == 80 or dec == 112)
    {
        cout<<charr[15];
    }

        if (dec == 81 or dec == 113)
    {
        cout<<charr[16];
    }

        if (dec == 82 or dec == 114)
    {
        cout<<charr[17];
    }

        if (dec == 83 or dec == 115)
    {
        cout<<charr[18];
    }

        if (dec == 84 or dec == 116)
    {
        cout<<charr[19];
    }

        if (dec == 85 or dec == 117)
    {
        cout<<charr[20];
    }

        if (dec == 86 or dec == 118)
    {
        cout<<charr[21];
    }

        if (dec == 87 or dec == 119)
    {
        cout<<charr[22];
    }

        if (dec == 88 or dec == 120)
    {
        cout<<charr[23];
    }

        if (dec == 89 or dec == 121)
    {
        cout<<charr[24];
    }

        if (dec == 90 or dec == 122)
    {
        cout<<charr[25];
    }
    return 0;
}
  • give a read in the vector library of c++ (vector) or create a string variable (string library) and use it concatenating the obtained values. For more information about string: http://www.cplusplus.com/reference/string/string/ E for vector: http://www.cplusplus.com/reference/vector/vector/

  • Thanks Lucas, I’ll take a look.

  • I read your code, I didn’t fully understand the purpose and the specific difficulty.

  • I don’t know how to store this conversion ex: 01010000 is equal to 80 which is equal to 'P' in the ascii table and 01110010 is equal to 114 which is equal to 'R'. How I put this 'P' and 'R' together and make them show side by side at the end of the show?

  • I still don’t understand what you want, which is a shame because the question sounds interesting. Before learning to program you need to learn to understand the problem and communicate it clearly, then programming becomes easier. Programming is detail, details are missing. This looks like a jumble of information. Maybe I understand something, but then your code doesn’t go anywhere near what you’re saying.

  • Pera ai vamos por partes kk I have to take a series of binary codes and convert them to the ASCII table, IE, have a message in binary code that I have to convert it to characters and form the phrase.

  • There’s nothing standard in the code and there’s no explanation for how you intend to do it, let alone a code that handles it.

  • It’s an exercise, Make a program that translates any message into binary! This code above is what I’m trying to do. I’m learning to program in C/C++, sorry if it’s too out of character kk

Show 3 more comments

1 answer

2


The first thing is to decide whether to do it in C or C++. It looks like it goes in C++, so use everything in C++ and avoid things in C.

I tried to do here in a better way what I understood is the question. Some things can be done better (can format output a little better, can change the do-while by a for, ask for a text input and not an integer, optimize performance), but I think it is already a great evolution.

#include <iostream>
#include <string>
using namespace std;

int main() {
    string texto = "";
    while (true) {
        int num;
        cout << "Digite o numero binario de 8 dígitos (dígite 0 para parar): ";
        cin >> num;
        cout << endl;
        if (num == 0) break;
        int dec = 0, d = 1;
        do {
            dec += num % 10 * d;
            d *= 2;
            num /= 10;
        } while (num != 0);
        if (!((dec >= 65 && dec <= 90) || (dec >= 97 && dec <= 122))) {
            cout << "Valor digitado está fora da faixa permitida" << endl;
            continue; 
        }
        texto += "ABCDEFGHIJKLMNOPQRSTUVXYZ"[(dec &  ~32) - 65];
    }
    cout << texto;
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

I took the unnecessary things and started using string in place of a array of char which is a C thing.

I gave an organized, including I left the statements of the variables closer to their use.

I created a loop that is the most important to ask for multiple numbers in sequence. Its output is when typing a number 0.

I created the variable texto that accumulates the letters.

Note that I have extensively used composite data assignment operators, so d *= 2 is the same as d = d * 2. This applies to other variables, including texto which concatenates the letters.

I checked if the number typed is in a valid range of the ASCII table that can be converted.

I killed all the ifs using math. I normalized the number for the letter index. Since what was typed could be uppercase or lowercase I used the operators of & (AND) and ~ (XOR) On top of 32, we ensure that the number is reduced to the range of 65 to 96 (although this is not guaranteed in the code). I also withdrew 65 to reach 0 of the index. The first dec possible equals to the letter A or a. is the initial index of a array or string (that is nothing more than a array of char). With this operation the result will always be 0 to 25, exactly what you wanted (provided that a binary equivalent to valid characters is typed).

I created a text with all letters positioned between 0 until 25 and with the operator [] I took the index calculated in the index according to formula above. The letter found is accumulated in texto.

If you don’t understand any concept you can ask specific new questions for every difficulty you encounter, so it gets more organized.

  • That’s exactly what the exercise was asking for. I’m going to study your code. I’m learning c/c++ now in college. Thank you very much.

  • Just didn’t get this part: } while (num != 0); Dec -= Dec > 96 ? 97 : 65; text += "ABCDEFGHIJKLMNOPQRSTUVXYZ"[Dec]; !

  • @Calicojack gave an improved code.

  • Thank you very much, it helped me a lot I was not managing to do this exercise!

  • 1

    @Calicojack takes a look at the [tour]. You can accept the answer (it is there next to the voting arrows, a sign of check in) and later, when you have a reputation, you can vote for anything you find useful on the site.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.