/* * ----------------------------------------------------- * CS2S03/SE2S03, October 2011 * Assignment 3, Question 3 * File: PE0510.cpp * ----------------------------------------------------- * This program takes an integer and displays its string * representation. * Do not enter the most negative int. * ----------------------------------------------------- */ #include "genlib.h" #include "simpio.h" #include /* Private function prototype */ string IntToString(int n); /* Main program */ int main() { int n; cout << "Enter an integer to convert to a string: "; n = GetInteger(); cout << "The string representation of " << n << " is: "; cout << IntToString(n) << endl; return 0; } /* * Function: IntToString * Usage: s = IntToString(n) * ---------------------------------------------------- * This function takes an integer and returns it string * representation. * Bug: Do not use when n is the most negative int */ string IntToString(int n) { if (n < 0) return '-' + IntToString(-n); if (n < 10) return string("") + char(n + '0'); else return IntToString(n / 10) + char(n % 10 + '0'); }