May 31, 2016 at 1:11pm UTC
What's an alternative for GetValue, or how would I identify it? It says identifier not found.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
#include<iomanip>
#include<iostream>
#include<string>
using namespace std;
int Main()
{
int num = 0;
string ignoreNextChar = "false" ;
string roman;
cin >> roman;
for (int i = 0; i < roman.length(); i++)
{
if (ignoreNextChar == "true" )
{
ignoreNextChar = "false" ;
continue ;
}
int anyBloodyNameHere = GetValue(roman.at(i));
if ((i + 1) < roman.length())
{
int nextVal = GetValue(roman.at(i + 1));
if (nextVal > anyBloodyNameHere)
{
num += nextVal - anyBloodyNameHere;
ignoreNextChar = "true" ;
continue ;
}
}
num += GetValue(roman.at(i));
}
cout << num << endl;
}
int GetValue(char romanNumeral)
{
switch (romanNumeral)
{
case 'M' :
return 1000;
case 'D' :
return 500;
case 'C' :
return 100;
case 'L' :
return 50;
case 'X' :
return 10;
case 'V' :
return 5;
case 'I' :
return 1;
}
return 0;
}
Last edited on May 31, 2016 at 1:12pm UTC
May 31, 2016 at 1:13pm UTC
You should at least declare, if not define GetValue() before main(), which by the way, should be main(), not Main().
Last edited on May 31, 2016 at 1:13pm UTC
May 31, 2016 at 1:21pm UTC
I did that, but how exactly would you do it. I put void GetValue(), and it says that function does not take 1 arguments
May 31, 2016 at 1:27pm UTC
Hi,
A few things:
ignoreNextChar could be of type bool
, then line 18 could just be if (ignoreNextChar)
length() returns a std::size_t type not an int
If values must positive, consider using one of the unsigned types.
After calling GetValue() , test the result to see that it is not zero.
Try to avoid having using namespace std;
Put std:: before each std thing - it's the easiest and best way in the end, all the experienced coders do this.
Good Luck !!