Please help!

Discussion in 'Software' started by Ryy75, Sep 7, 2004.

  1. Ryy75

    Ryy75 Private E-2

    I am new to proagramming and I would appreaciate any help I get can get from you guys. My assignment is to write a program in c++ that converts roman numerals to arabic numerals. First thing I did is ensure that only the letters IVXLCDM are used. And now I need to write a function that decides if the the letters sould if added (ie. VI = 6) or subtracted (ie. IV = 4). I can't seem find a way to do that!! I'm posting the first function of my program. Thank you.


    #include <iostream>
    #include <string>
    #include <cstdlib>

    using namespace std;

    string Get_valid_input(string& romanNum);



    int main()
    {
    string romanNum;

    Get_valid_input(romanNum);


    return 0;
    }



    string Get_valid_input(string& romanNum)
    {


    int len, n = 0;
    bool valid = true;


    cin >> romanNum;

    len = romanNum.length(); // Finds lenght of the user input

    cout << len;



    while ((n < len) && (valid == true))
    {

    if ((romanNum.at(n) == 'm') || (romanNum.at(n) == 'd') || (romanNum.at(n) == 'c')
    || (romanNum.at(n) == 'l') || (romanNum.at(n) == 'x') || (romanNum.at(n) == 'v')
    || (romanNum.at(n) == 'i'))

    valid = true;


    else

    valid = false;

    n++;
    }

    cout << valid;

    return romanNum;
    }
     
  2. Wookie

    Wookie Sergeant Major

    you would need something like


    if romanNum = "IV" then {
    do addition
    }


    dunno if the syntax is correct but you should get the jist of it.
     
  3. GregoryDalton

    GregoryDalton Private E-2

    I dont have a compiler to hand so cant test waht I am going to suggest and also I am a little unclear on the in's and outs of Roman Numerals.

    I thought the rule was that you subtracted if the number you are looking at was less than the number that proceeds it... Roman Numerals are almost presented in hundreds, tens, units etc... but if a ten preceeds a hundred then you subtract the ten from the hundred... again, if memory serves.
    To implement this I would first of all find out a definition of the exact rules followed for translating roman numerals to arabic letters are. These can then be followed in an algorithm that should produce the results you want.
    However inside the code, I think if I were to implement this I would parse what is entered into an array of integers.
    So.... (as used to follow any program on BBC back in the 90's, something like...)
    "MCMXCVIX"
    This would get parsed to an array of integers consisting of
    array[0]=1000;
    array[1]=100;
    array[2]=1000;
    array[3]=10;
    array[4]=100;
    array[5]=5;
    array[6]=1;
    array[7]=10;

    I would then loop the contents of the array following the rules. So look at array[0]... is that bigger than array[1]? Yes it is. So add array[0] to a increment....

    inc=0;
    inc += array[0];

    look at array[1]. Is it bigger than array2? No. So lets subtract array[1] from array[2] and add it to the total.

    inc += (array[3] - array[2]);

    Then look at array[4]... be careful not to examine array 3 again! Of course as I stated above Im not sure the exact rules for converting roman numerals and the above may be incorrect - but you hopefully get the idea of how I would at least do it...

    errrr I hope... :)

    Cheers,
    Greg

    PS - as a test MCMXCIX should return 1999.
     
  4. rmStar-R

    rmStar-R Private E-2

     
  5. glennk721

    glennk721 MajorGeek

    Inquiroring minds want to know,,,, lol Are we doing your HOMEWORK LOL :) :) :) J/K
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds