Breaking Down Strings

I am getting some errors I dont understand with this file. I'm trying to break down this string into pieces. I supposed to take the parts of the string that are within parentheses out and make them into a new string. It is supposed to do this until the string is empty.

|22|error: variable-sized object 'functions' may not be initialized|
|364|error: too many arguments to function 'void* malloc(size_t)'|
|50|error: at this point in file|

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
#include <iostream>
#include <math.h>
#include <strings.h>
#include <stdlib.h>
#include <cstdio>
#include <cstdlib>
using namespace std;

 string getstring (string question) {
    string mystring;
    cout << question;
    cin >> mystring;
    return mystring;
    }

int j,k,n;
string halftheproblem = "";

void breakdownfunction (string function) {
    string newfunction = "(" + function + ")";
    int functionlength = newfunction.size();
    char * functions[functionlength] = newfunction;
    int count = 0;
    for (int i=0; i<=functionlength; i++) {
        if (functions[i]=="(") {
            n = n+1;
        }
        if (functions[i]==")") {
            count = count + 1;
        }
        if (count != n) {
            break;
        }
    }

    do {
        for (int i=0; i<=functionlength; i++) {
            if (functions[i]=="(") {
                j=i;
            }
            if (functions[i]== ")") {
                k=i;
                break;
            }
        }
        for (int i=j; i<=k; i++) {
            string newstring = "";
            newstring = newstring + newfunction[i];
            string * pData;
            pData = (string*) malloc (i,sizeof(string));
            if (pData==NULL) exit (1);
        }
        for (int i=0; i<j; i++) {
            string newstring = "";
            newstring = newstring + newfunction[i];
            halftheproblem = newstring;
        }
        for (int i=k+1; i<=functionlength; i++) {
            string newstring = "";
            newstring = newstring + newfunction [i];
            newfunction = halftheproblem + newstring;
        }
    }
    while(newfunction != "");
    for (int i=0; i<n; i++) {
        cout << pData[n];
    }
}


int main()
{
    string question ="gimme function: ";
    string function = getstring(question);
    breakdownfunction(function);
}

string newfunction = "(" + function + ")";
int functionlength = newfunction.size();
char * functions[functionlength] = newfunction;


You are assigning a variable newfunction of C++ string datatype to an array of char * which may mean only the first element in the array functions is initialized. The rest maybe undefined.


pData = (string*) malloc (i,sizeof(string));


The C function malloc do not take 2 arguments as said by the warning void* malloc(size_t) which indicate only 1 argument.
I made a few notes in your code. I am confused by pData. I am not sure what it is meant to be/do.
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
#include <iostream>
#include <math.h>
#include <strings.h>
#include <stdlib.h>
#include <cstdio>
#include <cstdlib>
using namespace std;

 string getstring (string question) {
    string mystring;
    cout << question;
    cin >> mystring;
    return mystring;
    }

int j,k,n;
string halftheproblem = "";

void breakdownfunction (string function) {
    string newfunction = "(" + function + ")";
    int functionlength = newfunction.size();

    // Don't bother with char* just use string
    // char * functions[functionlength] = newfunction;
    int count = 0;
    for (int i=0; i<=functionlength; i++) {
        if (newfunction[i]=='(') { // note single quotes ' not "
            n = n+1;
        }
        if (newfunction[i]==')') { // note single quotes ' not "
            count = count + 1;
        }
        if (count != n) {
            break;
        }
    }

    do {
        for (int i=0; i<=functionlength; i++) {
            if (newfunction[i]=='(') {
                j=i;
            }
            if (newfunction[i]== ')') {
                k=i;
                break;
            }
        }
        for (int i=j; i<=k; i++) {
            string newstring = "";
            newstring = newstring + newfunction[i];

            // What is pData supposed to do?
            string * pData;
//            pData = (string*) malloc (i,sizeof(string));

            if (pData==NULL) exit (1);
        }
        for (int i=0; i<j; i++) {
            string newstring = "";
            newstring = newstring + newfunction[i];
            halftheproblem = newstring;
        }
        for (int i=k+1; i<=functionlength; i++) {
            string newstring = "";
            newstring = newstring + newfunction [i];
            newfunction = halftheproblem + newstring;
        }
    }
    while(newfunction != "");
    for (int i=0; i<n; i++) {
       // cout << pData[n]; // what is pData supposed to be?
    }
}


int main()
{
    string question ="gimme function: ";
    string function = getstring(question);
    breakdownfunction(function);
}
I am confused by pData. I am not sure what it is meant to be/do.


Good question I am also confused. Is this code written by you or by others ? It just malloc and then never assign values to it. Maybe someone can understand the original author intention ?
Also note that you should NEVER allocate complex types like string with malloc (unless you use placement new later) as malloc does not call the constructor.

Using string without having its constructor called would have disasterous results.
Yea, I see where I didnt assign Pdata, its supposed to pick up the newstring above it.
I think you should just make it an ordinary string. And you need to declare it OUTSIDE the loop otherwise it will be destroyed when the loop exits:
1
2
3
4
5
6
7
8
9
10
11
12
13
std::string pData;
do
{
    // ... stuff

    pData = newstring;

    // ... stuff
} while(newfunction != "")

    for (int i=0; i<n; i++) {
        cout << pData[n];
    }
Topic archived. No new replies allowed.