For a class assignment we are to take the roman numerals from 1-20 (I-XX) and place them in an array. We are then supposed to create a function that asks the user to input a number 1-20 and return the roman numeral equivalent (Ex. If the user inputs "5", the console should return "V"). I am having difficulty passing the array of roman numeral values that I imported into my function
#include "pch.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <iostream>
usingnamespace std;
void inputNumber(int Num, string romanNumerals, constint size);
int main()
{
constint SIZE = 20; //Size of array
string romanNumerals[SIZE]; //Decided to create a string array since roman numerals are alphabet characters, like XVI and XVII for instance.
short loop = 0; //Imported file, RomanNumerals.txt, has each roman numeral value listed one line at a time, so variable loop will be used as a counter for each line.
string line; //line is being used to place whatever value is on that line into the "loop" position of the array.
int num; //User inputted value
ifstream myfile("RomanNumerals.txt");
cout << "Input a number between 1 and 20.\n";
cin >> num;
if (myfile.is_open()); //This will put each value per line of the text file into each consecutive position of the array, so the value "I" on the first line will be at romanNumerals[0] for example.
{
while (!myfile.eof())
{
getline(myfile, line);
romanNumerals[loop] = line;
cout << romanNumerals[loop] << endl;
loop++;
}
}
inputNumber(num, romanNumerals, SIZE);//Function call
return 0;
}
void inputNumber(int Num,string romanNumerals, constint size)//Function that will take the users input and display the corresponsing roman numeral, for simplicity, I will do if statements for each value.
{
for (int i = 0; i <= 19; i++)
{
romanNumerals[i];
if (Num = 1)
{
cout << romanNumerals[0] << endl;
}
}
}
void inputNumber(int Num, string *romanNumerals, const int size)
{
romanNumerals[i]; //get one string from the array.
romanNumerals[i][j]; //access one letter of one string from the array
but just to get started, try
cout << romanNumerals[0]; //just do this so you can see what I am telling you, then build up from there
}
Since you want romanNumerals[i] to be the string representation of i, you actually need an array size of 21, not 20. Remember, in C++ an array of size N is accessed with indices 0 to N-1.
This also means you want to store the first string from the file into romanNumerals[1], not romanNumerals[0].
To fix these, change 11 to constint SIZE = 21; and change line 11 to short loop=1;