Write your question here.
I need some help with converting a user input (an integer) into roman numerals. Some of the numbers work but some do not work. for example 1900 shows a roman numeral that is mathematically correct but is not correct in terms of roman numerals. such as 4 is iv and not iiii.
This is checking if the number is from 10-19, OR if it equals 9, if it equals 9, it just outputs a nine, and sets the number equal to 0. The other thing I changed does the same thing except for with 4.
Another thing, it is easier instead of doing something like num = num-5, it is easier to do num-=5. It accomplishes the same purpose, and there is no difference it is just a bit more compressed.
The thing is you don't have just the IV and IX variations for 4 and 9. It is also the case for 40(XL) and 90(XC) then 400(CD) and 900(CM).
Here my take on your problem:
hey hiten, while your code works flawlessly, i do not know how the code works and have been trying to figure it out. some questions are what is the function of "void" in line 5 and the mathematical computation you did for the "else if" statements throughut lines 46, 50, 60, and 64. thanks
#include <iostream>
#include <string>
usingnamespace std;
void printNumeral(string numeral, int noTimes = 1){ //this function takes a string for instance "X" or "CM" and prints it noTimes times
for(int i = 0; i < noTimes; i++) // say your number is 3000. Then printNumeral("M", 3) is the same as writing cout << "M" three
cout << numeral; //times
} //if we call the function as printNumeral("M") then the second parameter is 1 by default
//it would be as if we'd call the function like printNumeral("M", 1);
int main(){
int num;
char result;
do {
int numeralM;
int numeralD;
int numeralX;
cout<<"enter a number:"<<endl;
cin>>num;
numeralM = num/1000; //numeralM represents the thousands number eg: num = 3211 then numeralM is 3
printNumeral("M", numeralM); //we print "M" numeralM times. In the above example we print MMM
num -= numeralM*1000; //substracts from num the thousands part. If num = 3211 then after this, num is 211
numeralD = num/100; //numeralD represends the hundreds number. In our case numeralD will be 2
if(numeralD == 9) //here are the conditions to avoid writing DCCCC (writing 4 times numeral C) or CCCC
printNumeral("CM"); //if the hundreds number is 9 then we need to print out CM
elseif(numeralD >= 5){ //if not and if the number is greater than 5 (meaning it's 5,6,7 or 8) we need to print out a D(for 5)
printNumeral("D"); //and as many C ( as many hundreds ) there are left that is numeralD-5
printNumeral("C", numeralD - 5);
}
elseif(numeralD == 4) //if the hundreds number is less than 5 we need to test if it's 4 because in that case we need to avoid
printNumeral("CD"); //printing CCCC. If it's the case we print out CD
else
printNumeral("C", numeralD); //in all other cases we print C for numeralD times. In our example above numeralD is 2 so we print CC
//
num -= numeralD*100; //after this num is 11
numeralX = num/10;
if(numeralX == 9) //same logic as above
printNumeral("XC");
elseif(numeralX >= 5){
printNumeral("L");
printNumeral("X", numeralX - 5);
}
elseif(numeralX == 4)
printNumeral("XL");
else
printNumeral("X", numeralX);
num -= numeralX*10;
// num is now < 9
if(num == 9)
printNumeral("IX");
elseif(num >= 5){
printNumeral("V");
printNumeral("I", num - 5);
}
elseif(num == 4)
printNumeral("IV");
else
printNumeral("I", num);
cout<<endl;
cout<<"do you wish to continue? (y/n)"<<endl;
cin>>result;
}
while ((result=='y') || (result =='Y'));
return 0;
}
Flush clears the output buffer. So when we write statements such as cout << "Please enter a number: "; We think of this string being printed to the screen. That's not actually true the string is sent to the ouput buffer where it waites to be output. By calling std::flush; we force the output buffer to print all character to our output ie. the screen , file, printer, ect.
So why is this important?
well in my program its not. The flush statement on line 20 and 29 could have been omitted. I do this mostly out of habbit. However, lets say you have a log file where you send error messages, and we try to print an error message without flushing the buffer. Then the computer crashes. The error message may not have been output to the log file, and that's the message we probably want.
its worth noting that endl; flushes the ouput buffer