Alternative for GetValue

What's an alternative for GetValue, or how would I identify it? It says its unidentified.

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;
}
Function prototype is missing and main should be lower case.

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
62
#include<iomanip>
#include<iostream>
#include<string>
using namespace std;

int GetValue(char); // Function prototype.

int main() // lower case.
{

    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;
};


However, If you place the main function last, then you dont need the function prototype. Like:


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
#include<iomanip>
#include<iostream>
#include<string>
using namespace std;

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;
}

int main() // lower case.
{

    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;

}

Last edited on
Thanks it works now
Great!
Hi,

Please don't do duplicate topics, It's ultimately a time waster for those who reply. The things is, people reply to one topic, then someone else says the same thing in the other one. You now have 3 Topics about the same thing, just keep one of them going - even if you change the code around a lot.

I prefer to have the function declarations before main, then main is always at the top of the file - we don't have to go looking for it :+)

When yo u do have a function declaration (prototype) give the parameters a name so they are the same as the function definition. Parameters can quite often be const, as they should be for the GetValue function.

Cheers :+)
Last edited on
The code works. However, I am having trouble with outputting an error message if its not 'M', 'C', 'L', 'X', 'V' or 'I'. I also want it to output a 0, if the string is empty when entered. Can someone please tell me what I should do for that?

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
62
#include<iomanip>
#include<iostream>
#include<string>
using namespace std;

int GetValue(char); 

int main(){ 

int num = 0;
string ignoreNextChar = "false";

cout << "Enter the Roman Numeral that need to be converted: ";
string roman;
cin >> roman;

for (int i = 0; i < roman.length(); i++)
{
if (ignoreNextChar == "true")
{
ignoreNextChar = "false";
continue;
}
int Value = GetValue(roman.at(i));
if ((i + 1) < roman.length())
{
int nextVal = GetValue(roman.at(i + 1));
if (nextVal > Value)
{
num += nextVal - Value;
ignoreNextChar = "true";
continue;
}
}
num += GetValue(roman.at(i));
}
cout << "The Decimal Value is: " << 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;
};


Thanks
From the other Topic:

TheideasMan wrote:
After calling GetValue(), test the result to see that it is not zero.


Any plans on doing the other things I mentioned there?
yeah, thanks for your help, I got it working.
Topic archived. No new replies allowed.