Integer to ASCII code

  • Question:
  • Write a program that takes an integer input and prints out the character with the corresponding ASCII code. Please make your program user-firendly and robust.

  • My solution:
#include <iostream>
#define RANGE 127

using namespace std;

bool checkRange(int n)
{
     if(n == -1)
         return true;

     if(n < 0 || n > RANGE)
     {
         cout << "Your input is not within the range !!!" << endl;
         cout << "Please enter a number between 0 to 127 (enter -1 to quit):";
         return false;
     }

     return true;
}

int main()
{
    int num;
    char c;

    while(true)
    {
        cout << "Please enter a number between 0 to 127 (enter -1 to quit):";
        cin >> num;

        while(!checkRange(num))
        {
            cin >> num;
        }

        if(num == -1)
            break;

        c = num;
        cout << c << endl;
    }

    system("PAUSE");
    return 0;
}

One Response

  1. mmmm

    int main()
    {
    int num;

    while(true)
    {
    do{
    cout <> num;
    }while(!checkRange(num))

    if(num == -1)
    break;

    cout << (char)c << endl;
    }

    system(“PAUSE”);
    return 0;
    }

Leave a Reply