GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 22-Nov-2007, 13:00
hoodedpython hoodedpython is offline
New Member
 
Join Date: Nov 2007
Posts: 11
hoodedpython is on a distinguished road

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:
#include<stdio.h>

int roman (char);

void main()
{
	char roman[] ={'M','D','C','L','X','V','I','rom'};
	char rom;
	
		//M=1000;D=500;C=100;L=50;X=10;V=5;I=1;//

			printf("please enter roman numeral\n");
			scanf("%i\n", &rom);
			printf("%i\n", rom);

}
int roman (char rom)
{
			if (rom=='I')
				return 1;
			else if (rom=='V')
				return 5;
			else if (rom=='X')
				return 10;
			else if (rom=='L')
				return 50;
			else if (rom=='C')
				return 100;
			else if (rom=='D')
				return 500;
			else if (rom=='M')
				return 1000;
			else
			printf("Not roman numeral");

}

			
  #2  
Old 23-Nov-2007, 04:35
davis
 
Posts: n/a

Re: Another Roman numeral conversion problem.


Quote:
Originally Posted by hoodedpython
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.


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:
#include <stdio.h>

int roman (char);

int main()
{
    //char roman[] ={'M','D','C','L','X','V','I'};
    char rom;
    
	//M=1000;D=500;C=100;L=50;X=10;V=5;I=1;//

    printf("please enter roman numeral\n");
    rom = getchar();
    printf("%c = %d\n", rom, roman (rom) );
}

int roman (char rom)
{
    int result = 0;

    switch (rom)
    {
    case 'i':
    case 'I':
	result = 1;
	break;
    case 'v':
    case 'V':
	result = 5;
	break;
    case 'x':
    case 'X':
	result = 10;
	break;
    case 'l':
    case 'L':
	result = 50;
	break;
    case 'c':
    case 'C':
	result = 100;
	break;
    case 'd':
    case 'D':
	result = 500;
	break;
    case 'm':
    case 'M':
	result = 1000;
	break;
    default:
	printf("Not roman numeral");
	result = 0;
	break;
    }
    return result;
}

Output:

Code:
please enter roman numeral I I = 1


This code should compile (as a C program). Whether it does what you want or not, is yet to be seen.


:davis:
  #3  
Old 24-Nov-2007, 10:38
hoodedpython hoodedpython is offline
New Member
 
Join Date: Nov 2007
Posts: 11
hoodedpython is on a distinguished road

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:
#include <stdio.h>

int roman (char);

void main()
{
    
    char rom; //variable rom defined as a character //
    
	//M=1000;D=500;C=100;L=50;X=10;V=5;I=1;//


    printf("please enter roman numeral\n"); //user asked to enter roman numberal//
    rom = getchar();
    printf("%d\n", roman (rom) );
}

int roman (char rom)
{
    int result = 0;
    int add;

		add= result+result;

		while
			result >= 1;  //if more than one character is entered, programme should add both values//
			result=add;
		
    switch (rom)
    {
    case 'i':case 'I':
	result = 1;
	break;
    case 'v':case 'V':
	result = 5;
	break;
    case 'x':case 'X':
	result = 10;
	break;
    case 'l':case 'L':
	result = 50;
	break;
    case 'c':case 'C':
	result = 100;
	break;
    case 'd':case 'D':
	result = 500;
	break;
    case 'm':case 'M':
	result = 1000;
	break;
    default:
	printf("Not roman numeral");
	result = 0;
	break;
    }
    return result;
}
  #4  
Old 24-Nov-2007, 21:11
davis
 
Posts: n/a

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:
    int result = 0;
    int add;
    add= result+result;

result = 0
add = 0 + 0

result = (result of switch statement assignments)

WTF do you _think_ might be the problem?


:davis:
  #5  
Old 26-Nov-2007, 09:04
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 713
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

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  
Old 26-Nov-2007, 10:11
Kimmo Kimmo is offline
Member
 
Join Date: Mar 2007
Location: Finland
Posts: 285
Kimmo is a jewel in the roughKimmo is a jewel in the roughKimmo is a jewel in the rough

Re: Another Roman numeral conversion problem.


And what exactly is this supposed to do?

CPP / C++ / C Code:
    int result = 0;
    int add;

		add= result+result;

		while
			result >= 1;  //if more than one character is entered, programme should add both values//
			result=add;
Your comment says, if more than one character is entered... Where? result is an int, it can't store "characters."

Then you do
CPP / C++ / C Code:
result = add;
Does this add something? Or does it just assign?

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:
int romanToDecimal(char roman);  // convert roman to decimal
int romanAdder(char roman1, char roman2); // or maybe even char* roman for adding as many romans as one can imagine

int main()
{
    char roman1 = 'i';
    char roman2 = 'i';

    int romanPlusRoman = romanToDecimal(roman1) + romanToDecimal(roman2);

    romanPlusRoman = romanAdder(roman1, roman2);

    return 0;
}

int romanAdder(char roman1, char roman2)
{
    return ( romanToDecimal(roman1) + romanToDecimal(roman2) );
}
  #7  
Old 27-Nov-2007, 05:21
hoodedpython hoodedpython is offline
New Member
 
Join Date: Nov 2007
Posts: 11
hoodedpython is on a distinguished road

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  
Old 27-Nov-2007, 07:10
davis
 
Posts: n/a

Re: Another Roman numeral conversion problem.


Quote:
Originally Posted by hoodedpython
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.

You should also have read what I wrote...

CPP / C++ / C Code:
    int result = 0;
    int add;
    add= result+result;

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:
#include <stdio.h>
#include <string.h>

int rtoi( const char* );

int main()
{
    //char roman[] ={'M','D','C','L','X','V','I'};
    char roman[BUFSIZ] = {0};
    
	//M=1000;D=500;C=100;L=50;X=10;V=5;I=1;//

    printf( "Please enter a Roman numeral string: " );
    fgets( roman, BUFSIZ, stdin );
    roman[ strlen( roman )-1 ] = '\0'; 
    printf( "Your entry: %s = %d\n", roman, rtoi( roman ) );
    
    return 0;
}

int rtoi( const char* roman )
{
    int result = 0;
    int temp = 0;
    int prev = 5000; // higher than highest Roman numeral
    while( *roman )
    {
	switch( *roman )
	{
	case 'i':
	case 'I':
	    temp = 1;
	    break;
	case 'v':
	case 'V':
	    temp = 5;
	    break;
	case 'x':
	case 'X':
	    temp = 10;
	    break;
	case 'l':
	case 'L':
	    temp = 50;
	    break;
	case 'c':
	case 'C':
	    temp = 100;
	    break;
	case 'd':
	case 'D':
	    temp = 500;
	    break;
	case 'm':
	case 'M':
	    temp = 1000;
	    break;
	default:
	    break;
	}
	if( prev >= temp )
	{
	    result += temp;
	}
	else // prev < temp
	{
	    result += (temp - (prev + prev));
	}
	prev = temp;
	++roman;
    }
    return result;
}

Output:

Code:
Please enter a Roman numeral string: MMVII Your entry: MMVII = 2007 Please enter a Roman numeral string: MCMIV Your entry: MCMIV = 1904 Please enter a Roman numeral string: XVIIII Your entry: XVIIII = 19 Please enter a Roman numeral string: XCIX Your entry: XCIX = 99 Please enter a Roman numeral string: 34262387 Your entry: 34262387 = 0

...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  
Old 28-Nov-2007, 16:26
hoodedpython hoodedpython is offline
New Member
 
Join Date: Nov 2007
Posts: 11
hoodedpython is on a distinguished road

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  
Old 29-Nov-2007, 05:50
hoodedpython hoodedpython is offline
New Member
 
Join Date: Nov 2007
Posts: 11
hoodedpython is on a distinguished road

Re: Another Roman numeral conversion problem.


Oh one more thing,

why did you put '++roman;' at the end of the function?
 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate 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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 08:38.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.