CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    May 2016
    Posts
    6

    Conversion Program

    I am supposed to be creating a program that converts roman numbers to its decimal values. I have done something, but the math is wrong. When entering MCMLXXVIII, I am supposed to get 1978, and instead I get 2178. I am using substrings to solve this problem, but I am not getting anywhere. Can somebody please advise me on what to do?

    Thanks

    Code:
    #include<iomanip>
    #include<iostream>
    #include<string>
    using namespace std;
    
    int main(){
    
    
    	int M = 1000;
    	int D = 500;
    	int C = 100;
    	int L = 50;
    	int X = 10;
    	int V = 5;
    	int I = 1;
    	
    
    	int num = 0;
    
    	cout << " Enter the Roman Numeral Value: ";
    	string roman;
    	cin >> roman;
    
    	string sub = roman.substr(0, 2);
    	cout << sub << endl;
    	
    
    
    
    
    	for (int i = 0; i < roman.length(); i++)
    	{
    		
    		switch (roman.at(i))
    		{
    		
    		case 'M':
    		case 'm':
    			num += M;
    			break;
    		case 'D':
    		case 'd':
    			num += D;
    			break;
    		case 'C':
    		case 'c':
    			num += C;
    			break;
    		case 'L':
    		case 'l':
    			num += L;
    			break;
    		case 'X':
    		case 'x':
    			num += X;
    			break;
    		case 'V':
    		case 'v':
    			num += V;
    			break;
    		case 'I':
    		case 'i':
    			num += I;
    			break;
    		}
    	}
    
    	cout << num << endl;
    }

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Conversion Program

    You need to treat 'specials' such as CM, IX, XL etc as one entry. You are treating CM as two symbols, 100 and 1000 when it should be treated as one symbol with a value of 900.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    May 2016
    Posts
    6

    Re: Conversion Program

    I have tried that before, but it sill outputs the same answer of 2178. I am supposed to be getting 1978

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Conversion Program

    Quote Originally Posted by ProgrammerXYZ View Post
    I have tried that before, but it sill outputs the same answer of 2178. I am supposed to be getting 1978
    Then your code is not correct. If you post the code you used that treats CM etc as one symbol we'll be able to provide further guidance.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    May 2016
    Posts
    6

    Re: Conversion Program

    Thats with the CM

    #include<iomanip>
    #include<iostream>
    #include<string>
    using namespace std;

    int main(){


    int M = 1000;
    int D = 500;
    int C = 100;
    int L = 50;
    int X = 10;
    int V = 5;
    int I = 1;
    int CM = 900;

    int num = 0;

    cout << " Enter the Roman Numeral Value: ";
    string roman;
    cin >> roman;

    string sub = roman.substr(1, 2);
    cout << sub << endl;

    string what = roman.substr(0, 1);





    for (int i = 0; i < roman.length(); i++)
    {

    switch (roman.at(i))
    {
    case 'CM':
    case 'cm':
    num += CM;
    break;
    case 'M':
    case 'm':
    num += M;
    break;
    case 'D':
    case 'd':
    num += D;
    break;
    case 'C':
    case 'c':
    num += C;
    break;
    case 'L':
    case 'l':
    num += L;
    break;
    case 'X':
    case 'x':
    num += X;
    break;
    case 'V':
    case 'v':
    num += V;
    break;
    case 'I':
    case 'i':
    num += I;
    break;
    }
    }

    cout << num << endl;
    }

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: Conversion Program

    Quote Originally Posted by ProgrammerXYZ View Post
    I have tried that before, but it sill outputs the same answer of 2178. I am supposed to be getting 1978
    Did you try to debug your code step-by-step to see where it goes wrong?
    Victor Nijegorodov

  7. #7
    Join Date
    May 2016
    Posts
    6

    Re: Conversion Program

    It does not indicate where it goes wrong, as it is compiling correctly. Im just getting the math wrong. I am supposed to be using substrings and functions to make it work. This is the exact question

    Write a program that converts a Roman number such as MCMLXXVIII to its decimal number representation. Hint: First write a function that yields the numeric value of each of the letters. Then write a function that converts the Roman number string and returns its value as follows: Look at the first two characters of the string. If the first has a larger value than the second, then simply convert the first, call the conversion function again for the substring starting with the second character, and add both values. If the first one has a smaller value than the second, compute the difference and add it to the conversion of the substring starting at the third character.

    I don't know how to do that with substrings

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Conversion Program

    As per your post #1, please use code tags when posting code!

    Code:
    case 'CM':
    case 'cm':
         num += CM;
         break;
    Why do you think this is going to work? CM is a string consisting of 2 characters - not a single char, yet roman.at(i) returns a single char!

    and what about other 'special' roman pairs eg IV ?

    The question tells you how to do this - but your code doesn't follow the hint. Where is the suggested function that yields the numeric value for a roman letter? I suggest you start with that and then modify your code to follow the rest of the given algorithm.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  9. #9
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Conversion Program

    I don't know how to do that with substrings
    For info on using string, see http://www.cplusplus.com/reference/string/string/
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  10. #10
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: Conversion Program

    Quote Originally Posted by ProgrammerXYZ View Post
    It does not indicate where it goes wrong, as it is compiling correctly. Im just getting the math wrong. ...
    The fact "it is compiling correctly" has nothing to do with the "working correctly". And the debugger was developed to help you (as fast as possible!) let your code correctly work!
    Debugging in Visual Studio
    Victor Nijegorodov

  11. #11
    Join Date
    May 2016
    Posts
    6

    Re: Conversion Program

    I actually did something like this, and it works perfectly. However, I am having trouble with outputting an error message if its not 'M', 'C', 'L', 'X', 'V' or 'I'. I also want it to output a 0, if the string is empty when entered. Can someone please tell me what I should do for that?

    Code:
    #include<iomanip>
    #include<iostream>
    #include<string>
    using namespace std;
    
    int GetValue(char); 
    
    int main(){ 
    
    	int num = 0;
    	string ignoreNextChar = "false";
    
    	cout << "Enter the Roman Numeral that need to be converted: ";
    	string roman;
    	cin >> roman;
    
    	for (int i = 0; i < roman.length(); i++)
    	{
    			if (ignoreNextChar == "true")
    		{
    			ignoreNextChar = "false";
    			continue;
    		}
    		int Value = GetValue(roman.at(i));
    		if ((i + 1) < roman.length())
    		{
    			int nextVal = GetValue(roman.at(i + 1));
    			if (nextVal > Value)
    			{
    				num += nextVal - Value;
    				ignoreNextChar = "true";
    				continue;
    			}
    		}
    		num += GetValue(roman.at(i));
    	}
    	cout << "The Decimal Value is: " << num << endl;
    
    }
    
    int GetValue(char romanNumeral)
    {
    	switch (romanNumeral)
    	{
    
    	case 'M':
    		return 1000;
    	case 'D':
    		return 500;
    	case 'C':
    		return 100;
    	case 'L':
    		return 50;
    	case 'X':
    		return 10;
    	case 'V':
    		return 5;
    	case 'I':
    		return 1;
    	}
    	return 0;
    };


    I want to make it so that when someone enters something like XYZ, it outputs an error message, and as I have said above if the string is empty output a 0.
    Thanks
    Last edited by ProgrammerXYZ; May 31st, 2016 at 10:26 AM.

  12. #12
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Conversion Program

    Well a good starting point would be to use code tags when posting code - as per post #1 - so that the code is readable!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  13. #13
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Conversion Program

    I am having trouble with outputting an error message if its not 'M', 'C', 'L', 'X', 'V' or 'I'
    Your GetValue() function returns 0 if an invalid roman numeral is present - so the return value of GetValue() should be checked for 0 after it has been called and if 0 display a message.

    if the string is empty when entered
    When obtaining a string via stream extraction, the string won't be empty unless a stream error occurred (highly unlikely for obtaining a string). So if you want to test for an empty string, then use something like getline() - http://www.cplusplus.com/reference/s...tring/getline/ - to obtain the input and then test for an empty string.

    PS ignoreNextChar would be better as a type bool.
    Last edited by 2kaud; May 31st, 2016 at 10:29 AM. Reason: PS
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  14. #14
    Join Date
    Aug 2006
    Posts
    231

    Re: Conversion Program

    I am having trouble with outputting an error message if its not 'M', 'C', 'L', 'X', 'V' or 'I'.
    You can just use a regular expression and abort immediately if there are any invalid letters in the string.
    http://www.cplusplus.com/reference/regex/

    However, you'll need better checks than that. What if someone enters "IM". That's not a valid roman number.

  15. #15
    Join Date
    May 2016
    Posts
    6

    Re: Conversion Program

    Thanks @2Kaud It works perfectly

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured