//--------------------------------------------------------- // This program converts a numeral representing a non- // negative integer to an integer type value. It also // converts a non-negative integer value to a string // representation (a numeral). // // D. Searls // Asbury College // Sep. 2006 //--------------------------------------------------------- #include <iostream> #include <string> using namespace std; int main() { string numeral; int integer; char ch; int digit; int position; cout << "Enter a non-negative integer value: "; cin >> numeral; cout << endl; // Convert a numeral (a string) to an integer integer = 0; position = 0; while(position < (int)numeral.length()) { ch = numeral.at(position); digit = int(ch) - int('0'); integer = 10 * integer + digit; position = position + 1; } cout << "The integer is " << integer << endl; // Convert an integer to a numeral (a string) numeral = ""; do { digit = integer % 10; integer = integer /10; ch = char(int('0') + digit); numeral = ch + numeral; } while (integer > 0); cout << "The numeral is \"" << numeral << '\"' << endl; return 0; }