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

2

I'm holding programming and am making a code where I must turn a code into binary in a sentence. I'm turning the binary value into decimal, and getting the decimal value and comparing the ASCII table. But I do not know how to save 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;
}
    
asked by anonymous 25.08.2016 / 02:47

1 answer

2

The first thing is to decide whether to do it in C or C ++. It seems to go from C ++, so use everything from C ++ and avoid things from C.

I tried to do here in a better way what I understood that is the question. Some things can be done better (you can format the output a little better, you can change the do-while by a for , request a text entry and not an integer, optimize the performance), but I think it is already a big 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;
    return 0;
}

See working on ideone .

I took away unnecessary things and started using string instead of an array of char which is C thing.

I gave an organized, even left the declarations of the variables closer to their use.

I've created a loop that is most important to request multiple numbers in a row. The output of it is when you enter a number 0.

I created the variable texto that will accumulate the letters.

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

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

I have killed all if s using math. I have normalized the number for the letter index. Since the typed could be uppercase or lowercase I used the operators of & ( AND ) and ~ ( XOR ) over 32, so we ensure that the number is reduced to the range of 65 to 96 (although this is not guaranteed in the code). I also removed 65 to get to index 0. The first dec equals the letter A or a . O is the initial index of an array or string (which is nothing more than an array of char ). With this operation the result will always be from 0 to 25, exactly what you wanted (as long as you enter a binary equivalent to the valid characters).

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

If you do not understand a concept you can ask new questions specific to each difficulty you encounter, so it becomes more organized.

    
25.08.2016 / 03:47