RunTime: String is not NULL terminated

Hello all!

Please don't close this because I'm about to say I've run into issues on a homework assignment! I'm close to having it finished, but I'm having difficulty with one specific area and I need a little prodding from experienced programmers to get me in the right direction!

That being said, part of the program is converting an arithmetic expression to post fix notation. That part was pretty easy. Another part is using the post fix notation to evaluate the expression. Seems easy enough, but this is where I'm running into my problem. We're doing stacks and queues and what I intended to do is strcat the popped characters (either numbers or operators) onto a variable (postFix) and then use that variable to use a stack to evaluate the expression. Here's the code:

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 <cstring>
using namespace std;

#include "QueueLinkedList.cpp"
#include "StackLinkedListTemplate.cpp"

const int MAX = 50;

void determinePostFixNotation(char* i, char* p);

int main()
{
	char infix[MAX], postFix;
	strcpy_s(infix, "(((5+2)*(6/4))-(4*1))");

	cout << "The expression to be evaluated is:     " << infix;
	cout << endl;
	
	cout << "The expression in postfix notation is: ";
	determinePostFixNotation(infix, &postFix);
	cout << endl;
	
	return 0;
}

void determinePostFixNotation(char* infix, char* postFix)
{
	try{
		QueueLinkedList list;
		StackLinkedlistTemplate<char> operands;

		int size = strlen(infix);

		for(int x=0;x<size;x++)
			list.push(infix[x]);
		
		int x=0;
		do{

			if(infix[x]=='(')
				operands.push(list.pop());
			else if(infix[x]==')')
			{
				list.pop();
				
				char poppedCharacter; 

				poppedCharacter=operands.pop();
				//strcat(&poppedCharacter, "\0");
				//strcat_s(postFix, sizeof(postFix),&poppedCharacter);
				if(poppedCharacter!='+' && poppedCharacter!='-' && poppedCharacter!='/' && poppedCharacter!='*')
				{
					cout << "Error: Too few operands in expression.\n" << endl;
					exit(1);
				}				
				
				cout << poppedCharacter << " ";

				poppedCharacter=operands.pop();
				if(poppedCharacter!='(')
				{
					cout << "Error: Unbalanced Parenthesis!\n" << endl;
					exit(1);
				}
			}
			else if(infix[x]=='+' || infix[x]=='-' || infix[x]=='*' || infix[x]=='/')
				operands.push(list.pop());			
			else
				cout << list.pop() << " ";
			
			x++;

			}while(x<size);		
	}
	catch(QueueLinkedList::Empty) 
	{
		cout << "\n\nException: Stack Empty" << endl;
	}
	
	cout << endl;
}


I've commented out the strcats that cause the error. I was getting a conversion error (cannot convert from char to *char) then I realized that it was wanting a pointer. I tried to strcat a NULL terminator onto the poppedCharacter variable to fix the runtime problem, because I know the line of code causing the runtime error is

strcat_s(postFix, sizeof(postFix),&poppedCharacter);.

If you need the stack and queue classes, let me know and I'll include those.

Now, please don't be too critical about my code. I am, after all, a student, but any help from an experienced programmer would be most appreciated :D

Why are you using a function to add one character to the string?
1
2
postFix[strlen(postFix)]=poppedCharacter;
postFix[strlen(postFix)+1]=0;

Needless to say, postFix will need enough space to hold at least one more character.
Why are you using a function to add one character to the string?
1
2


postFix[strlen(postFix)]=poppedCharacter;
postFix[strlen(postFix)+1]=0;


Needless to say, postFix will need enough space to hold at least one more character.


You mean that as it loops through, the above code will just add whatever is stored on poppedCharacter onto the end of postFix???

If you go through the first time and store 'R' in poppedCharacter, then it will assign that to postFix with your above code. Then the next time through poppedCharacter is 'a', and after your code postFix is "Ra"?

Edit: grammar

Last edited on
Mmh... Wait a sec. I forgot something.
1
2
3
int l=strlen(postFix);
postFix[l]=poppedCharacter;
postFix[l+1]=0;

There we go.
Yep. That's exactly what will happen.
Thanks a lot for the help. I'm gunna mess around with it some in the morning. I'll let you know how it turns out.
Topic archived. No new replies allowed.