-2

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;
} 
HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
Lucas
  • 5
  • 1
  • 2
    Use `{` and `}` !!! – jtbandes Jul 14 '16 at 20:25
  • 1
    Please indent your code properly before asking for help. It's hard to read it as it is. – HolyBlackCat Jul 14 '16 at 20:26
  • 2
    did u step through the code with a debugger? – pm100 Jul 14 '16 at 20:28
  • @jtbandes, thanks that did it. Should be more organized..thanks! – Lucas Jul 14 '16 at 20:31
  • Do your first two `if`s cover all possible values of `num` that your program can encounter? If not, `digit1` will be used uninitialised in the third `if`, and that's UB through and through. I _guess_ they cover all values, but still. – underscore_d Jul 14 '16 at 20:31
  • show us sample input and expected output – pm100 Jul 14 '16 at 20:32
  • Also, you might like to know that you can declare variables in a `for` statement (and soon in C++17, apparently, `if` and `switch`) and therefore need not declare `count` at function scope. _Also_, get out of the habit of postincrementing things when you don't need the pre-incremented value. I have no idea why that is so many people's default. It can pointlessly slow down your code once you start using iterators, etc. – underscore_d Jul 14 '16 at 20:33
  • @underscore_d Nice post-increment rant. :) I 100% agree. – James Adkison Jul 14 '16 at 20:37
  • sadly its K&R's standard for for loops, we will always be stuck with it – pm100 Jul 14 '16 at 20:42

2 Answers2

0

My guess is that the problem is in your input. Are you entering capital letters or lowercase letters? Their ASCII codes are different. So, you probably want to change the code from

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

to something like

if (num >= 'a')
    num = letters - 'a';
else
    num = letters - 'A';

Also, as mentioned by @jtbandes, use the curly braces { and }. Whitespace does not determine scope in C++. Even if it's for only one line of code after your if-statement, it'll save you headaches in the future.

Glasses2C_Sharp
  • 149
  • 2
  • 8
0

Is the static cast necessary? I recommend using a string stream or just traversing the string character by character using .at() and relying on the ascii values for conversion. http://web.cs.mun.ca/~michael/c/ascii-table.html.