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;
}
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: