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);   
}

Recommended Answers

All 7 Replies

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.

how would I fix that? while trying to keep the string?

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

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.

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.

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.