see something I don't?

This is a discussion on see something I don't? within the C++ Programming forums, part of the General Programming Boards category; hey guys. I'm working on this assignment for an online class. almost done, except i'm not getting the correct results. ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    5

    see something I don't?

    hey guys. I'm working on this assignment for an online class. almost done, except i'm not getting the correct results. i'm at the point where i've been staring at the code for too long so i thought i'd try here. this isnt the whole thing, just the exact part i'm working on right now. converting roman numerals to decimals. "CD" comes up as 600 when it's 400.

    Code:
    #include "stdafx.h"
    #include <string>
    #include <iostream>
    using namespace std;
    
    string input;
    
    class romanType
    {
    public:
    	int number;
    	void convertRoman();
    };
    
    int main()
    {
    	romanType myType;
    	cout << "Please enter a Roman number." << endl;
    
    	cin >> input;
    	myType.convertRoman();
    }
    
    void romanType::convertRoman(){
    
    	int output = 0;
    	int n = 0;
    
    	for (n = 0; n != input.length(); n++)
    	{ 
    		char p;
    
    		if (n > 0)
                 p = input[n - 1];
            else
                 p = input[n];
    
    		if (input[n] == 'M' || input[n] == 'm')
    			output = output + 1000;
    		
    		if	(input[n] == 'D' || input[n] == 'd')
    		{
    		    if (p == 'C' || p == 'c')
    				output = output + 400;
    			else
    				output = output + 500;
    		}
    		
    		if	(input[n] == 'C' || input[n] == 'c')
    		{
    			if (p == 'X' || p == 'x')
    				output = output + 90;
    			else
    				output = output + 100;
    		}
    		
    		if	(input[n] == 'L' || input[n] == 'l')
    		{
    			if (p == 'X' || p == 'x')
    				output = output + 40;
    			else
    				output = output + 50;
    		}
    		
    		if	(input[n] == 'X' || input[n] == 'x')
    		{
    			if (p == 'I' || p == 'i')
    				output = output + 9;
    			else
    				output = output + 10;
    		}
    		
    		if	(input[n] == 'V' || input[n] == 'v')
    		{
    			if (p == 'I' || p == 'i')
    				output = output + 4;
    			else
    				output = output + 5;
    		}
    		
    		if	(input[n] == 'I' || input[n] == 'i')
    			output = output + 1;
    		
    		if (input[n] == 'M' || input[n] == 'm' || input[n] == 'D' || input[n] == 'd' || input[n] == 'C' || input[n] == 'c' || input[n] == 'L' || input[n] == 'l' || input[n] == 'X' || input[n] == 'x' || input[n] == 'V' || input[n] == 'v' || input[n] == 'I' || input[n] == 'i')
    			cout << "Thanks!" << endl;
    		else
    			cout << "Please enter capital letters consisting of only M, C, D, L, X, V, and I." << endl;
    	}
    
    		cout << output << endl;
    }
    Last edited by blinkbomber; 11-14-2010 at 08:14 PM. Reason: as requested

  2. #2
    Registered User
    Join Date
    Nov 2010
    Posts
    5
    scratch that, i'm getting "500", not "600"

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    106
    looks like you have nothing in your code to handle if C comes first.. I see an addition for if it comes after say M.. also post your whole code so its possible for us to run it an tinker with it.

    also this line

    Code:
    output = output + 90;
    i normally would write as

    Code:
    output += 90;

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    5
    i gotcha man. yeah i'm still tinkering with it as well. but yeah, i dunno i just got stuck writing that out that way, lol.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    106
    yeah.. would you like to explain your method to try and convert the roman numerals to numbers.. I think I would do something like have a temp value that scored the current letter.. then checked to see if it was greater or less than the next value.. then subract if its less.. etc. then you have to handle instance such as IIIVX.. I'm going to run you code when i get a chance which should be in the next few minutes



    EDIT:

    after looking at and running the code it appears the problem is that you have nothing detecting the location of the Roman Numerals.. For example: you enter IIX and instead of subtracting 2 from ten it adds 2... theres also another problem because the value is then one less then what it needs to be..

    edit 2:

    something like:

    Code:
    function to return value of roman number1 one and roman numeral 2
    store them as temp values..
    compare the values
    Last edited by jamort; 11-14-2010 at 08:41 PM.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    5
    haha, yeah sorry man. i just figured if i can read the values stored in specific string positions, i could use that. just basically say what letter means what number depending on what's in front of it. (ie. pos 0 in front of pos 1).

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    5
    hmm... i just tried adding this to the 'c' and 'i' sections and its correcting some of the previously mentioned errors.

    Code:
    		if	(input[n] == 'C' || input[n] == 'c')
    		{
    			if (p == 'X' || p == 'x')
    				output = output + 90;
    			else if (d == 'D' || d == 'd')
    				output = output;
    			else
    				output = output + 100;
    
    		if	(input[n] == 'I' || input[n] == 'i')
    		{
    			if (d == 'X' || d == 'x' || d == 'V' || d == 'v')
    				output = output;
    			else
    				output = output + 1;

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21