C++ help-- alphabet, static cast, and the 'if'

Trio

New Member
So during class I did the wrong excersise, but it was a really good excersise and kept on working on it.

The excersise wants the user to input a number. If it's lower than 9, the number will be displayed. If it's greater than 10 and below 35, a letter will appear (10=A, 11=B, 12=C...). I'm asked to use static_cast to find the letters.

So I figured out a lot of it, but I'm stuck on converting the number into the proper alphabet letter; I keep getting 'A'. What I did was subtract char 'A' from 10, and used that to add it to whichever number is above 10 and lower 35 to find that number's alphabet letter. For example:

static_cast<int>('A') = 65 //finds the ASCII collating number of 'A'
65-10=55
55+10=65
static_cast<char>(65) = 'A'

This will find 'A' for number 10. Here's another example using 11:

static_cast<int>('A') = 65
65-10=55
55+11=66
static_cast<char>(66) = 'B'

The ASCII collating sequence for capital alphabet letters go from 65-94, so a 65 is A, 66 is B, 67 is C, etc. However, I keep getting 'A'. Can someone take a look at my code and tell me what's wrong? Thanks in advance. Variable number holds the number that's inputted, and alphabet holds the algebraic value to find the alphabet for numbers higher than 10 and below 35.



/*This program will display a number lower than 10, otherwise it will show the
alphabet for that number (10=A, 11=B, 12=C...)*/
#include <iostream>

using namespace std;

int main()
{
int number, alphabet;
cout<<"Enter a whole number between 1 and 35. If it's lower"
<<"than 9, the number will be displayed, otherwise an"
<<"alphabet will be shown."<<endl;
cin>>number;
if (number<=9)
cout<<number;
else
if (number>=10 && number<=35)
{
alphabet= static_cast<int>('A')-number;
alphabet+=number;
cout<<static_cast<char>(alphabet);
}
cin.get(); cin.get();
return 0;
}
 
Last edited:
FIXED!!

I had to change 'number' to '10' in this part:

alphabet= static_cast<int>('A')-number;
 
It's good that you fixed it...do you understand why your first way was wrong?

Yeah. It's because I was using the actual number instead of 10, and I had to use 10 to find the difference between the collated sequence and the numbers that are above 10. Because I used the number before instead of 10, the difference "stayed the same", so I kept getting the same char over and over again.

It's my first C++ class, and I hadn't learned C++ before because I was having issues with the compiler. So a lot of the code I'm practing is beginner stuff. I can't imagine how complex advance C++ will be, lol.
 
It's my first C++ class, and I hadn't learned C++ before because I was having issues with the compiler. So a lot of the code I'm practing is beginner stuff. I can't imagine how complex advance C++ will be, lol.
It can get pretty ugly and you'll make more mistakes but as long as you understand why something is wrong you should be fine. It's bad if you just keep changing it until it works but don't know why.
 
Back
Top