Function needs to return the correct value in C++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nuken
    New Member
    • Oct 2010
    • 16

    Function needs to return the correct value in C++

    the output looks likenthis:
  • whodgson
    Contributor
    • Jan 2007
    • 542

    #2
    Where do you convert Roman numerals to decimal values?

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      Lines 22-36 need to be an if-elseif-else cascade or, better yet, a switch statement. Instead you have sequential if statements -- that means that the last if statement overrides all of the others. Your Roman_digit_Val ue function will only ever return either M or bad_val.

      I presume I, V, X, L, C, D, M are all macros. Presumably bad_val is some negative number.

      Comment

      • nuken
        New Member
        • Oct 2010
        • 16

        #4
        This whole program really seems to be confusing me. Yes,
        I, V, X, L, C, D, M are const integers. And by if else cascade means: (we cant use switch)
        Code:
        int Roman_digit_Value( char roman_digit )
        {
           int val;
           if ( roman_digit == 'I' )
              val = I;
           else if ( roman_digit == 'V' )
              val = V;
           else if ( roman_digit == 'X' )
              val = X;
           else if ( roman_digit == 'L' )
              val = L;
           else if ( roman_digit == 'C' )
              val = C;
           else if ( roman_digit == 'D' )
              val = D;
           else if ( roman_digit == 'M' )
              val = M;
           else 
               val = bad_val;
           return val;
        }
        If I put the whole program on here could you help me fix it up its like 5 functions or so?

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          nuken wrote ...
          And output should look like this:
          MCCXXVI
          The first number is 1226
          LXVIIII
          The second number is 69
          The second example uses "VIIII" for 9 instead of "IX". Does this mean that you are not required to support the subtractive format for Roman Numerals?

          Comment

          • nuken
            New Member
            • Oct 2010
            • 16

            #6
            Yes, I am using only the additive form. Heres some more info.
            I = 1
            V = 5
            X = 10
            L = 50
            C = 100
            D = 500
            M = 1000
            bad_val = -1

            What is confusing me is how to use the basic functions i made to read each letter, change it to a number, add all the numbers together, and then display that number. It also has to do some eqautions but i think i can figure that part out after this first part.
            I have to add in there somewhere "The second number is " also.

            Comment

            • nuken
              New Member
              • Oct 2010
              • 16

              #7
              They are converted using a combination of the two functions. But I changed it a little how do i add all the numbers together before they are displayed?
              Code:
              int Roman_digit_Value( char roman_digit )
              {
                 int val;
                 if ( roman_digit == 'I' )
                    val = I;
                 else if ( roman_digit == 'V' )
                    val = V;
                 else if ( roman_digit == 'X' )
                    val = X;
                 else if ( roman_digit == 'L' )
                    val = L;
                 else if ( roman_digit == 'C' )
                    val = C;
                 else if ( roman_digit == 'D' )
                    val = D;
                 else if ( roman_digit == 'M' )
                    val = M;
                 else 
                     val = bad_val;
                 return val;
              }
              int Get_Roman_Numeral()
              {
                 int num = 0;
                 char ch = 0;
                 
                 cin >> ch; 
                 int value = Roman_digit_Value(ch);
                 if ( value > 0 )
                 {
                    num = num + value;
                    return num;
                 }
                    
               return 0;  
              }
                     
              int main()
              { 
                char operation = '+';
                string input_Roman;  
                int Roman_Num = 0;
                int Second_Roman_Num = 0;
                int Result = 0;
                Roman_Num = Get_Roman_Numeral();
                cout << "The first number is " << endl;
                    while ( !cin.eof() ) 
                    {  
                       
                       
                       Second_Roman_Num = Get_Roman_Numeral();
                       cout << "The second number is " << endl;
                       cin >> operation;
                       //cout << "Arithmetic operation is " << operation << endl;
                       //Result = Return_Operation (Second_Roman_Num, Roman_Num, operation);
                       //Print_Result( Roman_Num, Second_Roman_Num, Result, operation );
              
                    }
                    return 0;
              }

              Comment

              Working...