Roman numerals converter - my first program

Ask for help with your homework/assignments in this forum!

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

Roman numerals converter - my first program

Postby aryo_k » Tue Mar 31, 2009 10:22 am

hi guys :mrgreen:

this is my first program that actually can do something. :oops:

im a freshman in university and my course is software engineering and game programming.

our textbook for computer programming subject is " problem solving with c++ " by walter savitch and this is one of the book's earliest projects.

this program is a ROMAN NUMERAL CONVERTER. :D

i would love to see your comments and suggestions about everything, even the format and style of the text.

im a beginner and i dont want to get used to do things in c++ in a wrong way, because that will be hard to change in future. so i need your help and your comments.

THANKS A LOT FOR YOUR TIME
Attachments
Roman numerals.zip
(2 KiB) Downloaded 567 times
aryo_k
 
Posts: 2
Joined: Tue Mar 31, 2009 10:15 am

Re: Roman numerals converter - my first program

Postby ventsyv » Tue Mar 31, 2009 10:34 am

Ok, think how would you do the conversion by hand. You know your roman numerals right ?

I - 1
II - 2
IV - 4
V - 5
VI - 6
X - 10

L - 50
LX - 60
XL - 40

C - 100
D - 500
M - 1000
More info : http://www.novaroma.org/via_romana/numbers.html

So read the number in arabic format , to convert you need to use the highest possible numbers. If the number is 2009, you should use the thousandns first : MM - 2000 so MMIX is 2009. Do a few of those by hand to make sure you know how. Check your answers at the website I provided; there is a converter tool there.
Last edited by ventsyv on Tue Mar 31, 2009 10:37 am, edited 1 time in total.
User avatar
ventsyv
 
Posts: 2828
Joined: Mon Sep 22, 2003 5:25 pm
Location: MD USA

Re: Roman numerals converter - my first program

Postby aryo_k » Tue Mar 31, 2009 10:37 am

hi

thanks for your reply,

actually i have solved the problem and i have written the program, it is attached to my post.

thanks again.
aryo_k
 
Posts: 2
Joined: Tue Mar 31, 2009 10:15 am

Re: Roman numerals converter - my first program

Postby exomo » Sat Apr 04, 2009 2:13 am

1. All your variable names start _. I think this is quite unusual. Why not just call your variables input_value, thousands, ...

2. You use endl very often. There is no need to flush the output buffer (this is what endl does) in the middle of a text block. I would output \n instead.

3. Reading numbers with cin >> causes your program to run into an endless loop if user types a non-numerical value. For your exercise programs this is not a big problem, but in a "real" application you should allways handle wrong input correctly.
Who needs a signature anyway.
User avatar
exomo
 
Posts: 935
Joined: Fri Sep 26, 2003 12:30 pm
Location: germany->baden

Re: Roman numerals converter - my first program

Postby Alvaro » Sat Apr 04, 2009 5:40 am

I would write the transformation of a number into its Roman numeral representation in a separate function. For instance, mine would look like this:
Code: Select all
#include <string>

std::string roman_representation(int n) {
  if(n>3999999 || n<0)
    return "<< OUT OF RANGE >>";
  if(n==0)
    return "N";
  static std::string const number_table[4][10] = {
    {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"},
    {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"},
    {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"},
    {"", "M", "MM", "MMM"}
  };
 
  int place,power_of_10;
  for(place=0,power_of_10=10; power_of_10<=n; power_of_10*=10)
    ++place;
 
  std::string bars, number;
  for(;power_of_10/=10,place>=0;--place) {
    int digit = (n/power_of_10)%10;
    if(place>3 || place==3 && digit>3) {
      std::string roman_digit = number_table[place-3][digit];
      number += roman_digit;
      bars += std::string(roman_digit.length(),'_');
    }
    else
      number += number_table[place][digit];
  }
 
  return bars+'\n'+number;
}

User avatar
Alvaro
Moderator
 
Posts: 5185
Joined: Mon Sep 22, 2003 4:57 pm
Location: NY, USA


Return to Homeworks

Who is online

Users browsing this forum: No registered users and 1 guest