the output looks likenthis:
Function needs to return the correct value in C++
Collapse
X
-
-
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
-
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; }
Comment
-
nuken wrote ...
And output should look like this:
MCCXXVI
The first number is 1226
LXVIIII
The second number is 69Comment
-
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
-
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
Comment