Skip to content

Instantly share code, notes, and snippets.

@EitherRock
Created November 26, 2018 03:33
Show Gist options
  • Save EitherRock/97bd794a89370ddb66e79d686c9a70c6 to your computer and use it in GitHub Desktop.
Save EitherRock/97bd794a89370ddb66e79d686c9a70c6 to your computer and use it in GitHub Desktop.
#include "pch.h"
#include <cstring>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
std::string toLongRoman(int value);
std::string toLower(std::string intputString);
double getInput(std::string prompt, int min, int max);
const int SENTINEL = 0;
int main()
{
double numberRow[35]{ 0 };
std::string roman[35][2]{ " " };
double userNumber = getInput(
"Please enter a number 1-5000 >",
0,
5000);
while (userNumber != SENTINEL)
{
std::cout << "Number" << std::setw(5) << " " << "Roman" << std::setw(16) << " " << "Lowercase" << std::endl;
for (int indexRows = 0; indexRows < 20; indexRows++)
{
numberRow[indexRows] = userNumber;
roman[indexRows][0] = toLongRoman(userNumber);
roman[indexRows][1] = toLower(roman[indexRows][0]);
std::cout << std::setw(10) << std::left << numberRow[indexRows] << std::setw(20) << roman[indexRows][0] <<
" " << std::setw(10) << roman[indexRows][1] << std::endl;
userNumber++;
}
double userNumber = getInput(
"Please enter a number 1-5000 >",
0,
5000);
}
std::cout << "Goodbye!";
return 0;
}
std::string toLongRoman(int value)
{
int amountOfLetters = 0;
std::string romanNumerals = " ";
// arrays for roman numeral numbers and letters
int numbers[7]{ 1000, 500, 100, 50, 10, 5, 1 };
char letters[7]{ 'M','D','C','L','X','V','I' };
// since there is 7 numbers and letters, this will loop 7 times
for (int index = 0; index < 7; index++)
{ // checks to see if number is greater or equal to roman number
// if less then it will go to loop 2
if (value >= numbers[index])
{// divides value by the roman number
amountOfLetters = (value / numbers[index]);
// sets the number divided as the loop limit
for (int count = 0; count < amountOfLetters; count++)
{// adds as many roman numerals as needed to romanNumerals variable
romanNumerals += letters[index];
}
value %= numbers[index];
}
}
return romanNumerals;
}
std::string toLower(std::string inputString)
{
// takes the length of inputString and assigns the number to length
size_t length = inputString.length();
// uses length to set how many loops will be needed
for (int index = 0; index < length; index++)
{ // if theres a capital letter in the string then it will be changed
// to the lower case version of the letter
if ((inputString[index] >= 'A') && (inputString[index] <= 'Z'))
{
inputString[index] = inputString[index] + 32;
}
}
return inputString;
}
double getInput(std::string prompt, int min, int max) {
// displays desired prompt and gets user input for number
// between min and max numbers.
// if entered incorrectly, user will try again until correct input
std::cout << prompt;
double number = 0;
std::cin >> number;
while (std::cin.fail() || number < min || number > max) {
std::cin.clear();
std::cin.ignore(2000, '\n');
std::cout << "Invalid entry, try again. ";
std::cin >> number;
}
return number;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment