473,804 Members | 4,408 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Function needs to return the correct value in C++

16 New Member
the output looks likenthis:
Oct 24 '10 #1
6 1670
whodgson
542 Contributor
Where do you convert Roman numerals to decimal values?
Oct 25 '10 #2
donbock
2,426 Recognized Expert Top Contributor
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.
Oct 25 '10 #3
nuken
16 New Member
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
donbock
2,426 Recognized Expert Top Contributor
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
nuken
16 New Member
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
nuken
16 New Member
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

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

Similar topics

2
8755
by: Konstantin Zakharenko | last post by:
Hello, Our QA team have running a lot of test scripts (for automated regression testing), they run them on the different databases (Oracle/MS SQL). Several of those tests are dependent on the current date/time. In order to be able to use them efficiently, we changed the current date/time on the QA database server to a specific date/time before starting the scripts, so we are sure the test scripts always run in the same environment.
2
1661
by: eight02645999 | last post by:
hi i use odbc to update a table in a database but i always get return value of -1 even though i tried to return an integer. the table is updated though .... sql = """ update table set column = 0 where col = "%s" select @@rowcount
13
2164
by: Sara | last post by:
I have a query that pulls data for the month, using the Month End Date from a form (user enters) as criteria. It works. I want to use the same query to pull "month to date" data, on a weekly basis. So, for week 1, it would only show the first 7 days; week 2 the first 14 days, etc. If I enter the next month ending date (1/1/05) right now, I get the information for days since the last month end until current date. That's perfect. My...
1
1721
by: deko | last post by:
I think I remember reading somewhere that there's a built-in VBA function that returns a quoted value from a table or form field. Does such a function exist? I think it was something like Eval, but returned a quoted string... ?
6
6531
by: Hardy Wang | last post by:
Hi all, I have the following codes, but SCOPE_IDENTITY() just returns NULL to me. If I comment out SCOPE_IDENTITY() line and run @@IDENTITY line, it works fine!! Since I have a trigger on the table, I have to use SCOPE_IDENTITY(). Any ideas? SqlConnection conn = new SqlConnection(connectionString); conn.Open(); //Create the dataadapter
13
4266
by: Steve McLellan | last post by:
Hi, Is there any reason why the VC2003 C++ compiler emits the error "Function must return a value" for global functions and not for class member functions? For example: int test() {
3
1766
by: mikerich135 | last post by:
Is it possible that any function without return type and returning value will return any value. If so, How?
0
9579
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10577
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10320
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10077
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7620
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6853
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4299
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2991
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.