424,952 Members | 1,722 Online Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

Function needs to return the correct value in C++

 P: 16 the output looks likenthis: Oct 24 '10 #1
6 Replies

 100+ P: 542 Where do you convert Roman numerals to decimal values? Oct 25 '10 #2

 Expert 100+ P: 2,396 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_Value 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. Oct 25 '10 #3

 P: 16 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) Expand|Select|Wrap|Line Numbers 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? Oct 25 '10 #4

 Expert 100+ P: 2,396 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? Oct 25 '10 #5

 P: 16 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. Oct 25 '10 #6

 P: 16 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? Expand|Select|Wrap|Line Numbers 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; }   Oct 26 '10 #7 