Home > Net >  New to c and static_cast<char>()
New to c and static_cast<char>()

Time:10-04

So I'm in college right now and I have this lab that wants me to make a code that inputs an integer and outputs a corresponding letter, I was able to get the code to work but it wants me to use static_cast() for 1-9 " Write a program that prompts the user to input an integer between 0 and 35. The prompt should say Enter an integer between 0 and 35:

If the number is less than or equal to 9, the program should output the number; otherwise, it should output:

A for 10 B for 11 C for 12 . . . and Z for 35. (Hint: For numbers >= 10, calculate the ACSII value for the corresponding letter and convert it to a char using the cast operator, static_cast().)"

and here is what I have.

 #include <iostream>

using namespace std;

int main() 
{
 int num;

 cout << "Enter an integer between 0 and 35: ";
 cin >> num;
 cout << endl;

 switch(num)
 {
   case 0:
    cout << "0";
      break;
   case 1:
    cout << "1";
      break;
   case 2:
    cout << "2";
      break;
   case 3:
    cout << "3";
      break;
   case 4:
    cout << "4";
      break; 
   case 5:
    cout << "5";
      break;
   case 6:
    cout << "6";
      break;
   case 7:
    cout << "7";
      break;
   case 8:
    cout << "8";
      break;
   case 9:
    cout << "9";
      break;
   case 10:
    cout << "A";
    break;
   case 11:
    cout << "B";
   case 12:
    cout << "C";
   case 13:
    cout << "D";
   case 14:
    cout << "E";
   case 15:
    cout << "F";
   case 16:
    cout << "G";
   case 17:
    cout << "H";
   case 18:
    cout << "I";
   case 19:
    cout << "J";
   case 20:
    cout << "K";
   case 21:
    cout << "L";
   case 22:
    cout << "M";
   case 23:
    cout << "N";
   case 24:
    cout << "O";
   case 25:
    cout << "P";
   case 26:
    cout << "Q";
   case 27:
    cout << "R";
   case 28:
    cout << "S";
   case 29:
    cout << "T";
   case 30:
    cout << "U";
   case 31:
    cout << "V";
   case 32:
    cout << "W";
   case 33:
    cout << "X";
   case 34:
    cout << "Y";
   case 35:
    cout << "Z";
 }
    // Write your main here
    return 0;
}

CodePudding user response:

Here's a quick improvement to you code. Rather than have 36 case statements, use a table.

const char table[] = "0123456789ABCDEFGHUJKLMNOPQRSTUVWXYZ";

if ((num >= 0) && (num <= 35))
{
    cout << table[num];
}

But I suspect your professor wants you to make use of the fact that chars are integers to and can be added with an offset to get another char. (e.g. 'A' 1 == 'B'). And all character literals like '4' and 'G' have ordinal values that match their values on the ascii char. (e.g. 'A' is 65 and '0' is 48)

So I think what they are getting at is this:

if ((num >= 0) && (number <= 9))
{
    int ord = '0'   num;
    cout << static_cast<char>(ord) << endl;
}
else if ((num>= 10) && (num <= 35))
{
    int ord = 'A'   num - 10;
    cout << static_cast<char>(ord) << endl;
}

Notice that I'm using character literals in single quotes ('A') instead of string literals in double quotes like "A". You can effectively "add" chars together. Math operations applied to strings have a quite different effect.

Pedantic comment on my answer. Technically, the C standard only stipulates that digit chars ('0' to '9') are aligned sequentially in ordinal values. Ancient encodings like EBCIDIC didn't have the letters in sequential order. But your prof called out "ascii", so you're in the clear.

CodePudding user response:

The ideal solution here is to build a time machine and get the ASCII developers to put 9 adjacent to A in order to simplify the logic required. :)

What you have with the big switch is unsatisfactory for the test. I recommend looking at the ASCII table and observing the decimal numbers that represent each character.

For example, 65 is 'A', 66 is 'B'. static_cast<char>(65) will output 'A'. With a little bit of math, you can manipulate the input number to add it to 65. A trick in C/C is that you can add to the character value directly, such as 'A' offset.

Try to get your resulting code as simple as possible!

  •  Tags:  
  • c
  • Related