|
-
June 19th, 2008, 03:05 PM
#1
[RESOLVED] Help with Pointers
I get a "Debug Assertion Failed" when I go to run my program. More specifically it says "Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)". I assume this is because I'm using pointers incorrectly.
Here is some information about the assignment:
A file contains text representing a prioritized collection of messages. The messages were received one character at a time. At any time during the receipt of a message, a message might have been interrupted by the beginning of a new message of higher priority. (The interrupting message might have been further interrupted by the beginning of a still higher priority message, and so on.) The beginning of a message is always indicated by the '[' character; the end of a message is always indicated by the ']' character. End-lines might occur at any point in the file, but are not part of any message. Your program must read this file (whose name is input from the keyboard). As each message is completed, the program must display it. If any incomplete messages remain when the file has been completely processed, the program must display each with the label "Incomplete: ".
Error processing:
When a character other than end-of-line or '[' is encountered, but no message is being processed, the program must display an error message containing the offending character and identifying the problem. The program should then continue its processing.
Example:
Suppose the file contains the following text:
[abc[PQ[S[123]]T]
defgh]$[AB[uvwxy[1[45]2]CDEF]
The following output should result:
123
S
PQT
abcdefgh
The character '$' does not appear to be part of any message.
45
12
uvwxyCDEF
Incomplete: AB
Here is the required part I am having problems with:
In the main function, use a string-pointer (* String) to represent the current message. Ensure that this pointer is set to NULL before beginning to process the file. (This indicates that there is no current message being processed.) Immediately after completing each message, deallocate the memory referenced by the pointer and then set the pointer back to NULL to show that there is no current message being processed.
and here is my code:
Code:
#include <iostream>
#include <fstream>
#include <string>
#include "Stack.h"
using namespace std;
void main ( )
{
const string MessageFileName = "Dummy.txt";
ifstream MessageFile;
char Symbol;
char SymbolTemp;
string * CurrentMessage;
Stack<char> MyStack;
//Set CurrentMessage to NULL
CurrentMessage = NULL;
//Open input file
MessageFile.open(MessageFileName.c_str());
//Loop through message file
while (MessageFile.get(Symbol))
{
if (Symbol == '[')
{
if (CurrentMessage != NULL)
{
MyStack.Push(Symbol);
delete CurrentMessage;
} //end if
CurrentMessage = new string;
}
else if (Symbol == ']')
{
if (CurrentMessage != NULL)
{
cout << CurrentMessage;
if (MyStack.IsEmpty())
{
CurrentMessage = NULL;
}
else
{
MyStack.Pop(SymbolTemp);
CurrentMessage = CurrentMessage + SymbolTemp;
} //end if
}
else
{
//report an error
} //end if
}
else if (Symbol == '\n')
{ } //do nothing
else
{
if (CurrentMessage != NULL)
{
CurrentMessage = CurrentMessage + Symbol;
}
else
{
//report an error
} //end if
} //end if
} //end while
if (CurrentMessage != NULL)
{
cout << CurrentMessage;
delete CurrentMessage;
CurrentMessage = NULL;
} //end if
while (!MyStack.IsEmpty())
{
CurrentMessage = new string;
MyStack.Pop(SymbolTemp);
CurrentMessage = CurrentMessage + SymbolTemp;
cout << CurrentMessage;
} //end while
}
If anyone could help me out with this I would appreciate it. I assume my problem is I'm using the pointer CurrentMessage incorrectly throughout my main program.
-
June 19th, 2008, 03:07 PM
#2
Re: Help with Pointers
Code:
CurrentMessage = CurrentMessage + SymbolTemp;
In that line of code, CurrentMessage is a pointer, so you are doing pointer addition. That's probably not what you wanted.
-
June 19th, 2008, 03:11 PM
#3
Re: Help with Pointers
What is the correct way to do it?
-
June 19th, 2008, 03:16 PM
#4
Re: Help with Pointers
 Originally Posted by dreamuser
What is the correct way to do it?
Dereference the pointer each time you want to use it as a string. In fact, I notice this needs to be done in other places as well.
If you're working on the pointer (e.g. setting it to null, deleting it or assigning a new value), then you use CurrentMessage. If you are modifying or accessing the string it points to, you dereference it (with *CurrentMessage).
-
June 19th, 2008, 03:22 PM
#5
Re: Help with Pointers
thanks that was exactly my problem
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|