I've written the program and it works fine. but my instructor said that i can make the program short and use functions in it, but i cant really understand functions that good. so i just need a little help making the program short and use functions too. I'm posting my code here but i'm not posting the cin statements and the validating user entry loops. i'm just posting the if statements that i used. so here is my code:
thousands = number / 100; // number is the user input.
hundreds = number / 100 % 10;
tens = number / 10 % 10;
ones = number % 10;
(thousands == 1 ? roman += "M":
thousands == 2 ? roman += "MM":
thousands == 3 ? roman += "MMM":
hundreds == 1 ? roman += "C":
hundreds == 2 ? roman += "CC":
hundreds == 3 ? roman += "CCC":
hundreds == 4 ? roman += "CD":
hundreds == 5 ? roman += "D":
hundreds == 6 ? roman += "DC":
hundreds == 7 ? roman += "DCC":
hundreds == 8 ? roman += "DCCC":
hundreds == 9 ? roman += "CM"
: roman = roman);
(tens == 1 ? roman += "X":
tens == 2 ? roman += "XX":
tens == 3 ? roman += "XXX":
tens == 4 ? roman += "XL":
tens == 5 ? roman += "L":
tens == 6 ? roman += "LX":
tens == 7 ? roman += "LXX":
tens == 8 ? roman += "LXXX":
tens == 9 ? roman += "XC"
: roman = roman);
(ones == 1 ? roman += "I":
ones == 2 ? roman += "II":
ones == 3 ? roman += "III":
ones == 4 ? roman += "IV":
ones == 5 ? roman += "V":
ones == 6 ? roman += "VI":
ones == 7 ? roman += "VII":
ones == 8 ? roman += "VIII":
ones == 9 ? roman += "IX"
: roman = roman);
2 1953
try to write a function and then report the problem u facing ....
DeMan 1,806
Top Contributor
The key to creating these functions (and in fact any function) is to work out the rules that have to be followed....So first consider the rules that Roman Numerals follow.....
We can think of a common pattern with several exceptions, but thankfully even these exceptions follow patterns.....
Your code will probably work reasonably well, but I suspect the instructor is looking for something that follows rules, rather than explicitly setting the values.
In the simplest case (for you) you could write a function that does exactly what you do now and returns a string with the entire value. It sounds like the instructor would award more marks, however, If you created seperate functions for units, tens, hundreds etc (which you could still call from one function called from main if you liked).
I suppose the biggest idea in an exercise such as this, though, is to simplify a process. Your solution, while providing the user with an answer, hasn't simplified the proccess.
Hope these thoughts help....
Post again if you get stuck and need further help!
Just some ideas, hope they helped....
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Ivo |
last post by:
Hi,
An array contains elements of the form , for example IVa, XIXb, LIX and LXIVc. Does anyone have a
function which can sort this array in a way a human would expect it to, with
the roman letters treated as their numerical values? I don't think this is a
especially problematic thing to do, but I don't want to re-invent the wheel.
Thanks
Ivo
|
by: level9 |
last post by:
How can I convert roman numerals to intergers in c++ VS 6?
|
by: Christopher Benson-Manica |
last post by:
Inspired by a thread on clc++, I decided to try it out...
#include <stdio.h>
#include <stdlib.h>
int main( int argc, char *argv )
{
int i;
int result=0;
int this;
|
by: Patrick Blackman |
last post by:
How do you write regular numbers eg 1 23 475896 in roman numerals inC#
|
by: daming23 |
last post by:
Hi,
I'm taking an online C++ class and having some difficulty understanding the assignments. Can someone please look at the code I wrote to ensure validity? I am not asking for the answer just some guidance, I’m totally lost.
The assignment is as follow:
Write a prg to convert numbers entered in Roman Numeral to decimal. Your prg should...
| |
by: maojecel |
last post by:
guyz just want to know the simple code or program how to convert integers to roman numerals.. plz do a reply soon.. i badly need it as soon as u answer. plzzz
|
by: mookbooker |
last post by:
I have a code written out with zero errors but i know my main() function is completely wrong and after i build the solution i compile it and nothing happens!! Not even cout statements please help.
// 2.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include...
|
by: kotlakirankumar |
last post by:
please help me out the program for converting the integers to roman
numerals using files in the c language from 1-5000 range send the
program to my
mail id ::::::: kotlakirankumar@gmail.com
thank you all
|
by: capoeira26 |
last post by:
I have is that my entered Roman numerals work only if the numerlas are entered in large to smaller format. For exapmple if I type MMC program will correctly display 2100, but when I enter MCM it will display 2100 instead of 1900. I know that I need to come up with a way to determine what letter comes before so program can subtract it but at this...
|
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...
|
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. ...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
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...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |