Passing a "string array" into a function

Feb 15, 2019 at 4:16am
Hey, im doing a project for class and I have to convert a user entered number into roman numerals. I have 3 criteria to meet
1. read the conversion data from file into an array
2. have a loop that allows the user to continue entering numbers until an end sentinel such as 0 is entered.
3. create a user-defined function that takes arguments of an integer ( between 1-20) , as well as the data conversion array and its size. The function will return or display the corresponding Roman numeral.

I think I have the logic down but im getting errors when I try to pass the "string array" into my function. Im really stuck, can anyone shed some light?

#include <string>
#include <iostream>
#include <fstream>
#include <iomanip>
void display(string array[], int num);


using namespace std;

int main()
{

string numeralArray[19];
string line;
string infile;
int userNum = 1;
int size = 1;
int start = 1;


ifstream file("Homework2.txt");
if (file.is_open())
{


for (int i = 0; i < 5; ++i)
{
file >> numeralArray[i];
}
cout << numeralArray[3] << endl;

while (userNum != 0)
{
cout << " Enter a number between 1-20 or 0 to exit" << endl;
cin >> userNum;
if (userNum != 0)
{
display(numeralArray, userNum);
}

}









system("pause");
}

}

void display(string array[], int num)
{

}
Feb 15, 2019 at 5:16am
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
#include <string>
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

void display(string array[], int num);


int main()
{
    string numeralArray[19];
    string line;
    string infile;
    int userNum = 1;

    ifstream file("Homework2.txt");

    if (file.is_open())
    {
        for (int i = 0; i < 5; ++i)
        {
            file >> numeralArray[i];
        }
        
    cout << numeralArray[3] << endl;

    while (userNum != 0)
    {
        cout << " Enter a number between 1-20 or 0 to exit" << endl;
        cin >> userNum;
        
        //this if statement is useless. condition already checked by while loop
        if (userNum != 0)
        {
            display(numeralArray, userNum);
        }
    }

    system("pause");
    
    return 0;
}

}

void display(string array[], int num)
{
    cout << "\n" << array[num] << "\n";
}


Since I don't have the file and its contents, as far as I can tell your code is correct.

The error you're getting is line 36 "display is not declared in this scope". You have a function prototype and a function definition, but the latter is empty.

Hope this helps
Last edited on Feb 15, 2019 at 5:19am
Feb 15, 2019 at 5:56am
thanks for the reply! the text file is just roman numerals, I know what to do inside the function but didnt fill it out yet. I was getting these error messages

E0308 more than one instance of overloaded function "display" matches the argument list: 38
Error C2065 'string': undeclared identifier 5
Error C2146 syntax error: missing ')' before identifier 'array' 5 C3861 'display': identifier not found 38



Feb 15, 2019 at 6:31am
> E0308 more than one instance of overloaded function "display" matches the argument list: 38
> Error C2065 'string': undeclared identifier 5
Well in your first code, the first problem is your prototype is BEFORE your using namespace.

Also, 'display' is such a common word that it could well be defined somewhere in your header files, leading to the "more than one instance" problem.



Feb 16, 2019 at 4:52pm
Dude thank you so much! Appreciate the help
Topic archived. No new replies allowed.