returning arrays in function problem?

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
#include <iostream>
#include <string>
using namespace std;

//fucntions
void intro(string name[subName]);

int main()
{
    string name[5];
    int subName;
    intro(name[subName]);

    cout << string name[5];
}

void intro(string name[subName])
{

    string name[5];
    char redoName;

    do
    {

        cout << "Please type the names of the people you wish to take with you. \n"
             << "You may only bring 5 people. " << endl <<endl;

        int subName;
        for(subName = 0; subName <= 4; subName++)
            {
                cout << "enter name: ";
                cin >> name[subName];
            }

        //outputting all names
        cout << endl;
        for(subName = 0; subName <= 4; subName++)
            {
                cout << name[subName] << endl;
            }

        cout << "\nAre you sure that this is the party you want to take with you? [Y/N] " <<endl;
        cin >> redoName;
    }
    while(redoName == 'n' || redoName == 'N');
}


I am trying to bring back the 5 names to use in main for future use

error subName not declared
Last edited on
There are quite a few errors in your code...

What have you learnt about arrays and strings so far?

void intro(string name[subName]);

Can you see why the compiler is reporting "error subName not declared"?
Last edited on
yeah i know there was a lot of errors.
I tried to mimic a tutorial and put my own values in (with the exception to making it into a function).

the tutorial one worked, but not mine.
Last edited on
i am just trying to put everything in main into a 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
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string name[5];
    char redoName;

    do
    {

        cout << "Please type the names of the people you wish to take with you. \n"
             << "You may only bring 5 people. " << endl <<endl;

        int subName;
        for(subName = 0; subName <= 4; subName++)
            {
                cout << "enter name: ";
                cin >> name[subName];
            }

        //outputting all names
        cout << endl;
        for(subName = 0; subName <= 4; subName++)
            {
                cout << name[subName] << endl;
            }

        cout << "\nAre you sure that this is the party you want to take with you? [Y/N] " <<endl;
        cin >> redoName;
    }
    while(redoName == 'n' || redoName == 'N');
}
Topic archived. No new replies allowed.