assert causing break

Heres my code
https://github.com/rebukeiscool/calculator

The error I'm having occurs at line 136 of operation.cpp.
delete deletethis;

the input string is initially stored in a dynamically allocated array (in getinput()), so I figure I should probably add a delete statement in there when I change the location of the input.

Why is this assert error occurring? The deletethis pointer is pointing to the same location as the input pointer was pointing to (the address returned by 'new char[..]' in getinput() ).
Please post the code inside code tags in your post.
Very well.

1
2
3
4
5
6
7
8
9
10
int main()
{
	while(true){
		char* input=getinput();
		int endset=GetTermLoc(input);
		solveset(input,0,&endset);
		delete input;
	}
	return 0;
}


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
void solveset(char* input, int startset, int* endset){
	DoParenth(input,startset,endset);
	DoExp(input,startset,endset);
	DoMultiDiv(input,startset,endset);
	DoPlusMinus(input,startset,endset);
	return;
}

void DoExp(char* input, int startset, int* endset){
	int counter=startset;
	for(;counter<=*endset;counter++){
		if(input[counter]=='^'){
			int tempcount=counter-1;
			for(;tempisnum;tempcount--){
				if(tempcount<startset) break;
				if(tempcount==0){
					tempcount--;
					break;
				}
			}
			tempcount++;
			int startreplace=tempcount;
			double dleft=0;
			double* pleft=&dleft;
			getTerm(input,tempcount,pleft);
			double dright=0;
			double* pright=&dright;
			
			for(;tempisnum;tempcount++){
				if(tempcount>*endset){
					cout << "error. right term is outside of set" << endl;
					return;
				}
			}
			tempcount++;
			getTerm(input,tempcount,pright);
			double danswer=pow(dleft,dright);
			string stanswer= to_string(danswer);

			for(;tempisnum;tempcount++){}
			int appendhere=tempcount;
			AnsReplace(input,startreplace,appendhere,stanswer);
		}
	}
	return;
}

void getTerm(char* input,int counter,double* term){
	if(!isnum){
		cout << "error in getTerm. current char is "<< input[counter] << endl;
		return;
	}
	int ndigit=10;
	int ndecimal=0;
	for(;isnum;counter++){
		if(input[counter]=='.'){
			if(ndecimal){
				cout << "error in getTerm" << endl;
				return;
			}
			else{
				*term/= power(10,ndigit);
				ndecimal++;
				continue;
			}
		}
		else{
			if(ndecimal){
				if(input[counter]=='0'){
					ndecimal++;
					continue;
				}
				else{
					*term+=(double(input[counter])-48)*power(10,-ndecimal);
					ndecimal++;
					continue;
				}
			}
			else{
				if(input[counter]=='0'){
					ndigit--;
					continue;
				}
				else{
					ndigit--;
					*term+=(double(input[counter])-48)*power(10,ndigit);
					continue;
				}
			}
		}
	}
	if(!ndecimal) *term/=power(10,ndigit);
	return;
}

void AnsReplace(char* input,int beginreplace,int appendhere,string streplace){
	int counter=appendhere;
	int tempcount=0;
	string appendthis="";
	string stinput="";
	for(;input[counter]!='\0';counter++,tempcount++){}
	appendthis.append(&(input[appendhere]),tempcount);
	char* deletethis=&(input[0]);
	input = new char[strlen(input)];
	strncpy(input,deletethis,beginreplace);
	strncpy(&(input[beginreplace]),streplace.c_str(),streplace.size());
	strncpy(&(input[beginreplace+streplace.size()]),appendthis.c_str(),appendthis.size());
	input[beginreplace+streplace.size()+appendthis.size()]='\0';
	int initial=appendhere-beginreplace;
	cout << initial << endl;
	delete deletethis;
	return;
}



Where is the getinput(); function?

I keep functions like getinput() and other functions that I have written that can be used for most applications in a source code file on my desktop.

1
2
3
4
5
6
7
8
9
char* getinput(){
	char* input=new char[50];
	cout << "Enter problem" << endl;
	cin.getline(input,50,'\n');
	int counter=0;
	for(;input[counter];counter++){}
	input[counter]='\0';
	return input;
}


edit:
I apologize for forgetting to add it in.
Last edited on
Look at the following two snippets:
char* input=new char[50];

delete input;
In the first snippet you use new[] but in the second you use delete without the brackets. You should probably be using delete[].

What is the purpose of the loop in getinput()?

the purpose of the loop in getinput() is to append a terminating character after the last valid element in the array.
After all, if the user enters "3+4^3", I can't have
cout << input << endl;
outputting something like
"3+4^3===#%&&%(@(D@#$&T*@"
Since you're using getline() you will always have the end of string character in your string, you don't need to add it yourself.

Topic archived. No new replies allowed.