Trying to call String Array, with data taken from a file ,into a Function

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

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
 #include "pch.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <iostream>
using namespace std;
void inputNumber(int Num, string romanNumerals, const int size);
int main()
{
	const int 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, const int 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;
		}
	}
}
I prefer pointer syntax for this.
try:

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
}

the call to the function is just fine like this.
Last edited on
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 const int SIZE = 21; and change line 11 to short loop=1;
Registered users can post here. Sign in or register to post.