Why does the content in my char* change into random characters?

I want to write a code which can read math equation, I'm not sure if there is another simpler way. Right now, this is what i have:

textprocessor.cpp(or main)
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
#include <iostream>
#include <string>
#include "obj.h"
#include <iomanip>
using namespace std;



int main(){
    cout << setprecision(10);
    obj one;
    char *chrp;
    chrp = "123+1.1234567+1234567=";
    cout << chrp;

    cout << endl << endl;
    one.recon(chrp);
    cout <<endl;

    for (int i = 0; i<one.equation.size(); i++)
    {
        cout<< one.equation[i].number <<"  "<< one.equation[i].sign << "\n";
    }

    return 0;
}


obj.h(header)
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
#include <string.h>
#include <iostream>  //stringstream()
#include <stdlib.h> //atoi()
#include <vector>


using namespace std;

struct mathcomp
{
    long double number;
    bool numorsign;
    const char* sign;
};

class obj
{




      public:
          vector<mathcomp> equation;//temporary putting it in
                                    //public for easier debug
          void recon(char*&);
          void spaceremoval(char*&text);
};


obj.cpp
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
#include "obj.h"
#include <string.h>
#include <iostream>

#define nonnumeric ((text[0]=='+')||(text[0]=='-')||(text[0]=='*')||(text[0]=='/')||(text[0]=='=')||(text[0]=='='))
#define numeric ((text[0]=='1')||(text[0]=='2')||(text[0]=='3')||(text[0]=='4')||(text[0]=='5')||(text[0]=='6')||(text[0]=='7')||(text[0]=='8')||(text[0]=='9')||(text[0]=='0')||(text[0]=='.'))

#define nonnumeric2 ((text[ii]=='+')||(text[ii]=='-')||(text[ii]=='*')||(text[ii]=='/')||(text[ii]=='='))
#define numeric2 ((text[ii]=='1')||(text[ii]=='2')||(text[ii]=='3')||(text[ii]=='4')||(text[ii]=='5')||(text[ii]=='6')||(text[ii]=='7')||(text[ii]=='8')||(text[ii]=='9')||(text[ii]=='0')||(text[ii]=='.'))

using namespace std;




void obj::recon(char* &text)
{
        if numeric
        {
            for(int ii = 0; ii<strlen(text); ii++)
            {
                if nonnumeric2
                {
                    string des = text;////initializing
                    char buffer[125];
                    int len;
                    mathcomp datastruct;
                    cout << "extracting number:" << endl;

                    len = des.copy(buffer, ii+1, 0); // copying the first number found
                    datastruct.number = atof(buffer);
                    cout <<"length of number copied : " <<len << "\n";

                    cout <<"number copied: " << datastruct.number << endl;//assigning the number copied
                    datastruct.numorsign = 0;                             //to a struct(datastruct) then the stuct is
                    equation.push_back(datastruct);                       //added into a vector in the class(equation)

                    text = text+ii;                                       //remove the number in the string
                    cout << "the rest of the equation : " << text <<endl; //then enter recursive which will stop when
                    cout <<endl;                                          //text becomes empty
                    recon(text);
                    break;
                }
            }
        }
        if (nonnumeric)////////////////////////wat happen over here...
        {
            for(int ii = 0; ii<strlen(text); ii++)
            {
                if (numeric2)
                {
                    string str(text);                  //almost same like what happen in the previous part
                    char buffer[10];
                    int len = str.copy(buffer, ii,0);
                    buffer[len] = '\0';
                    mathcomp datastruct;
                    cout << "copying char:" << endl;
                    cout << "number of char copied: " << len << endl;

                    datastruct.sign = buffer;
                    datastruct.numorsign = 1;
                    datastruct.number = 0;
                    equation.push_back(datastruct);
                    cout << "characters in buffer: " << buffer << endl;
                    cout << "characters in datastruct.sign: " << datastruct.sign << endl;
                    cout << "the rest of the text: " << text << endl;
                    cout << endl;

                    text = text+1;
                    recon(text);
                    break;
                }
            }

        }



}
void obj::spaceremoval(char*&text) //remove the space in the string
{
    string str = text;

    for(int i = 0; i < str.size(); i++)
    {
        if (str[i] == ' ')
        {
            str.erase(str.begin()+i);
        }
    }
    text = const_cast<char*>(str.c_str());
    cout << text << endl;
}


when running the code, the number copying part doesn't seem to has any problems. But the char copying part, char copied into my buffer work fine, but the chars copied into structure turn out to be something weird.

the equation to be copied is :
123+1.1234567+1234567=


but in the vector(number display first followed by char display at right)
1
2
3
4
5
123           ♫ôD   //first member in vect, nothing wrong i guess...
0             (     // i don't know  where the '(' come from, it should be '+'
1.1234567     .     //third member in vect, nothing wrong i guess...
0                   //can't figure out where is the char i copied gone to...
1234567       H°(   //fifth member in vect, nothing wrong i guess... 
In this structure
1
2
3
4
5
6
struct mathcomp
{
    long double number;
    bool numorsign;
    const char* sign;
};

there is no space allocated to store the data part of sign.

Instead at line 60 of obj.cpp there is this:
datastruct.sign = buffer;
which copies the address of buffer to sign.
And what is buffer?
It is a local variable defined at line 53
char buffer[10];

As soon as buffer goes out of scope, it no longer exists. The address stored in sign is now pointing to an object which no longer exists, that location in memory could contain anything.

You could instead of a pointer, use a char buffer (an array of characters) and use strcpy(sign, buffer) or just use a std string.

If the intention that sign should be just a single character, then don't bother with a string, just use a plain char.
You are right, my problem solved, thanks!! :D
Topic archived. No new replies allowed.