jeudi 14 juillet 2016

Truncated If Statement C++ (Turning letters to numbers)

I am trying to change a series of letters into numbers with C++ and have started by making this code.

However, it appears that the math that calculates the digit1 variable is never getting executed.

Any thoughts?

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int qtyofnumbers, num, digit1, counter;
    char letters, upperlower;

    cout << "Enter a letter: ";
    cin >> letters;

    for (counter = 0; counter < 8; counter++)
    {
        if (counter == 3)
            cout << "-";

        num = static_cast<int>(letters) - static_cast<int>('A');

        if (0 <= num && num < 26)
            digit1 = (num / 3) + 2;

        if (((num / 3 == 6 ) || (num / 3 == 7)) && (num % 3 == 0))
            digit1 = digit1-1;

        if (digit1 > 9)
            digit1 = 9;    

        cin >> letters;
    }

    cout << digit1;

    return 0;
} 

Aucun commentaire:

Enregistrer un commentaire