Roman numerals to integer using string (menuoptions)

Hello, im trying to create a menu where there should be different options, for example converting roman to normal numbers. Im not very good at programming so I need some help. I have combined 2 different programmes that work by themselves but dosent work together. Will someone please help me? :)


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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
 #include <iostream>
#include <string>

using namespace std;
int main()
{

	int menyval;
	bool fortsatt = true;
	
	do
	{
		cout << " 1 - Tabell Romerska siffror.\n";
		cout << " 2 - Omvandla romerska siffror\n";
		cout << " 3 - mllmml.\n";
		cout << " 4 - Avsluta.\n";
		cout << " Enter your choice and press return: ";

		cin >> menyval;

		switch (menyval)
		{
		case 1:
			cout << "...\n";
			int value(char roman)
			{ //Here's where the problem is
				switch (roman)
				{
				case 'I':return 1;
				case 'V':return 5;
				case 'X':return 10;
				case 'L':return 50;
				case 'C':return 100;
				case 'D':return 500;
				case 'M':return 1000;
				}
			}

			//Function to convert Roman Numerals to Integer
			int romanToInt(string A)
			{
				int i, n, ans = 0, p = 0;
				n = A.length() - 1;

				for (i = n; i >= 0; i--)
				{
					if (value(A[i]) >= p)
						ans = ans + value(A[i]);
					else
						ans = ans - value(A[i]);

					p = value(A[i]);
				}
				return ans;
			}

			//Main Function
			int main() {

				string s;
				int num;
				cin >> s;
				num = romanToInt(s);
				cout << num << "\n";
			}
			break; //You can ignore the rest if u want, just wanted to show the full picture.
		case 2:
			
			cout << "Ange ett romerskt tal\n";
			
			break;
		case 3:
			cout << "Deluppgift 2\n";
			// rest of code here
			break;
		case 4:
			cout << "End of Program.\n";
			fortsatt = false;
			break;
		default:
			cout << "Not a Valid Choice. \n";
			cout << "Choose again.\n";
			cin >> menyval;
			break;
		}

	} while (fortsatt);
	return 0;
} 
Last edited on
You have in line 5 int main() and again -- within this first main -- on line 59 once more. Merging two running programs together into a single new one, I would take the menu loop as main and call from there the others as subroutines.
Im very thankful for the help and I removed the int main on line 59 but I have the same problem unfortunately.
Do you think there could be a problem with me using "switch" multiple times like this(line 21):

1
2
3
4
5
6
7
 switch (menyval) //here
		{
		case 1:
			cout << "...\n";
			int value(char roman)
			{ //Here's where the problem is
				switch (roman) //and then here  
Line 40-55: You're trying to embed a function inside main(). You can't do that. Move those lines between 4 and 5. However, when you do that, value becomes undefined.

Line 25: What are you trying to do here? You're missing a ; With a ; it looks line a function template. If you're trying to initialize value with roman, roman is undefined and char shouldn't be there.

Line 40-55: Are you saying I need to move line 40-55, like copypaste move?

Line 25: I'm not quite sure, my teacher told me to do something like that and unfortunately I didnt ask any questions about it. But thanks for your help, I'll write back to let you know how Im doing :)
Did you have a look at the Tutorials of this site? Reading the chapters
Basics of C++ and Program structure could be beneficial. May be you get the answers to the questions you didn't dare to ask you teacher.

Suggestion, alas not working correctly, it's just an idea how to define functions and call them from main():
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>     // std::cout, cin
#include <string>
#include <limits>       // std::numeric_limits
using namespace std;

int value(char roman)
{ //Here's where the problem is
	switch (roman)
	{
		case 'I':return 1;
		case 'V':return 5;
		case 'X':return 10;
		case 'L':return 50;
		case 'C':return 100;
		case 'D':return 500;
		case 'M':return 1000;
	}
    return 0; // to avoid compiler warning
}

//Function to convert Roman Numerals to Integer
int romanToInt(string A)
{
	int i, n, ans = 0, p = 0;

	n = A.length() - 1;
	for (i = n; i >= 0; i--)
	{
		if (value(A[i]) >= p)
			ans = ans + value(A[i]);
		else
			ans = ans - value(A[i]);

		p = value(A[i]);
	}
	return ans;
}

int main()
{

	int menyval;
	bool fortsatt = true;

// moved here from case 1:
				string s;
//				int num;
	
	do
	{
		cout << " 1 - Tabell Romerska siffror.\n";
		cout << " 2 - Omvandla romerska siffror\n";
		cout << " 3 - mllmml.\n";
		cout << " 4 - Avsluta.\n";
		cout << " Ange ditt val och tryck på Retur: ";

		cin >> menyval;

		switch (menyval)
		{
		case 1:
			cout << "...\n";

//Main Function ... of menu selection 1, but not in the logic of C++
//			int main() {

    cin.ignore( numeric_limits <streamsize> ::max(), '\n' );
				cin >> s;
				cout << "Input is " << s;
				cout << romanToInt(s) << "\n";
//			}
			break; //You can ignore the rest if u want, just wanted to show the full picture.
		case 2:
			
			cout << "Ange ett romerskt tal\n";
			
			break;
		case 3:
			cout << "Deluppgift 2\n";
			// rest of code here
			break;
		case 4:
			cout << "End of Program.\n";
			fortsatt = false;
			break;
		default:
			cout << "Not a Valid Choice. \n";
			cout << "Choose again.\n";
			cin >> menyval;
			break;
		}

	} while (fortsatt);
	return 0;
}
Last edited on
I will have myself a look, thank you alot
Sorry that my example does not work as I expected. I have edited it, inserted a line before and one after cin >> s; in line 68 (and another #include), no clue why it does not end. Looks as if it ignores hit Enter or Return.

In addition, if I run it under http://www.cpp.sh/ it starts only once. if I press Stop it never runs again. I have to quit and reopen a new cpp.sh session.
cpp.sh has sandbox issues. It is not reliable.

In your code, choose either line-based input or element-based input, but be consistent.

Nothing in that program requires you to cin.ignore().
Topic archived. No new replies allowed.