LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   C++ Roman Numeral Converter (http://www.linuxquestions.org/questions/programming-9/c-roman-numeral-converter-855966/)

ACSUSA 01-12-2011 08:34 PM

C++ Roman Numeral Converter
 
I am trying to finish up my intro C++ course, and have to make a Roman numeral to Arabic converter. I had no trouble with the reverse (Arabic to Roman), but I must be missing something on this one. I tried a few variations, and either got 0 or an insanely high number as the output.

Code:

#include<iostream>
#include<cstring>

using namespace std;



int main() {

    char roman[20];

    int arabic, x, y;

    cout << "Roman Numeral to Arabic Number Converter" << endl;

    cout << "Please enter a Roman Numeral to convert: ";

    cin.get(roman, 20);

    cin.ignore(80, '\n');

   

    x = strlen(roman);

   

    for(y = 1;y == x; y++) {

            if(roman[y] == 'M') {

                    arabic = arabic + 1000;

            }

            if(roman[y] == 'D') {

                    arabic = arabic + 500;

            }

            if(roman[y] == 'C') {

                    arabic = arabic + 100;

            }

            if(roman[y] == 'L') {

                    arabic = arabic + 50;

            }

            if(roman[y] == 'X') {

                    arabic = arabic + 10;

            }

            if(roman[y] == 'V') {

                    arabic = arabic + 5;

            }

            if(roman[y] == 'I') {

                    arabic = arabic + 1;

            }

    }

   

    cout << "You entered " << x << " character(s)." << endl;
    cout << "Your arabic number is: " << arabic << endl;

   

    return 0;

}


dwhitney67 01-12-2011 09:50 PM

Here's a hint... initialize the variable 'arabic'.

Another... fix the for-loop's initial state and conditional statement.

Also, "IV" is 4, not 6 as your program will compute. It would seem that you have more work ahead.

moneya 02-24-2011 10:11 AM

Quote:

I am trying to finish up my intro C++ course, and have to make a Roman numeral to Arabic converter. I had no trouble with the reverse (Arabic to Roman), but I must be missing something on this one. I tried a few variations, and either got 0 or an insanely high number as the output.
LOL i found this thread in a search i did trying to find the answer to the same question, as i have intro to C++ as an online class thanks!

dwhitney67 02-24-2011 10:35 AM

Quote:

Originally Posted by moneya (Post 4269694)
LOL i found this thread in a search i did trying to find the answer to the same question, as i have intro to C++ as an online class thanks!

I think that the point of your exercise is to employ the use of independent thought versus trying to locate an answer on the web.

We've all heard the argument against "reinventing the wheel", however that notion surely does not apply to students.


All times are GMT -5. The time now is 06:06 AM.