Write a program that does the following:

Asks the user to enter an integer
Prints the integer in Roman numerals
1.) Asks the user if they'd like to convert another integer
2.) If the user enters 'n' or 'N': end the program (return 0;)
3.) If the user enters anything else: go back to step 1

This is what I have so far but I keep getting errors, I don't get what I'm doing wrong.

#include <iostream>
#include <string>

using namespace std;

int main(){

    string answer; 
    int integer;
    int num;
    int y; 

    cout << "Please enter an integer: ";
    cin >> integer;

        cout << "Would you like to convert another integer(Y / N)?";
        while (answer == 'Y' && answer != 'N');

        if (num >= 1){
            answer += 'I';
            num -= 1;
        }
        else if (num >= 5){
            answer += 'V';
            num -= 5;
        }
        else if (num >= 10){
            answer += 'X';
            num += 10;
        }
        else if (num >= 50){
            answer += 'L';
            num -= 50;
        }
        else if (num >= 100){
            answer += 'C';
            num -= 100;
        }
        else if (num >= 500){
            answer += 'D';
            num -= 500;
        }
        else if (num >= 1000){
            answer += 'M';
            num -= 1000;
        }
    }


        system("pause");
        return 0;

This is an example of what the output should be:

Please enter an integer: 123
--> CXXIII

Would you like to convert another integer (Y/N)? y

Please enter an integer: 401
--> CDI

Would you like to convert another integer (Y/N)? y

Please enter an integer: 1299
--> MCCXCIX

Would you like to convert another integer (Y/N)? n

Recommended Answers

All 8 Replies

It would help if you told us what errors you were getting and what lines they seem to appear on.

For that matter, if you would look at your compiler error messages closely, they point you to the several things you need to fix just to get it to compile.

Line 17 - you compare strings to characters. For that matter, answer doesn't have any value yet.

Line 19 and on - num used without having any valid value.

Line 50 - system(pause)not declared (don't use it, anyway.)

Fix those things, then start clearing up any logic problems you find.

Are my while and the if statements right, or am I doing those wrong too? I don't have visual studio on my laptop so I will try to check the errors in the lab tomorrow.

Try walking through your code. You ask for a number, then you ask if the user wants to convert another number. Shouldn't that second question come at the end of the main loop? I think a do...while loop structure is called for.

Then look at how you are creating the Roman numeral. If the input is, say, 12, it passes the test at line 19, add an "I" to the answer string, subtracts one (value now 11) and then...what happens? Nothing.

So, you need a loop that keeps building the answer till there's no remaining value to convert. You also should start from the big numbers (thousands) first, working down to the units.

Using a couple of arrays as lookup tables will help shorten your code tremendously:

const string digits = "IVXLCDM";
const int number[] = {1,5,10,50,100,500,1000};
int amount[] = {0,0,0,0,0,0,0};
string ToRoman(int input)
{
    if(!input < 4000)
        return "Invalid input";
    for(int i = 6;i > -1; i-=2)
    {
        amount[i] = (int)(input/pow(10,(i/2)));
        input -= number[i] * amount[i];
        if(amount[i] > 4)
        {
            if(amount[i] < 9)
            {
                amount[i+1]++;
                amount[i]-=5;
            }
        }           
    }
    return ToString();
}
string ToString()
{
    string output = "";
    for(int i = 6;i > -1; i--)
    {
        if(amount[i] == 9)
        {
            output += digits[i];
            output += digits[i+2];
        }
        else
        if(amount[i] == 4)
        {
            output += digits[i];
            output += digits[i+1];
        }
        else
            for(int j = 0; j < amount[i]; j++)
            {
                output += digits[i];
            }
    }
    return output;
}

This calulates how many of each digit is needed then creates the string. It also handles those cases when a 4 or a 9 is needed

So this is what I have, it works but the problem is that it when
The output comes out it like this:

Please enter an integer 123
CXXIIIWould you like to convert another integer (y/n)?

I want the question after it though. How do I fix this??

#include <iostream>
using namespace std;
int main()
{
    char repeat='y';
    while (repeat=='y' || repeat=='Y')
    {
    cout << "Please enter an integer ";
    int num;
    cin >> num;
    while (num >=1000) {
        cout <<"M";
        num = num - 1000;
}
 while (num ==900) {
        cout <<"CM";
        num = num - 900;
}
 while (num >=500) {
        cout <<"D";
        num = num - 500;
}
 while (num ==400) {
        cout <<"CD";
        num = num - 400;
}
 while (num >=100) {
        cout <<"C";
        num = num - 100;
}

 while (num >=90 && num<=99) {
        cout <<"XC";
        num = num - 90;
}
 while (num >=50) {
        cout <<"L";
        num = num - 50;
}
 while (num ==40) {
        cout <<"XL";
        num = num - 40;
}
 while (num >=10) {
        cout <<"X";
        num = num - 10;
}
  while (num ==9) {
        cout <<"IX";
        num = num - 9;
}
 while (num >=5) {
        cout <<"V";
        num = num - 5;
}
 while (num ==4) {
        cout <<"IV";
        num = num - 4;
}
 while (num >=1) {
        cout <<"I";
        num = num - 1;
}
    cout<<"Would you like to convert another integer (y/n)? "<<endl;
        cin>>repeat;
    }
  system("PAUSE");
    return 0;
}

Change:

 cout<<"Would you like to convert another integer (y/n)? "<<endl;

To:

 cout<<"\nWould you like to convert another integer (y/n)? \n";

I believe your logic is flawed. For example, if I enter 1969 into your app, it will return MDCCCCLXIX. Unfortunately, that is not the correct Roman Numeral for 1969. The correct Roman Numeral is MCMLXIX.

You logic is flawed because you put == instead of >= . Consider the lines 15 -19 .

while (num ==900) {
cout <<"CM";
num = num - 900;
}

Now if number is 901, that isnt going to be executed, so you want to change the == to >= . Beyond that it now mostly works I think.

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.