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.
//This is romanType.cpp file.
#include <iostream>
#include "romanType.h"
usingnamespace 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;
}
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?
//This is romanType.cpp file.
#include <iostream>
#include "romanType.h"
usingnamespace 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.
//This is romanType.cpp file.
#include <iostream>
#include "romanType.h"
usingnamespace 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;
}
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.