LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-12-2011, 07:34 PM   #1
ACSUSA
LQ Newbie
 
Registered: Jul 2007
Location: Michigan
Distribution: Ubuntu 8.04, Linpus Lite 10, Ubuntu 10.10
Posts: 6

Rep: Reputation: 0
Question 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;

}
 
Old 01-12-2011, 08:50 PM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,522

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
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.

Last edited by dwhitney67; 01-12-2011 at 09:09 PM.
 
Old 02-24-2011, 09:11 AM   #3
moneya
LQ Newbie
 
Registered: Feb 2011
Posts: 1

Rep: Reputation: 0
Talking

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!
 
0 members found this post helpful.
Old 02-24-2011, 09:35 AM   #4
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,522

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
Quote:
Originally Posted by moneya View Post
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.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Remember your Roman numerals? Jeebizz General 13 09-29-2006 05:47 PM
'ls' .. or something to identify filenames that have a (space)(numeral) pattern rickh Programming 7 05-09-2006 01:51 AM
Times New Roman for Firefox? apachedude Suse/Novell 0 11-30-2004 09:13 PM
Times New Roman-like fonts jrdioko Linux - Newbie 6 09-12-2004 07:52 PM
Displaying Non-Roman characters with Apache Inexactitude Linux - Networking 1 05-24-2004 05:54 PM


All times are GMT -5. The time now is 03:30 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration