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

I messed around with it a little this morning and got this for number one

#include <iostream>

using namespace std;

int main()
{

double num;
int intnum, m, d, c, l, x, v, i, n;



cout << "Enter a number:";
cin >> num;
intnum = (int)num;

m = intnum / 1000;
d = ((intnum%1000)/500);
c = ((intnum%500)/100);
l = ((intnum%100)/50);
x = ((intnum%50)/10);
v = ((intnum%10)/5);
i = (intnum%5);
n = m+d+c+l+x+v+i;



while (n > 0)
{
      cout << "!";// to show the loop ran everytime
      
      if (m > 0)
      {
      cout << "M";
      m--;
      n--;
      }

      else if (d > 0)
      {
      cout << "D";
      d--;
      n--;
      }
      else if (c > 0)
      {
      cout << "C";
      c--;
      n--;
      }
      else if (l > 0)
      {
      cout << "L";
      l--;
      n--;
      }
      else if (x > 0)
      {
      cout << "X";
      x--;
      n--;
      }
     else if (v > 0)
      {
      cout << "V";
      v--;
      n--;
      }
     else if (i > 0)
      {
      cout << "I";
      i--;
      n--;
      }

}


system("pause");
return 0;
}

Enter a number:5678
!M!M!M!M!M!D!C!L!X!X!V!I!I!I
Press any key to continue . . .

That is running the while loop 7 times like the directions say.

I then tried to do number 2. But the loop is completing everything in 1 run.

//Eric Sanders
//9/16/2011
//CS142-02
//Roman Numeral Project

#include <iostream>

using namespace std;

int main()
{

double num;
int intnum, m, d, c, l, x, v, i, n;



cout << "Enter a number:";
cin >> num;
intnum = (int)num;

m = intnum / 1000;
d = ((intnum%1000)/500);
c = ((intnum%500)/100);
l = ((intnum%100)/50);
x = ((intnum%50)/10);
v = ((intnum%10)/5);
i = (intnum%5);
n = m+d+c+l+x+v+i;


while (n > 0)
{
      cout << "!"; // to show the loop running
      {
      for (m; m>0; m--)
		cout << "M";
      }
      {
      for (d; d>0; d--)
		cout << "D";
      }
            {
      for (c; c>0; c--)
		cout << "C";
      }
      {
      for (l; l>0; l--)
		cout << "L";
      }
      {
      for (x; x>0; x--)
		cout << "X";
      }
      {
      for (v; v>0; v--)
		cout << "V";
      }
      {
      for (i; i>0; i--)
		cout << "I";
      }
      n--;


}


system("pause");
return 0;
}

Output

Enter a number:5678
!MMMMMDCLXXVIII!!!!!!!!!!!!!
Press any key to continue . . .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.