Help! Bad parser :/.

This is a discussion on Help! Bad parser :/. within the C++ Programming forums, part of the General Programming Boards category; [Edit] Read a few posts down. Ok - I've been working on an excel project for a few days. Now ...

  1. #1
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379

    Question Help! Bad parser :/.

    [Edit] Read a few posts down.

    Ok - I've been working on an excel project for a few days. Now I need to transpot theyse 10x10 excel grids to things that my parser will understand. However, my parser is incorrect, or more, I'm an idiot and built it poorly.

    My code is very straightforward, open a file, take the files contents, parse it, open another file, output the parsed data. My problem is, a 10x10 grid should have 100 characters. For some odd reason, I'm getting poorly parsed results.

    My code:
    Code:
    #include <iostream>
    #include <string>
    #include <windows>
    #include <fstream>
    using namespace std;
    
    string PARSER(string tbp);
    string Load();
    void OP(string a);
    
    int main() {
     string a;
    
     a=PARSER(Load());
     cout << a;
     OP(a);
    }
    
    string Load() {
     char gur[1001];
     string gar;
     string MAPPATH="C:\\Info.txt";
     int x;
    
     ifstream file_in(MAPPATH.c_str(), ios::nocreate);
    
     for(x=0; x < 1000; x++)
      file_in.get(gur[x]);
    
     gar+=gur;
    
     return gar;
    }
    
    string PARSER(string tbp) {
     int x;
     char m[1];
     string agr;
    
     for(x=0; x < tbp.length(); x++) {
      m[1] = tbp[x];
      if(m[1] == '\n')
       goto done;
      if(m[1] == ' ')
       goto done;
      if(m[1] == '\t')
       agr+='B';
      if(m[1] == 'R')
       agr+='R';
      done:
     }
    
     return agr;
    }
    
    void OP(string a) {
     string MAPPATH="C:\\Output.txt";
     int x;
    
     ofstream file_out(MAPPATH.c_str(), ios::nocreate);
    
     file_out << a;
    }
    As I said, very straightforward.

    And my parser data (three of them as an example, they're all the same length

    1:
    Code:
    R	R			R	R			R	R
    				R					
    				R					
    R	R			R					R
    R	R								R
    R	R								R
    R	R								R
    R	R			R					R
    R	R			R					R
    R	R			R	R	R	R	R	R
    2:
    Code:
    R	R			R	R			R	R
    R	R			R	R				R
    R	R			R	R				R
    R	R								R
    				R					
    									
    R							R	R	R
    R							R	R	R
    R							R	R	R
    R	R			R	R	R	R	R	R
    3:
    Code:
    R	R	R	R	R	R			R	R
    R								R	R
    R									R
    R		R							R
    R									R
    R									R
    R			R						R
    R									R
    R									R
    R	R	R	R	R	R			R	R
    And the output is respectively:
    Code:
    1: RBRBBBRBRBBBRBRBBBBRBBBBBBBBBRBBBBBRBRBBBRBBBBBRRBRBBBBBBBBRRBRBBBBBBBBRRBRBBBBBBBBRRBRBBBRBBBBBRRBRBBBRBBBBBRRBRBBBRBRBRBRBRBR
    
    2: RBRBBBRBRBBBRBRRBRBBBRBRBBBBRRBRBBBRBRBBBBRRBRBBBBBBBBRBBBBRBBBBBBBBBBBBBBRBBBBBBBRBRBRRBBBBBBBRBRBRRBBBBBBBRBRBRRBRBBBRBRBRBRBRBR
    
    3: RBRBRBRBRBRBBBRBRRBBBBBBBBRBRRBBBBBBBBBRRBBRBBBBBBBRRBBBBBBBBBRRBBBBBBBBBRRBBBRBBBBBBRRBBBBBBBBBRRBBBBBBBBBRRBRBRBRBRBRBBBRBR
    As is visible, the output is different in character length. They should all have exactly 100 characters. Oh, and reguarding the goto, its just so the computer doesnt need to run uneccesary if checks.

    Thanks for reading this, and thanks in advanced for help!
    Last edited by Blackroot; 03-06-2006 at 07:38 PM.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Oh, and reguarding the goto, its just so the computer doesnt need to run uneccesary if checks.
    Code:
    for(........)
    {
    	if(....)
    	{
    		//do something
    	}
    	else if(.....)
    	{
    		//do something
    	}
    	else if(....)
    	{
    		//do something
    	}
    
            //done:	
    }
    or:

    Code:
    for(.........)
    {
    	switch (m[1])
    	{
    	case '\n':
    		//do something
    		break;
    	case '\t':
    		//do something
    		break;
    	case ' ':
    		//do something
    		break;
    	default:
    		cout<<"something is wrong"<<endl;
    	}
           
           //done:
    }
    Last edited by 7stud; 03-06-2006 at 04:13 AM.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    alrite why u r not getting 100 characters?
    because take this line for example:
    R <tab> R <tab> R

    u want the the number of characters to be 3,
    but the output is 5,because there still are two tabs there,


    but here

    R<tab><space><tab>R

    u want the output to be 3 ,but u r going to get 4,

    it would be better if u ignore the tabs and consider the space,that way u should get 100 characters.
    Last edited by qqqqxxxx; 03-06-2006 at 05:42 AM.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Code:
    string Load()
    {
        char gur[1001];
    
        ...
    
        for(x=0; x < 1000; x++)
            file_in.get(gur[x]);
    
        gur[x] = 0;
    
        gar+=gur;
    Just to make sure you NULL terminate the character array before you assign it to the string.

    Code:
    string PARSER(string tbp)
    {
        int x;
        char m[1];
        string agr;
    
        for(x=0; x < tbp.length(); x++)
        {
            m[1] = tbp[x];
            if(m[1] == '\n')
                goto done;
            if(m[1] == ' ')
                goto done;
            if(m[1] == '\t')
                agr+='B';
            if(m[1] == 'R')
                agr+='R';
            done:
        }
    
        return agr;
    }
    The only valid index for an array of length one is 0, i.e. m[0], anything else is out of bounds.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> Oh, and reguarding the goto, its just so the computer doesnt need to run uneccesary if checks.

    FWIW, the keyword continue does the same thing as your goto, but is easier to read and understand. You should get out of the habit of trying to optimize each and every line of code you write to the detriment of its clarity.

  6. #6
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    Updated code:
    Code:
    #include <iostream>
    #include <string>
    #include <windows>
    #include <fstream>
    using namespace std;
    
    string PARSER(string tbp);
    string Load();
    void OP(string a);
    
    int main() {
     string a;
    
     a=PARSER(Load());
     cout << a;
     OP(a);
    }
    
    string Load() {
     char gur[1001];
     string gar;
     string MAPPATH="C:\\Info.txt";
     int x;
    
     ifstream file_in(MAPPATH.c_str(), ios::nocreate);
    
     for(x=0; x < 1000; x++)
      file_in.get(gur[x]);
    
     gar[x] = 0;
     gar+=gur;
    
     return gar;
    }
    
    string PARSER(string tbp) {
     int x;
     char m[1];
     string agr;
    
     for(x=0; x < tbp.length(); x++) {
      m[0] = tbp[x];
      if(m[0] == '\n')
       goto done;
      if(m[0] == ' ')
       goto done;
      if(m[0] == '\t')
       agr+='B';
      if(m[0] == 'R')
       agr+='R';
      if(m[0] == 'T')
       agr+='T';
      done:
     }
    
     return agr;
    }
    
    void OP(string a) {
     string MAPPATH="C:\\Output.txt";
     int x;
    
     ofstream file_out(MAPPATH.c_str(), ios::nocreate);
    
     file_out << a;
    }
    Output snippets (By looking for spaces)
    Code:
    RBRBRBRBRBRBBBRBRRBBBBBBBBRBRRBBBBBBBBBRRBBRBBBBBBBRRBBBBBBBBBRRBBBBBBBBBRRBBBRBBBBBBRRBBBBBBBBBRRBBBBBBBBBRRBRBRBRBRBRBBBRBR
    
    RBRBRBRBRBRBRBRBRBRRBBBBBBBBBRRBBBRBBBBBBRRBBBBBBBBBRBBBBBBBBBBBBBBBBBBBBBBBBBBBRRBRBBBBBBBBRRBBBBBBBBBRRBRBRBRBRBRBRBRBRBR
    <Spacing is a glitch from copying from the command prompt>

    Output snippets (By looking for tabs)
    Code:
    RBRBBBRBRBRBRBRBRBBBBBBRBBBBBBBBBRBBBRBBBBBBRBBBRRBRBBBRBRBRBBBRRBBBBBBBBBRRBBBBBBBBBRRBBBBRBRBBBBRRBBBBRBRBBBBRRBRBBBRBRBBBRBR
    
    RBRBRBRBRBRBBBRBRRBBBBBBBBRBRRBBBBBBBBBRRBBRBBBBBBBRRBBBBBBBBBRRBBBBBBBBBRRBBBRBBBBBBRRBBBBBBBBBRRBBBBBBBBBRRBRBRBRBRBRBBBRBR
    <Spacing is a glitch from copying from the command prompt>

    As is visible, its still not quite right. I fixed all thoes errors you listed as shown above in my new code, hk.

    I havent heard of continue, but I dont know how to manipulate it, so I'll stick with a goto, it may be uglier, but it saves half a milisecond of my life :P.

    So, does anyone else see a glitch here?
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    You didn't implement one of hk_mp5kpdw's fixes properly.

  8. #8
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    I probably shouldent have nammed them one letter apart -,-.

    Anyways, the updated code doesnt seem to change much.

    Code:
    #include <iostream>
    #include <string>
    #include <windows>
    #include <fstream>
    using namespace std;
    
    string PARSER(string tbp);
    string Load();
    void OP(string a);
    
    int main() {
     string a;
    
     a=PARSER(Load());
     cout << a;
     OP(a);
    }
    
    string Load() {
     char gur[1001];
     string gar;
     string MAPPATH="C:\\Info.txt";
     int x;
    
     ifstream file_in(MAPPATH.c_str(), ios::nocreate);
    
     for(x=0; x < 1000; x++)
      file_in.get(gur[x]);
    
     gur[x] = 0;
     gar+=gur;
    
     return gar;
    }
    
    string PARSER(string tbp) {
     int x;
     char m[1];
     string agr;
    
     for(x=0; x < tbp.length(); x++) {
      m[0] = tbp[x];
      if(m[0] == '\n')
       goto done;
      if(m[0] == ' ')
       goto done;
      if(m[0] == '\t')
       agr+='B';
      if(m[0] == 'R')
       agr+='R';
      if(m[0] == 'T')
       agr+='T';
      done:
     }
    
     return agr;
    }
    
    void OP(string a) {
     string MAPPATH="C:\\Output.txt";
     int x;
    
     ofstream file_out(MAPPATH.c_str(), ios::nocreate);
    
     file_out << a;
    }
    Output:
    Code:
    RBRBBBRBRBRBRBRBRRBBBBBBBBBRRBRBBBBRBBRBBRRBBBBBBBRBRBRBBBBBBBBBBBBBBBBBBRBBBBBBBBBRRBRBBBRBBBBRBRRBBBBBBBBBRRBRBBBRBRBBBRBR
    
    RBRBRBRBRBRBBBRBRRBBBBBBBBBRRBBBBBBBBRBRRBBBBBBBBRBRRBBBRBRBRBRBRBRBRRBBBRBBBBBBRRBBBRBBBBTBBRRBBBBBBBBBRRBBBBBBBRBBRRBRBRBRBRBRBRBRBRBR
    -,-.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    I cannot compile your code since it uses the outdated, non-standard headers. If you feel like taking the time to update it I might be able to look further.

    I did notice that you are calling get() 1000 times even though you say there are only 100 characters in your input file. You should break out of the for loop when get fails otherwise you will be adding the same character over and over to the end of your string.

    Actually, I count 137 characters in your first input, which not so surprisingly matches the 137 characters in the second output. Maybe it is "working" and you need to count again.
    Last edited by Daved; 03-06-2006 at 06:46 PM.

  10. #10
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    I dont know where you got 137 characters. The output is their.

    Let me see...

    Code:
    123 characters.
    RBRBRBRBRBRBRBRBRBRRBBBBBBBBBRRBBBRBBBBBBRRBBBBBBBBBRBBBBBBBBBBBBBBBBBBBBBBBBBBBRRBRBBBBBBBBRRBBBBBBBBBRRBRBRBRBRBRBRBRBRBR
    
    124 characters.
    RBRBBBRBRBRBRBRBRRBBBBBBBBBRRBRBBBBRBBRBBRRBBBBBBBRBRBRBBBBBBBBBBBBBBBBBBRBBBBBBBBBRRBRBBBRBBBBRBRRBBBBBBBBBRRBRBBBRBRBBBRBR
    -- Why are theyse being spaced out???

    The span goes from 120 - 135 characters depending on what block I check.

    I can give a good guess that when I'm copying from excel to the info file something is being punctured.

    And, I thought .h was outdated? Oh well, heres reupdated code, including a counter:
    Code:
    #include <iostream.h>
    #include <string.h>
    #include <windows.h>
    #include <fstream.h>
    using namespace std;
    
    string PARSER(string tbp);
    string Load();
    void OP(string a);
    
    int main() {
     string a;
    
     a=PARSER(Load());
     cout << a;
     OP(a);
    }
    
    string Load() {
     char gur[1001];
     string gar;
     string MAPPATH="C:\\Info.txt";
     int x;
    
     ifstream file_in(MAPPATH.c_str(), ios::nocreate);
    
     for(x=0; x < 1000; x++)
      file_in.get(gur[x]);
    
     gur[x] = 0;
     gar+=gur;
    
     return gar;
    }
    
    string PARSER(string tbp) {
     int srco;
     int x;
     char m[1];
     string agr;
    
     for(x=0; x < tbp.length(); x++) {
      m[0] = tbp[x];
      if(m[0] == '\n')
       goto done;
      if(m[0] == ' ')
       goto done;
      if(m[0] == '\t') {
       agr+='B';
       srco++;
      }
      if(m[0] == 'R') {
       agr+='R';
       srco++;
      }
      if(m[0] == 'T') {
       agr+='T';
       srco++;
      }
      done:
     }
    
     cout << srco << endl;
     return agr;
    }
    
    void OP(string a) {
     string MAPPATH="C:\\Output.txt";
     int x;
    
     ofstream file_out(MAPPATH.c_str(), ios::nocreate);
    
     file_out << a;
    }
    Has anyone tried copying to and from excel before? Did it work proporely?

    And yes, I call get 1000 times. I never got the solution to <file.EOF> it just left me confused. I just use 1000 for safety, to make sure all characters are accounted for.
    Last edited by Blackroot; 03-06-2006 at 07:02 PM.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Yes, the .h is outdated. It appeared as if you were using the .h and then removing it when posting to the forum, since I don't know of any <windows> header and you used nocreate which AFAIK is only available in fstream.h, not fstream.

    The second output in the post above my previous one had 136 characters (I was off by one in both counts).

    Please post a single example of input and its exact output. Chances are they will have the same number of characters. In fact, please post the exact number of characters you think the input has and the number of characters you think the output has.

  12. #12
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    Counting code:

    Code:
    int count(string y) {
     int x;
    
     for(x=0; x < y.length(); x++);
      cout << "";
    
     return x;
    }
    Input - Length 127:
    Code:
    R	R			R	R			R	R
    				R					
    				R					
    R	R			R					R
    R	R								R
    R	R								R
    R	R								R
    R	R			R					R
    R	R			R					R
    R	R			R	R	R	R	R	R
    Output - Length 127:
    Code:
    RBRBBBRBRBBBRBRBBBBRBBBBBBBBBRBBBBBRBRBBBRBBBBBRRBRBBBBBBBBRRBRBBBBBBBBRRBRBBBBBBBBRRBRBBBRBBBBBRRBRBBBRBBBBBRRBRBBBRBRBRBRBRBR
    Thank you for making me run that test. Something appears is wrong with the literal size of what I'm copying. In excel the block is 10x10. But it seems that a tab is added for every R aswell. So conclusively, I must remove one tab directly after every R found. Or at least, thats my logical solution. I'll try that, thanks again .
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  13. #13
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    Thats strange. This algorhythem didint change anything.

    Code:
    #include <iostream>
    #include <string>
    #include <windows>
    #include <fstream>
    using namespace std;
    
    string PARSER(string tbp);
    string Load();
    int count(string x);
    void OP(string a);
    
    int main() {
     string a;
    
     a=PARSER(Load());
     cout << count(a) << endl;
     cout << a;
     OP(a);
    }
    
    int count(string y) {
     int x;
    
     for(x=0; x < y.length(); x++);
      cout << "";
    
     return x;
    }
    
    string Load() {
     char gur[1001];
     string gar;
     string MAPPATH="C:\\Info.txt";
     int x;
    
     ifstream file_in(MAPPATH.c_str(), ios::nocreate);
    
     for(x=0; x < 1000; x++)
      file_in.get(gur[x]);
    
     gur[x] = 0;
     gar+=gur;
    
     return gar;
    }
    
    string PARSER(string tbp) {
     int srco;
     int x;
     char m[1];
     string agr;
    
     for(x=0; x < tbp.length(); x++) {
      m[0] = tbp[x];
      if(m[0] == '\n')
       goto done;
      if(m[0] == ' ')
       goto done;
      if(m[0] == '\t') {
       if(agr[x-1] != 'R' || agr[x-1] != 'T') {
        agr+='B';
        srco++;
       } else
        goto done;
      }
      if(m[0] == 'R') {
       agr+='R';
       srco++;
      }
      if(m[0] == 'T') {
       agr+='T';
       srco++;
      }
      done:
     }
    
     cout << srco << endl;
     return agr;
    }
    
    void OP(string a) {
     string MAPPATH="C:\\Output.txt";
     int x;
    
     ofstream file_out(MAPPATH.c_str(), ios::nocreate);
    
     file_out << a;
    }
    The new version of parser doesnt help... It should only add a character if R or T is not before itself... Why doesnt this algo work??
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> Why doesnt this algo work??
    The best way to find out is to step through it. If you are using Visual C++, it has a great debugger that makes it clear what is going on and not that difficult to use. Or for other compilers you might be able to use gdb which is more difficult. Even without a debugger, stepping through the code in your head while keeping track of the values of the variables on paper is a great way to figure out what is wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with a file parser.
    By Hulag in forum C++ Programming
    Replies: 7
    Last Post: 03-17-2005, 08:54 AM
  2. Parser - need help!
    By thelma in forum C Programming
    Replies: 2
    Last Post: 04-05-2004, 08:06 PM
  3. data loss bad bad bad
    By RoD in forum Tech Board
    Replies: 4
    Last Post: 05-01-2003, 12:06 PM
  4. Shocking(kind of)
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 25
    Last Post: 12-10-2002, 07:52 PM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21