There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
access audio avg avg 8 bios blue screen boot bsod computer connection cpu crash css dell desktop dma driver drivers dvd email error excel explorer firefox firefox 3 freeze gimp graphics hard drive hardware hijackthis hjt install internet internet explorer itunes keyboard laptop macro malware monitor motherboard network networking outlook outlook 2003 outlook 2007 outlook express pio problem problems router seo server slow sound sp3 spyware trojan usb video virtumonde virus vista vundo windows windows vista windows xp winxp wireless
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
C++ Help


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

 
Thread Tools
Rhino24's Avatar
Member with 41 posts.
 
Join Date: Feb 2008
Experience: Beginner
15-Mar-2008, 12:56 PM #1
C++ Help
I am trying to convert decimal to Roman numeral can anyone tell me what I am doing wrong because its not compiling.

#include <iostream>
using namespace std;


class decimalType{
public:
void decimal();
int convert();
void print();
void get();

private:
int 1 = I;
int 2 = II;
int 3 = III;
int 4 = IV;
int 5 = V;
char decimalNumber;
};

void decimalType::decimal(){
1 = I;
2 = II;
3 = III;
4 = IV;
5 = V;

}


int decimalType::convert(){
if (decimalNumber = 1){
cout << I;
}else if(decimalNumber = 2){
cout << II;
}else if(decimalNumber = 3){
cout << III;
}else if(decimalNumber = 4){
cout << IV;
}else if(decimalNumber = 5){
cout << V;

}

return decimalNumber;
}


void decimalType:rint(){
cout << decimalNumber << endl;
}

void decimalType::get(){


}

int main(){
char decimalNumber;
cout << "Welcome to the Roman numeral to decimal converter!\nPlease enter a number in Roman numerals to be converted: ";
cin >> decimalNumber;

//print();
return 0;
}
demortes's Avatar
Computer Specs
Member with 98 posts.
 
Join Date: Mar 2007
Location: Sioux City, Iowa, USA, Earth
Experience: Advanced
15-Mar-2008, 01:01 PM #2
First off, int's can not be "I" or "II". Those are strings...
Rhino24's Avatar
Member with 41 posts.
 
Join Date: Feb 2008
Experience: Beginner
15-Mar-2008, 01:05 PM #3
I am kinda of a beginner can you give me a hand with the code...I would sure appreciate it. Thanks
demortes's Avatar
Computer Specs
Member with 98 posts.
 
Join Date: Mar 2007
Location: Sioux City, Iowa, USA, Earth
Experience: Advanced
15-Mar-2008, 01:18 PM #4
Well... without writing it all your going to have to accept a string or array of char's...

string numeral;
cin << numeral;

Then you're going to have to parse it. This part I'm a little scratchy on, but it should be something like this....

for (int i = 0;char numeral.c_str[i] != "\n";i++)
{
if (numeral.c_str[i] = "I")
total = total +1; //This can be simplified into total++;
if (numeral.c_str[i] = "V")
total += 5;
//etc
}
Rhino24's Avatar
Member with 41 posts.
 
Join Date: Feb 2008
Experience: Beginner
15-Mar-2008, 01:40 PM #5
string numeral;
cin << numeral;

I think I got something mixed up there, but its almost there

#include <iostream>
using namespace std;
using std::cin;


string numeral;
cin << numeral;

for (int i = 0;char numeral.c_str[i] != "\n";i++)
{
if (numeral.c_str[i] = "I")
total = total +1;
if (numeral.c_str[i] = "II")
total = total +2;
if (numeral.c_str[i] = "III")
total = total +3;
if (numeral.c_str[i] = "IV")
total = total +4;
if (numeral.c_str[i] = "V")
total += 5;

}

int main(){
char decimalNumber;
cout << "Welcome to the Roman numeral to decimal converter!\nPlease enter a number in Roman numerals to be converted: ";
cin >> numeral;

//print();
return 0;
}
demortes's Avatar
Computer Specs
Member with 98 posts.
 
Join Date: Mar 2007
Location: Sioux City, Iowa, USA, Earth
Experience: Advanced
15-Mar-2008, 02:34 PM #6
Yes, you do. What's going on is that you're only checking for ONE character with the numeral.c_str[i]. So you it's I, not II. Might have to also check for special cases.

Also, I made a mistake, it's if (numeral.c _str[i] == "I"). two ='s, not one.

My suggestion is go through a tutorial or something. I personally made this harder than it should be. You should be able to use strings for it.

if (numeral == "I")
total = 1;
if (numeral == "II")
total = 2;
if (numeral == "III")
etc.

Also, give us compiler errors when you post.

Also, what are the requirements would be nice. If you need to be able to post something like IX (9), etc.
TheRobatron's Avatar
Computer Specs
Senior Member with 418 posts.
 
Join Date: Oct 2007
Location: England
Experience: Intermediate
16-Mar-2008, 05:45 AM #7
The above code would only work for single Roman Numerals - if you want it to be flexible, you need to search the string for each character, then determine where they are (e.g. if it finds a I before a X it knows that the number is 9, not just searching for "IX".)
__________________
There's no place like 127.0.0.1

treydx's Avatar
Senior Member with 112 posts.
 
Join Date: Jan 2006
Experience: Intermediate
19-Mar-2008, 07:16 PM #8
Don't forget to put your code in a function and actually call it from main . . .

How I would tackle this problem is to convert all of your roman numerals to decimals one-by-one from left to right as you read your input string. Add each of them to a running sum.

After you get that working, if you need to handle cases such as IX=9, you're going to have to keep up with the runningTotal, the previously read numeral, and the currently read numeral. If the currently read numeral is larger than the previously read one, you need to subtract the previous one and add the current one to the sum. If the current one is smaller or equal to the previous one, just add the current one.
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are Off
Refbacks are Off

You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 01:17 PM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.