Array Help.

My professor asked us to write a program that converts roman numerals into arabic numerals using classes and a separate header file. I was able to do that, I'm sad to say that I didn't pay enough attention when arrays were being taught. I manually entered my array, but I need to make it so a user enters the roman numerals. I was thinking array[size]. Then cin >> size, But I don't want to limit the size of the characters entered. Any help is appreciated.

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
//This is romanType.cpp file.

#include <iostream>
#include "romanType.h"

using namespace std;

int main()
{

	romanType myType;
	char type;

	cout << "Please enter in a series of Roman Numerals (M C D L X V I):" << endl << endl;


	int i;
	int total = 0;
	char array[6] = {'C','C','C','L', 'I', 'X' };

	

	for (i = 0; i < 6; i++)
	{
		
		cout << array[i];

		myType.setType(array[i]);
		total = myType.convertType() + total;	
	}

	cout << " in arabic form is " << total << endl <<endl;

	system("pause");
	return 0;

}




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
//This is romanType.h header file

#include <iostream>

using namespace std;

class romanType{

public:
	void setType(int);
	int getType();
	int convertType();
	romanType();
	

private:
	int currentType;
};

void romanType::setType(int type)
{
	currentType = type;	
}

int romanType::getType()
{
	return currentType;
}
int romanType::convertType()
{
	int m1 = 1000;
	int d1 = 500;
	int c1 = 100;
	int l1 = 50;
	int x1 = 10;
	int v1 = 5;
	int i1 = 1;

	switch (currentType)
	{
	case 'M':
		return m1;
		break;
	case 'D':
		return d1;
		break;
	case 'C':
		return c1;
		break;
	case 'L':
		return l1;
		break;
	case 'X':
		return x1;
		break;
	case 'V':
		return v1;
		break;
	case 'I':
		return i1;
		break;

	default:
		cout << "Invalid \n";
	}
}


romanType::romanType()  //default constructor
{

}
1
2
3
4
string array;
cin >> array;
for( int i = 0; i < array.length(); ++ i ){
}


is this what you want ???
Ok so, I was able to make it so that the program asks user to enter in roman numerals. However I set the the array to have size of 5. The program does not work unless the user enters in 5 characters. How can I make it so it works all the time no matter how many characters the user enters?


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
//This is romanType.cpp file.

#include <iostream>
#include "romanType.h"

using namespace std;

int main()
{

	romanType myType;
	char type;

	cout << "Please enter in a series of Roman Numerals (M C D L X V I):" << endl << endl;


	int i;
	int total = 0;
	char array[5];
	
    
	

	for (i = 0; i < 5; i++)
	{
		cin >> array[i];
		cout << array[i];

		myType.setType(array[i]);
		total = myType.convertType() + total;
        
	}

	cout << " in arabic form is " << total << endl <<endl;

	system("pause");
	return 0;

}
I seem to have figured a work around. But if anyone can help me find a way where it doesn't have to ask the user how many characters he's going to enter then that would be better.

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
//This is romanType.cpp file.

#include <iostream>
#include "romanType.h"

using namespace std;

int main()
{

	romanType myType;
	char type;
    int size;
	
   
    cout << "How many characters will you be entering?" << endl << endl;
    cin >> size;
    
    cout << endl << endl << "Please enter in a series of Roman Numerals (M C D L X V I):" << endl << endl;
    
	int i;
	int total = 0;
	char array[size];
	
    
	

	for (i = 0; i < size; i++)
	{
		cin >> array[i];
		cout << array[i];

		myType.setType(array[i]);
		total = myType.convertType() + total;
        
	}

	cout << " in arabic form is " << total << endl <<endl;

	system("pause");
	return 0;

}
1
2
3
4
cin >> size;
//...

char array [ size ] // this will not work ! 

Your code will not work since the size of the array must be determined at compile time, you will need dynamic memory allocation to be able to do that.

As @rmxhaha said use a std::string, for now just think of this as a char array that automatically expands whatever the length of your input is.

Edit

or if you want you can just use a fix sized array: char array [ 50 ] i think is just enough.


I didn't pay enough attention when arrays were being taught

Array tutorial : http://www.cplusplus.com/doc/tutorial/arrays/
Last edited on
Yeah the way I had before was with a fixed array size. But if i put char array[50] the user actually has to enter in 50 characters or else the program doesnt work.
The number of input doesn't have to be 50 ?

The problem is in the condition in the for loop, you can use
strlen function as a substitute for size:

1
2
3
for ( int i = 0; i < strlen ( array ); i++ ) {
    // convert
}


EDIT
and #include <cstring> for the strlen function to work
Last edited on
Topic archived. No new replies allowed.