Hello, I'm doing a homework assignment from my Computer Science course and I'm having a bit of trouble.

so far I have the code below and its producing errors, can anyone help? thanks!

#include <iostream>
#include <string>
using namespace std;

int main()
{
    char ans;
    int num;
    do
     {
      string intToRoman(int num); 
        {
            
            string roman;
            int h,t,o;
            static const char *ones[] = {"","I","II","III","IV","V","VI","VII","VIII","IX"};
            static const char *tens[] = {"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};
            static const char *hundreds[] = {"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"};
            static const char *thousands[] = {"","M","MM","MMM","MMMM","MMMMM"};

       
   
             if (num <= 3000 || num >= 1000) 
              {
                    th = num / 1000;
                    num = num % 1000; 
  
                    h = num % 100;
                    num = num % 100; 

                    t = num / 10;
                    o = num % 10;

                     roman = roman + thousands[th] + hundreds[h] + tens[t] + ones[o];
            
            cout << "The year you have entered converts to: " << roman << endl << endl;
            break;
            }

            else
             {
              roman = "Please enter a number between 1000-3000\n";
             
             }
       }     
    
    cout << "Would you like to use this program again?\n";
    cout << "If so, press y. If not, press another key\n";
    cout << "followed by the enter key." << endl << endl;
    cin >> ans;
    
    
    }while(ans == 'y' || ans == 'Y');
    
    


    
    
          system("pause");
    return (0);   
}

Edited 5 Years Ago by Narue: added code tags

Assuming that the error is something like:

error: ‘th’ was not declared in this scope

the error is that you're trying to use a variable, th, that you never created.

The line in question is this:

th = num / 1000;

The problem is that th does not exist. You can make it exist like this:

int th;

Alternatively, correct the spelling of it; where you wrote th, you meant to write t

Edited 5 Years Ago by Moschops: n/a

Oooh, I see it now, thank you! There seems to be something wrong with my code that doesn't produce the effects I want it too, anyone have any idea? When I put in 1549, the program crashes

The program crashes because you did not initialize the variable "num".

And I also noticed that in the 23rd line, the logical operators you used is wrong. In this case, instead of using ||, you should be using &&. So if the number is less than or equal to 3000 AND more than or equal to 1000, the code in the scope executes.

Also, there's an error to your algorithm. Look at the 28th line of the code. It should be h = num / 100, not h = num % 100.

Edited 5 Years Ago by Cuzzie: n/a

Sorry, but regardless of the errors in your code you have the wrong algorithm.

You seem to have assumed that you can translate directly from base 10 decimal numbers, you cant. Eg. Consider 49. That would be written IL. however, your code
would produce XL1X.

So I would suggest that you have a go at creating another algorithm. Try it out on paper first, that will greatly help when you come to coding it up.

Sorry, but regardless of the errors in your code you have the wrong algorithm.

As do you.

You seem to have assumed that you can translate directly from base 10 decimal numbers, you cant. Eg. Consider 49. That would be written IL. however, your code
would produce XL1X.

Whereas the 'more correct' correct answer is XLIX.
I can negatively modify V & X
X can negatively modify L & C

Using your algorithm, 999 would be IM, which is not normal.

caveat: there are arguments for both techniques since there is no real standardization. But a fairly common interpretation is as above in lieu of a documented standard.

This article has been dead for over six months. Start a new discussion instead.