Roman Numbers

Extend the program so that it can deal with single digit numbers of any value. A single digit number is one that consists only of thousands, hundreds, tens, or units. Thus **LXX** (70) and **CD** (400) are single digit numbers, but **XIV** (14) and **MC** (1100) are not. Use the same approach as for units digits, but with 4 different arrays, one each for the thousands, hundreds, tens, and units digits. Try looking for thousands digits first, then for hundreds, and so on. When you find a match in one of the arrays, print the corresponding value and stop.

Modify the program so that it reads and converts all input numbers until end of file (eof) on standard input. You will probably be able to do this by simply adding an appropriate reading loop around the code that reads a single line.

<code>
#include <array>
#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

int main(int argc, char *argv[]) {
if (argc != 2) { return 0; }
std::string romanSingle[10] = {" ", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
std::string romanTens[10] = {" ", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
std::string romanHundreds[10] = {" ", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
std::string romanThousands[4] = {" ", "M", "MM", "MMM"};

string num;// = std::string(argv[2]);
//cin >> num;
std::transform(num.begin(), num.end(), num.begin(), ::toupper);

for (int i = 0; i < 4; i++) {
if (romanThousands[i] == num) {
std::cout << i * 1000 << " ";
break;
}
}

for (int i = 0; i < 10; i++) {
if (romanHundreds[i] == num) {
std::cout << i * 100 << " ";
break;
}
}
for (int i = 0; i < 10; i++) {
if (romanTens[i] == num) {
std::cout << i * 10 << " ";
break;
}
}
for (int i = 0; i < 10; i++) {
if (romanSingle[i] == num) {
std::cout << i << " ";
break;
}
}
}

<code>
and the C++ question is?
Runs without any error, but does not write output in the console. Unable to find why?
> string num;// = std::string(argv[2]);
> //cin >> num;
Pick ONE.

Either you pass the string on the command line, or you type it in when the program is running.

First things first:
PLEASE learn to use code tags, they make reading and commenting on source code MUCH easier.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.

Some formatting & indentation would not hurt either

CPlusPlus doesn't use HTML tags, it uses BB Code tags ( [ ] brackets instead of angle braces < > )

Wali23 wrote:
Runs without any error, but does not write output in the console.

Well, you've commented out the two methods you wrote for obtaining a number from the user. No number to process, no output:
1
2
string num;// = std::string(argv[2]);
//cin >> num; 

You are checking the THIRD command-line argument, not the second. Arrays are zero-based in C/C++.

Are you sure you want to require at least that many arguments, 3 or more? my_app 12345 makes more sense if you want command-line input.

Getting input via std::cin makes more sense for getting repeated input via a loop for testing purposes. Easily rewritten for file input after done testing with console input. No need to change the loop.
Topic archived. No new replies allowed.