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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
01-12-2011, 07:34 PM
|
#1
|
|
LQ Newbie
Registered: Jul 2007
Location: Michigan
Distribution: Ubuntu 8.04, Linpus Lite 10, Ubuntu 10.10
Posts: 6
Rep:
|
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;
}
|
|
|
|
01-12-2011, 08:50 PM
|
#2
|
|
Senior Member
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,383
|
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.
|
|
|
|
02-24-2011, 09:11 AM
|
#3
|
|
LQ Newbie
Registered: Feb 2011
Posts: 1
Rep:
|
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.
|
02-24-2011, 09:35 AM
|
#4
|
|
Senior Member
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,383
|
Quote:
Originally Posted by moneya
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.
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 04:11 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|