By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 1,722 Online
Bytes IT Community
+ Ask a Question
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
Share this Question
Share on Google+
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
  1. int Roman_digit_Value( char roman_digit )
  2. {
  3.    int val;
  4.    if ( roman_digit == 'I' )
  5.       val = I;
  6.    else if ( roman_digit == 'V' )
  7.       val = V;
  8.    else if ( roman_digit == 'X' )
  9.       val = X;
  10.    else if ( roman_digit == 'L' )
  11.       val = L;
  12.    else if ( roman_digit == 'C' )
  13.       val = C;
  14.    else if ( roman_digit == 'D' )
  15.       val = D;
  16.    else if ( roman_digit == 'M' )
  17.       val = M;
  18.    else 
  19.        val = bad_val;
  20.    return val;
  21. }
  22.  
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
  1. int Roman_digit_Value( char roman_digit )
  2. {
  3.    int val;
  4.    if ( roman_digit == 'I' )
  5.       val = I;
  6.    else if ( roman_digit == 'V' )
  7.       val = V;
  8.    else if ( roman_digit == 'X' )
  9.       val = X;
  10.    else if ( roman_digit == 'L' )
  11.       val = L;
  12.    else if ( roman_digit == 'C' )
  13.       val = C;
  14.    else if ( roman_digit == 'D' )
  15.       val = D;
  16.    else if ( roman_digit == 'M' )
  17.       val = M;
  18.    else 
  19.        val = bad_val;
  20.    return val;
  21. }
  22. int Get_Roman_Numeral()
  23. {
  24.    int num = 0;
  25.    char ch = 0;
  26.  
  27.    cin >> ch; 
  28.    int value = Roman_digit_Value(ch);
  29.    if ( value > 0 )
  30.    {
  31.       num = num + value;
  32.       return num;
  33.    }
  34.  
  35.  return 0;  
  36. }
  37.  
  38. int main()
  39.   char operation = '+';
  40.   string input_Roman;  
  41.   int Roman_Num = 0;
  42.   int Second_Roman_Num = 0;
  43.   int Result = 0;
  44.   Roman_Num = Get_Roman_Numeral();
  45.   cout << "The first number is " << endl;
  46.       while ( !cin.eof() ) 
  47.       {  
  48.  
  49.  
  50.          Second_Roman_Num = Get_Roman_Numeral();
  51.          cout << "The second number is " << endl;
  52.          cin >> operation;
  53.          //cout << "Arithmetic operation is " << operation << endl;
  54.          //Result = Return_Operation (Second_Roman_Num, Roman_Num, operation);
  55.          //Print_Result( Roman_Num, Second_Roman_Num, Result, operation );
  56.  
  57.       }
  58.       return 0;
  59. }
  60.  
Oct 26 '10 #7

Post your reply

Sign in to post your reply or Sign up for a free account.