Note that there are some explanatory texts on larger screens.

plurals
  1. POConverting roman numerals into numbers using find statement in C++
    primarykey
    data
    text
    <p>Hi I am having trouble converting roman numerals into normal numbers in C++, the code works to a degree however it if the numerals (XIV 14 or LIV etc) are entered it will out put 15 or 55. I have tried to implement the find statement however I have no real idea how to use it to solve my problem, here is a copy of my code so far;</p> <pre><code>int convNum; int total = 0; string romanNum; const string units [10]= {"0","I","II","III","IV","V","VI","VII","VIII","IX"}; const string tens [10]= {"0","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"}; const string hundreds [10]= {"0","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"}; const string thousands [4]= {"0","M","MM","MMM"}; string input; while(!cin.eof()){ cin&gt;&gt;romanNum; if(cin.fail()){ break; }else{ for(int i=0; i&lt;romanNum.length(); i++){ romanNum[i]=toupper(romanNum[i]); } for(int y=3; y &gt; 0; y--){ if(romanNum.find(thousands[y])!= string::npos){ total += y*1000; input.erase(0,thousands[y].length()); break; } } for(int y=9; y &gt; 0; y--){ if(romanNum.find(hundreds[y])!= string::npos){ total += y*100; input.erase(0,hundreds[y].length()); break; } } for(int y=9; y &gt; 0; y--){ if(romanNum.find(tens[y])!= string::npos){ total += y*10; input.erase(0,tens[y].length()); break; } } for(int y=9; y &gt; 0; y--){ if(romanNum.find(units[y])!= string::npos){ total += y; input.erase(0,units[y].length()); break; } } cout &lt;&lt; total &lt;&lt; endl; total = 0; } for(int k=0; k &lt; romanNum.length(); k++){ input[k] = romanNum[k]; } } return 0; </code></pre> <p>}</p> <p>If anyone can help me with this it will be greatly appreciated, as I am a beginner and coding this amount of C++ code took me around 2 weeks to code.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload