Member Avatar for chudapati09

I need to write a program that changes a Roman Numeral to a numeric value.
I've been inputing the roman numeral "CCLXXXVII", expecting it to output "287". Instead I've been getting "7101".

Here is my code:

#include <iostream>
#include <string>

using namespace std;

const int VALUE_I = 1;
const int VALUE_V = 5;
const int VALUE_X = 10;
const int VALUE_L = 50;
const int VALUE_C = 100;
const int VALUE_D = 500;
const int VALUE_M = 1000;

void sendValue(string& , int&);

int main(){
   string romanNumeral;
   int romanNumeralValue;

   cout << "Enter a Roman Numeral: ";
   cin >> romanNumeral;

   sendValue(romanNumeral, romanNumeralValue);

   cout << endl << "Value of the Roman Numeral: " << romanNumeralValue;

return 0;
}

void sendValue(string & a, int & b){
   int stringLength = a.length();
   int count = 0;
   b = 0;
   string temp;
   
   while(count < stringLength){
      temp = a.substr(count , count + 1);
      if(temp == "I")
         b += VALUE_I; 
      else if(temp == "V")
         b += VALUE_V;
      else if(temp == "X")
         b += VALUE_X;
      else if(temp == "L")
         b += VALUE_L;
      else if(temp == "C")
         b += VALUE_C;
      else if(temp == "D")
         b += VALUE_D;
      else 
         b += VALUE_M;
      
      count++;
   }   
}

We need to use functions for this program. Here are the values I got for string temp:

When count = 0 : temp = "C"
When count = 1 : temp = "CL"
When count = 2 : temp = "LXX"
When count = 3 : temp = "XXXV"
When count = 4 : temp = "XXVII"
When count = 5 : temp = "XVII"
When count = 6 : temp = "VII"
When count = 7 : temp = "II"
When count = 8 : temp = "I"

I would loop through your string like below

#include <iostream>
#include <string>

int main()
{
  std::string the_string;
  
  std::cout << "enter your number->";
  std::cin >> the_string;
  std::cout << std::endl;
  
  
  for (unsigned int i = 0; i < the_string.size(); ++i)
  {
    //do your comparisons here
    // (the_string[i] == 'I')
    std::cout << the_string[i] << std::endl;
  }
  return 0;
}

I find it cleaner but maybe that's the C in me talking. Also note that my way uses characters in the comparison not strings.

To access a single member of a string, it's typically safer to call string::at(), as it checks that the value passed to at() is in range.

substr() seems really round-about for getting just one character.
std::string[] or std::string.at() are much better solutions for your problem

In terms of processing an integer value into a roman numeral string, it might be better to use an order of magnitude consideration (power of 10), and perhaps an (if greater than 5), since all numerals are segmented as:
1 11 111 (sub 1)5 5

Including the upper range above 5.
The only thing that changes with each magnitude is the character represented.

That may, however, have been out of the scope of what you are doing.

Best of luck!

Member Avatar for chudapati09

I would loop through your string like below

#include <iostream>
#include <string>

int main()
{
  std::string the_string;
  
  std::cout << "enter your number->";
  std::cin >> the_string;
  std::cout << std::endl;
  
  
  for (unsigned int i = 0; i < the_string.size(); ++i)
  {
    //do your comparisons here
    // (the_string[i] == 'I')
    std::cout << the_string[i] << std::endl;
  }
  return 0;
}

I find it cleaner but maybe that's the C in me talking. Also note that my way uses characters in the comparison not strings.

Thanks for your suggestion, but i forgot that we can't use array's, if "the_string[i} == 'I' is an array. Also we haven't learned to use "std::".

Member Avatar for chudapati09

To access a single member of a string, it's typically safer to call string::at(), as it checks that the value passed to at() is in range.

substr() seems really round-about for getting just one character.
std::string[] or std::string.at() are much better solutions for your problem

In terms of processing an integer value into a roman numeral string, it might be better to use an order of magnitude consideration (power of 10), and perhaps an (if greater than 5), since all numerals are segmented as:
1 11 111 (sub 1)5 5

Including the upper range above 5.
The only thing that changes with each magnitude is the character represented.

That may, however, have been out of the scope of what you are doing.

Best of luck!

Thanks I just used the .at() function to do it and it works.

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.