By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,385 Members | 1,739 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,385 IT Pros & Developers. It's quick & easy.

What is wrong with my code? (strings)

P: 79
Hello,

I have written a fairly complex parsing tool that is used to parse information from company documents. The program works very well, but in order to insure that all the data is copied properly, I had to remove some of the things that are not part of the data, but are simply headings. Here is what I mean:

Expand|Select|Wrap|Line Numbers
  1. *Heading
  2. <CODE1> = <COMMAND1>
  3. <CODE2> = <COMMAND2>
The program reads in each line, and separates <CODE1> from <COMMAND1> at the equals sign. the program then stores the <COMMAND1> in a CMapStringToString object with <CODE1> as the key. I have already mentioned that in other discussions.

Here is the problem: when retrieving <COMMAND1> directly below the heading, the code isn't simply <CODE1>, but all the stuff in the heading is part of it. What I mean is, if I want to get whatever is stored as <COMMAND2>, I would type:

Expand|Select|Wrap|Line Numbers
  1. CMapStringToStringObject["<CODE2>"]
But if I want to get <COMMAND1>, I would have to type:

Expand|Select|Wrap|Line Numbers
  1. CMapStringToStringObject["*Heading\n<CODE1>"]
So in order to solve this, I designed the program to delete the first digits of the <CODE> string if they are not equal to "<". This works, but when I output everything to the console, the headings disappear, as they have been deleted.

So here is what I put before the printing is done:

Expand|Select|Wrap|Line Numbers
  1.         int i = 0;
  2.  
  3.         BOOL newLine = false, text = false;
  4.  
  5.         std_string code , cmd, dummy;
  6.  
  7.         while (std::getline(input, code, input.widen('='))) {    //reads in characters until the '='
  8.  
  9.             std::getline(input,cmd);                            //reads in remaining characters in the line
  10.  
  11.             code.erase(code.begin() + (code.size()-1));            //erase last character, a whitespace
  12.             cmd.erase(cmd.begin());                                //erase first character, a whitespace
  13.  
  14.             while (code[0] != '<') {                            //checks if the first character of 'code' is not <
  15.  
  16.                 if (code[0] == '\n') {
  17.                     newLine = true;
  18.                 } else {
  19.                     text = true;
  20.                 }            
  21.  
  22.                 dummy[i] = code[0];
  23.                 code.erase(code.begin());
  24.                 i++;
  25.             }
  26.  
  27.             if (newLine) {                //checks if there has been a newline character preceding a 'code' in this iteration
  28.                 wcout << endl;            //if there is, a newline character is printed onto console and ofstream file
  29.                 output << endl;
  30.             }
  31.  
  32.             if (text) {                            //checks if there has been text preceding a 'code' in this iteration
  33.                 wcout << dummy << endl;            //if there is, the text is printed onto console and ofstream file
  34.                 output << dummy << endl;
  35.             }
  36.  
  37.             newLine = false;    //resets the value for newLine to false
  38.             text = false;
  39.             i = 0;
  40.  
  41.         }
Like I said, all other parts of the code work, except for this:

Expand|Select|Wrap|Line Numbers
  1.             while (code[0] != '<') {                            //checks if the first character of 'code' is not <
  2.  
  3.                 if (code[0] == '\n') {
  4.                     newLine = true;
  5.                 } else {
  6.                     text = true;
  7.                 }            
  8.  
  9.                 dummy[i] = code[0];
  10.                 code.erase(code.begin());
  11.                 i++;
  12.             }
According to the debugger, it crashes at the second iteration, when it reaches dummy[i] = code[0]
May 24 '07 #1
Share this Question
Share on Google+
1 Reply


100+
P: 110
std_string code , cmd, dummy;

u have not declared dummy as an array and u r using it as an array..
it is crashing because of that reason i suppose..
Xoinki
May 25 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.