Hello, I've gotten the apparently classic programming student problem of "here's a text file with a bunch of roman numerals, do math with them." My professor requires that we use this list of functions, and make our main function in a couple lines of calling other functions. I'm not allowed to use global variables, and my solution for each part must be in an individual function.
1 2 3 4 5
|
string dataInput() // this reads from a .txt file, included in the next code block
int romanToDecimal(char r) // takes in a roman numeral as an input, and outputs an integer. I got this bit working, but I'm including everything I can.
char get_Oper(ifstream&) // this is the main one I'm having issues with. The textbook we have doesn't cover this without getting far too advanced.
void calcRomans(int n1, int n2, char operand, int&) // inputs two integers and an operator. I'm mainly confused with the int& here, as it's implied in the assignment that it should be an output.
void printResult(int result) // this is where the int from calcRomans() is supposed to end up.
|
So there's a lot of functions there. I have personally solved this in like, 80 lines of code in a single function, but that's not allowed on this assignment. Right now, I have the following set of code for the get_Oper function.
1 2 3 4 5 6 7 8 9 10
|
string dataInput() { //string data input
ifstream fileInput; //opens file input stream
fileInput.open("mp4data.txt"); //opens file
}
char get_Oper(ifstream&fileInput) {
string testString;
getline(fileInput, testString);
cout << testString;
}
|
And finally, here's the text file. There's tons of whitespaces which is annoying, but I'll find a solution for that easily I'd imagine.
1 2 3 4 5 6 7 8 9
|
MCCXXVI CV +
MCCXXVI MCCXXVI /
V I -
MDCLXVI III *
DL DXXXXVIII -
D L /
MDI CXI +
XXV IIII /
XI CII *
|
The goal is to take each line, get the roman numerals from that line, and do the operation that's listed last. I can work with that, my question is more about referencing an ifstream as a parameter for a function. I could be massively misunderstanding, but I don't think this is possible.
Edit: I've realized my mistake for the get_Oper function, as I need to use getline() instead of trying to directly convert, but I still don't get any output.