![]() |
|
#1
|
|||
|
|||
Another Roman numeral conversion problem.Hi guys, this is yet another forum about converting Roman NUmerals to Arabic numbers. I'm really new to programming and I just started today. I wanted to srart off simple by typing in one roman numeral, and getting the value, but I keep getting the wrong value. Help please.
CPP / C++ / C Code:
|
|||
|
#2
|
|||
|
|||
Re: Another Roman numeral conversion problem.Quote:
1: Your code is not valid C++. If anything, it is 'C' programming, or nearly. 2: Your code will not compile as written. In C++ main must be written to return an integer. Most C compilers will allow main to return void, but you will want to get into the habbit of returning an int even from your C programs. 'rom' is not a single char in your array named roman. When you see that your code needs to identify a value from a set of possible values as you've done using if...else if...else then you may want to consider using a switch/case statement where each case represents a choice from those possible. The default case is for those that you didn't chose to implement or, in this case, for all the possible choices that wouldn't be a valid Roman numeral. CPP / C++ / C Code:
Output: Code:
This code should compile (as a C program). Whether it does what you want or not, is yet to be seen. :davis: |
|
#3
|
|||
|
|||
Re: Another Roman numeral conversion problem.Thanks for your help davis, if it wasn't for you. I'd still be stuck trying to figure this out.
However, I have another problem, I tried to get the programme to add two roman numerals, I don't know if I have to use a string or an array, but I've tried, and nothing seems to work, I think its the syntax I use. CPP / C++ / C Code:
|
|
#4
|
|||
|
|||
Re: Another Roman numeral conversion problem.Why should I ever help you again? Didn't I just get done telling you that main MUST (that's absolutely MUST) return an int in C++? ...and what did you do? Write void main AGAIN like you're going to do that the rest of your natural life and beyond the grave if given the opportunity? Screw you. Figure it out yourself.
CPP / C++ / C Code:
result = 0 add = 0 + 0 result = (result of switch statement assignments) WTF do you _think_ might be the problem? :davis: |
|
#5
|
|||
|
|||
Re: Another Roman numeral conversion problem.What exactly are you trying to add? That function takes only 1 character as input. Do you want to return twice the value of that character?
|
|
#6
|
|||
|
|||
Re: Another Roman numeral conversion problem.And what exactly is this supposed to do?
CPP / C++ / C Code:
Then you do CPP / C++ / C Code:
And, after fiddling around with the result variable for a while, you finally overwrite everything in your switch. So in short: EXPLAIN the logic of your function. EXPLAIN how it is supposed to add and what it is supposed to add. How about this: Since your function can (can it? I don't really know, but I assumed it can now) convert a roman number into a decimal, how about, instead of "ruining" your whole function by making it a specialized roman-number-adder, you convert TWO roman numbers to decimal and THEN add them. OR Create a separate roman number adder function. In this function you would then call this converter function for both the numbers, add them and return the result. Like this: CPP / C++ / C Code:
|
|
#7
|
|||
|
|||
Re: Another Roman numeral conversion problem.Sorry davis your completely correct, I should have listened to what you said, its just a habit I have of putting void's next to main.
To fakepoo Kimmo, or anyone else who happens to be reading, thank you for all your help, I'll be a bit more specific, what I am trying to do is to add two or more values together after they have been converted from roman numerals to Arabic numbers, how would I go about doing that?? |
|
#8
|
|||
|
|||
Re: Another Roman numeral conversion problem.Quote:
You should also have read what I wrote... CPP / C++ / C Code:
result = 0 add = 0 + 0 In your function where you attempt to "add" some Roman numeral to some other Roman numeral, you aren't. Look (to quote Dave) REALLY LOOK at your code. Here is another example: CPP / C++ / C Code:
Output: Code:
...I doubt that my code conforms perfectly to the rules for Roman numerals, but it isn't too far from a reasonable starting point. :davis: |
|
#9
|
|||
|
|||
Re: Another Roman numeral conversion problem.WOW davis your a genius, thanks that works really well. However, there are some things I don't understand like:
1) What does BUFSIZ mean? 2) What does the return 0; at the end do? 3) What does stdin mean? 4) Why does int prev = 5000 and not 0? 5) And what does this line 'roman[ strlen( roman )-1 ] = '\0'; ' mean? I know strlen stands for string length but I don't understand the rest. Thankyou for your help. |
|
#10
|
|||
|
|||
Re: Another Roman numeral conversion problem.Oh one more thing,
why did you put '++roman;' at the end of the function? |
Recent GIDBlog
Toyota - 2009 May Promotion by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| help writing a roman numeral to decimal program | Ichigo | C++ Forum | 3 | 23-Mar-2006 15:05 |
| conversion problem ..help | raj_SS | C++ Forum | 1 | 19-Mar-2006 06:45 |
| Frustration...Roman Numeral Program | SpyD3r | C++ Forum | 14 | 13-Nov-2005 21:02 |
| ROMAN Numeral Pt. 2, Floating Point Exception | SpyD3r | C++ Forum | 1 | 13-Nov-2005 01:02 |
| Frustrating C++ problem... Roman numerals | Elsydeon | C++ Forum | 10 | 01-Sep-2005 08:37 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The