-1

for class I have to create a code so that it could ask the user for an integer between 1-99 and be able to print that integer in roman numerals. Issue is after creating my code it would only print the numbers fully up to 39. Once it hits 40 it give's no Roman Numeral output and then from 41-99 it wont print the tenth place value(Ex. 45 will come out as V). Here's my code at the moment.

    #include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

int main()
{
    int num;
    int tenum;
    int remnum;
    string romnum;
    string fromnum;

    cout << "Enter a number between 1 and 99. \n";
    cin >> num;

    tenum = num / 10;
    remnum = num % 10;

    if (tenum == 9)
    {
        fromnum == "XC";
    }
    else if (tenum == 8)
    {
        fromnum == "LXXX";
    }
    else if (tenum == 7)
    {
        fromnum == "LXX";
    }
    else if (tenum == 6)
    {
        fromnum == "LX";
    }
    else if (tenum == 5)
    {
        fromnum == "L";
    }
    else if (tenum == 4)
    {
        fromnum == "XL";
    }
    else if (tenum == 3)
    {
        fromnum = "XXX";
    }
    else if (tenum == 2)
    {
        fromnum == "XX";
    }
    else if (tenum == 1)
    {
        fromnum == "X";
    }

    if (remnum == 9)
    {
        romnum = "IX";
    }
    else if (remnum == 8)
    {
        romnum = "VIII";
    }
    else if (remnum == 7)
    {
        romnum = "VII";
    }
    else if (remnum == 6)
    {
        romnum = "VI";
    }
    else if (remnum == 5)
    {
        romnum = "V";
    }
    else if (remnum == 4)
    {
        romnum = "IV";
    }
    else if (remnum == 3)
    {
        romnum = "III";
    }
    else if (remnum == 2)
    {
        romnum = "II";
    }
    else if (remnum == 1)
    {
        romnum = "I";
    }



    cout << tenum << endl;
    cout << remnum << endl;
    cout << fromnum;
    cout << romnum << endl;

    return 0;


}
jess
  • 13
  • 1
  • 7
  • 2
    Welcome to Stack Overflow! It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: [How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – NathanOliver Jun 23 '17 at 12:33
  • Has your class covered loops and `switch` yet? – Ken White Jun 23 '17 at 12:40
  • Possible duplicate of [Converting integer to Roman Numeral](https://stackoverflow.com/questions/19266018/converting-integer-to-roman-numeral) – Chris Jun 23 '17 at 12:41
  • vtc as this is a trivial typo. – Martin Bonner supports Monica Jun 23 '17 at 12:52
  • Help yourself. Break it out into a function. The actual programming is the point of the exercise. – Malcolm McLean Jun 23 '17 at 12:53
  • If they have covered functions, a function like `std::string RomanDigit(int value, char unit, char five, char ten)` would halve the repetion (and would make the extension to 1-1399 trivial - beyond that requires Unicode which is *much* more complicated). – Martin Bonner supports Monica Jun 23 '17 at 13:01
  • You should split your code. I'd propose that you separate it into one functions that governs the whole process, `string roman_number(const unsigned int input);` and two functions for the remainder and the tens. Also helps with the debugging, you can test it by a for loop that iterates through the legal inputs for the sub functions and print the result. On the long run, you will probably want to go with a `class RomanNumber`. – Aziuth Jun 23 '17 at 13:44

2 Answers2

2

You've mixed up == and =

change lines like

fromnum == "XL"

to

fromnum = "XL"

Demo

(I used a switch statement for brevity)

AndyG
  • 39,700
  • 8
  • 109
  • 143
0

same answer with: Converting integer to Roman Numeral

#include <iostream>
#include <string>

std::string to_roman(int value)
{
  struct romandata_t { int value; char const* numeral; };
  static romandata_t const romandata[] =
     { 1000, "M",
        900, "CM",
        500, "D",
        400, "CD",
        100, "C",
         90, "XC",
         50, "L",
         40, "XL",
         10, "X",
          9, "IX",
          5, "V",
          4, "IV",
          1, "I",
          0, NULL }; // end marker

  std::string result;
  for (romandata_t const* current = romandata; current->value > 0; ++current)
  {
    while (value >= current->value)
    {
      result += current->numeral;
      value  -= current->value;
    }
  }
  return result;
}

int main()
{
  for (int i = 1; i <= 4000; ++i)
  {
    std::cout << to_roman(i) << std::endl;
  }
}

this code converts up to 1000. just take what you need and you are good to go! from http://rosettacode.org/wiki/Roman_numerals/Encode#C.2B.2B

sygon
  • 1