Here is a task out of a book
Your task is to write a C++ program to help you convert a number into roman numerals. Roman numerals are I for 1, V for 5, X for 10, L for 50, C for 100, D for 500, and M for 1000. Some numbers are formed by using a subtraction of Roman “digits”; for example, IV is 4, since V minus I is 4. Others are IX
for 9, XL for 40, XC for 90, CD for 400, and CM for 900. Your program will proceed in three parts:1. Write a program that reads in a decimal number from the screen (using cin) and prints out
a simple Roman numeral. A simple Roman numeral does not use the subtraction rule. For
example, the simple Roman numeral for the number 493 will be CCCCLXXXXIII, and the simple
Roman numeral for the number 3896 will be MMMDCCCLXXXXVI. Your program should print the
entire Roman numeral on a single line, with no spaces between letters. You will need a while
loop in your program to print out the letters one at a time. Hint: Look at your minutes
program for ideas on how figure out which letter to print next.2. Modify your program so that you print out all of the repeated letters in a single iteration
of your while loop. You will need to put a few for loops inside your while loop to do this
part. For example, if the Roman numeral you have to print out is MMMMXXX, the first part
iterated through your while loop seven times, one for each letter you were printing out. Now,
you want to iterate your while loop just twice, one for each kind of Roman numeral being
printed. Thus, the first iteration will print out MMMM, and the second will print out XXX. Your
final output will be MMMMXXX on a single line.3. Modify your program to also handle subtractions. So now, 493 should give the answer CDXCIII,and 3896 should give the answer MMMDCCCXCVI.
I got the program to work with this code
#include <iostream>
using namespace std;
int main()
{
double num;
int intnum, m, d, c, l, x, v, i, n;
char yes ='y';
while (yes == 'y')
{
cout << "Enter a number: ";
cin >> num;
intnum = (int)num;
if (intnum >= 1000)
{
m = intnum / 1000;
n = 0;
{
for (n; n < m; n++)
cout << "M";
}
intnum = intnum%1000;
}
if (intnum >= 900)
{
cout << "CM";
intnum = intnum%900;
}
else if (intnum >= 500)
{
{
d = intnum / 500;
n = 0;
for (n; n < d; n++)
cout << "D";
}
intnum = intnum%500;
}
if (intnum >= 400)
{
cout << "CD";
intnum = intnum%400;
}
else if (intnum >= 100)
{
{
c = intnum / 100;
n = 0;
for (n; n < c; n++)
cout << "C";
}
intnum = intnum%100;
}
if (intnum >= 90)
{
cout << "XC";
intnum = intnum%90;
}
else if (intnum >= 50)
{
{
l = intnum / 50;
n = 0;
for (n; n < l; n++)
cout << "L";
}
intnum = intnum%50;
}
if (intnum >= 40)
{
cout << "XL";
intnum = intnum%40;
}
else if (intnum >= 10)
{
{
x = intnum / 10;
n = 0;
for (n; n < x; n++)
cout << "X";
}
intnum = intnum%10;
}
if (intnum >= 9)
{
cout << "IX";
intnum = intnum%9;
}
else if (intnum >= 5)
{
{
v = intnum / 5;
n = 0;
for (n; n < v; n++)
cout << "V";
}
intnum = intnum%5;
}
if (intnum >= 4)
{
cout << "IV";
intnum = intnum%4;
}
else if (intnum >= 1)
{
i = intnum;
n = 0;
for (n; n < i; n++)
cout << "I";
}
cout << "\nWould you like to run this program again? (y/n): ";
cin >> yes;
cout << endl;
}
return 0;
}
However, I don't quite understand the procedure that the directions tell me to do. For instance, number one says:
"You will need a while loop in your program to print out the letters one at a time."
Don't I need more than one like I do?
I also don't know how to approach number 2.
As you can tell, I don't use a while loop to print out the roman numerals. So I'm not following the directions.
Can anyone help clarify some of this for me?
Thanks