How do i return an array?

when i return the function i do not do it properly...
compile it, and you will see what i mean!

*n does not contain what *Final returned, and if it does it is just partially what it equals...

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <iostream>

using namespace std;

static char* CASplice (char* List, char* Splicers)
{
    //Found represents whether a splice was found
    bool Found = false;
    //Last represents whether the last char was a splice
    bool Last = true;
    //Pieces represents the number that the Final[][] will initialize the Final[x][]
    //It starts at 1 because Final[0] represents the number of strings
    int Pieces = 2;
    //Represents the number of chars in the string initially given (always has a null)
    int CharCount = 1;
    //Contains the number of chars in the longest char* that will be made (Will need room for two ints and a null char)
    int LongCont = 3;
    //Contains the current number of chars needed to create the current char* (always has a null)
    int CurCont = 1;
    //For loop to iterate through each char in List[]
    for (int c=0; List[c]!=0; ++c)
    {
        //For loop to iterate through each Splicer
        for (int s=0; Splicers[s]!=0 && !Found; ++s)
        {
            //Checks each char for a splice
            if (Splicers[s]==List[c])
            {
                //If Last was a splice then pieces does not increment
                if (!Last && List[c + 1]!=0)
                {
                ++Pieces;
                cout << "   new piece" << Splicers[s] << List[c] << Pieces << "\n";
                }
                else
                {
                cout << "no new piece" << Splicers[s] << List[c] << Pieces << "\n";
                }
                Found = true;
            }
        }
        //If char is not a splice then...
        //    1. increase the char count by one
        //    2. increase the CurCont
        if (!Found)
        {
            ++CharCount;
            ++CurCont;
            //If the CurCont is greater than LongCont, set LongCont to CurCont
            if(CurCont>LongCont)
            {
                LongCont=CurCont;
            }
        }
        else
        {
            CurCont = 1;
        }
        //Set Last to the value of found
        Last = Found;
        //Reset default value of Found
        Found = false;
    }
    
    //Create Char* to hold all values (It will act as a char**)
    cout << Pieces * LongCont << " " << Pieces << " " << LongCont << "\n";
    char Final[Pieces * LongCont];
    //Set first value to hold the number of strings
    Final[0] = Pieces;
    //Set second value to hold the number of chars held in each string
    Final[1] = LongCont;
    //Give null value to end of the string
    Final[2] = '\0';
    //Set all values to null
    for (int i = 3;i <= Final[0]*Final[1]; ++i)
    {
        Final[i] = '\0';
    }
    //s holds the current string line
    int l=1;
    //p holds the current char position
    int p=0;
    //reset Found
    Found = false;
    //reset Last
    Last = true;
    //For loop to iterate through each char in List
    for (int c = 0;List[c]!=0;++c)
    {
        //For loop to iterate through each char in Splicers
        for (int s = 0;Splicers[s]!=0&&!Found;++s)
        {
            //If splicer equals char then set found to true
            if (List[c]==Splicers[s])
            {
                Found = true;
            }
        }
        //If found and the last char was not a splice
        if (Found && !Last)
        {
            //Increment l by one to move to the next array indice
            ++l;
            //Set position to 0
            p = 0;
            //this is so you can see the output nicer...
            cout << "\n";
        }
        //If not found
        else if (!Found)
        {
            //Set final[l][p] = List[c]
            Final[(Final[1]*l) + p] = List[c];
            //this is so you can see the output nicer...
            cout << List[c] << " Sent to position" << Final[1]*l + p << "\n";
            //This increments the position by one
            ++p;
        }
        //Set last to represent whether the last char was splice
        Last = Found;
        //Reset Found
        Found = false;
    }
    //Also just so that you can see that it came out properly...
    for (int i; i<Final[0]*Final[1];++i)
    {
        cout << Final[i] << " " << i << "\n";
    }
    //Also just to exemplify that it works...
    for (l = 0; l < Final[0]; ++l)
    {
        for (p = 0; Final[l*Final[1]+p]!=0; ++p)
        {
            cout << Final[l*Final[1]+p];
        }
        cout << ".\n";
    }
    cout << "\n";
    //Returns final
    return Final;
}
int main()
{
    char* n = CASplice("phrase % TOTEST#*m.b,","* ,.!?/");
    for (int i = 0; i <142; ++i)
    {
        cout << n[i] << " " << i << "\n";
    }
    cin.get();
}
Last edited on
Topic archived. No new replies allowed.