1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
string convert(int digit, string low, string mid, string high);
void saveToFile(int &, string[], const int &);
int main()
{
const int MAX_INPUT = 3999, MIN_INPUT = 1, // These constants hold high and low integer numbers,
ARRAY_SIZE = 4; // and the array size declarator.
string answers[ARRAY_SIZE] = { "", "", "", "" }; //An array of string to hold the output from the convert function.
int accumulator = 0; // Variable to hold number of arabic numbers converted.
int userNum = 0; // Variable to hold user input.
saveToFile(userNum, answers, ARRAY_SIZE);
do { //Main loop - ensures repeated execution until negative entered.
cout << "Enter a negative number to end the program.\n";
cout << "Enter an arabic number between 1 and 3999: ";
accumulator++;
while (!(cin >> userNum) || (userNum < MIN_INPUT || userNum > MAX_INPUT)){ //input validation - only proceed with
if (userNum < 0) //valid, in-range input.
{
cout << "Program Ending due to user request.";
cout << endl << "Arabic numbers converted: " << accumulator - 1 << endl; //Counter
cout << "Thank you for using the program. Have a nice day!" << endl;
system("PAUSE");
exit(EXIT_SUCCESS); //Termintaion with message and exit function
}
else {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n'); //handling of non-integer input.
cout << "Invalid Value. Number must be between 1 and 3999: ";
}
}
// Digit Extraction - turns userNum into four seperate values
int thous = userNum / 1000; //thousands place value
int hund = userNum % 1000 / 100; //hundreds place value
int tens = userNum % 100 / 10; //tens place value
int ones = userNum % 10 / 1; //ones place value
// filling answers ARRAY OF STRINGS with results from convert function.
answers[0] = convert(thous, "M", "M", "M");
answers[1] = convert(hund, "C", "D", "M");
answers[2] = convert(tens, "X", "L", "C");
answers[3] = convert(ones, "I", "V", "X");
// Printing out equivelent roman numeral on one line.
cout << "\nRoman numeral for " << userNum << " is: ";
cout << answers[0] << answers[1] << answers[2];
cout << answers[3] << endl;
} while (userNum > 0); //Loop to allow multiple numbers per run.
system("PAUSE");
return 0;
}
// Convert function - returns a string for roman numerals broken up by digits, Accepts as arguments
// the extracted digits and three string values known as low, med, and high.
string convert(int digit, string low, string mid, string high)
{
if (digit == 1)
{
return low;
}
if (digit == 2)
{
return low + low;
}
if (digit == 3)
{
return low + low + low;
}
if (digit == 4)
{
return low + mid;
}
if (digit == 5)
{
return mid;
}
if (digit == 6)
{
return mid + low;
}
if (digit == 7)
{
return mid + low + low;
}
if (digit == 8)
{
return mid + low + low + low;
}
if (digit == 9)
{
return low + high;
}
if (digit == 0)
{
return "";
}
}
void saveToFile(int &userNum, string answers[], const int &ARRAY_SIZE)
{
char writeToFile;
cout << "Do you want to write output to a file? Y/N ";
cin >> writeToFile;
if (writeToFile == 89 || writeToFile == 121)
{
ofstream outputFile;
outputFile.open("output.txt");
if (outputFile)
{
outputFile << userNum << ":" << answers[0] + answers[1] + answers[2] + answers[3];
outputFile.close();
}
else
{
cout << "Error opening the file.\n";
exit(EXIT_FAILURE);
}
}
else
return;
}
|