So i know that it has the strings library but I prefer to create my own splicer for the experience... what i have is not working for only one reason... what the function returns is different than what the array should equal...
It is using a 1D method to hold an array of arrays since my 2d was fighting with me...
using all the code i paste will give you my simple diagnostics which shows that the function is finishing with the correct output, but the output is not becoming the correct input for the new char*... (everything is commented...)
please inspect the return type, the actual return, the creation of my char*'s and the assignment of one char* to another...
#include <iostream>
usingnamespace std;
staticchar* 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
elseif (!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* outputholder= CASplice("type phrase to test here","type delimiter set here...");
for (int i = 0; i <142; ++i)
{
cout << outputholder[i] << " " << i << "\n";
}
cin.get();
}
These are your problems, well the ones the compiler picked up anyhow..
(67): error C2057: expected constant expression
(67): error C2466: cannot allocate an array of constant size 0
(67): error C2133: 'Final' : unknown size
(69): warning C4244: '=' : conversion from 'int' to 'char', possible loss of data
(71): warning C4244: '=' : conversion from 'int' to 'char', possible loss of data
(140): warning C4172: returning address of local variable or temporary
1 2
char* Final = newchar[Pieces * LongCont]; // this is dynamic allocation
delete[] Final; // dont forget clean up when it is no longer needed