Thread: unable to call function ????!!!

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    40

    unable to call function ????!!!

    continue to my previuos

    I can't rely solve this problem , The function couldn't be called because the string that got form file is "SYMBOLS]" instead of "[SYMBOLS]" I don't no why , please help me.

    the program get words from file "bin.txt" the file has tags , and all the words under each tag is transfared to an array.

    I'm using the function istream.getline to get the lines from the file bin.txt

    the function sacn_bin() gets all of the line inside that file and compares it with a specifed strings , according to that decides which finction to call , here is the point .

    please note, if you changed the strcmp statement to compare with "SYMBOLS]" the function is called !!!!!

    please if the program idea still unclear tell me.

    thanks to you all.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I can't open that attachment for some reason, but if your program has changed little since the last thread, I think it would help to set col to zero in scan_bin()'s while loop.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    What compiler are you using?

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    40

    Unhappy

    I hope you can now , I think the problem in getting the line , when I'm using
    bin_file.getline(temp,30);
    the finction elemenate the '['

    I''m using Visual C++ 6.0
    sorry for this .. just copy and past
    Code:
    
      #include < iostream.h>
      #include < fstream.h >
      #include < string.h  >
     
    
    //using namespace std;
    
    
     ifstream input("c:\\compiler_test\\Data_file.txt"); // the code text file
     ifstream bin_file("c:\\compiler_test\\bin.txt"); // compiler information file
     
     
    //this for RESERVED_WORDS
    const int reserved_size=40;
    typedef char string[20] ; // array holds the reserved word
    string reserved[reserved_size]; // holds reserved_size reserved words
    int reserved_ID[reserved_size]; // holds the corsponding ID for each reserved word
    
    
    //this for SYMBOLS
    const int symbols_size=40;
    typedef char symbol[3]; //max symbol length is 2 characters
    symbol symbols_list[symbols_size];
    int symbols_ID[symbols_size];
    
    
    //the following ID list set by the functio set_ID()
    int Names_ID; // the ID of the names
    int Reserved_ID; // the starting ID for reserved_ID list
    int Symbols_ID; // the starting ID for the symbol_ID list
    
    
    // separators list
    // 10 the end line , 32 the space char
    // you can add new separator symbol here
    char sep_list[] = { 10,32, '.', ',', ';', ',', '+', '-', '='};
    const int is_Sep(char) ;  // returns 1 if in the sep_list ,0 if not
    
    
    
    
    // scanning functions
    void get_Reserved();
    void get_Symbols();
    void get_Tokens();
    void scan_bin();
    void set_ID();
    
    
    
    // 
    //
    //
    int main(int argc, char* argv[])
    {
        if(argc > 1)
    	{cout << "You need to pass in a file name to the program via the command line\n";
    	  cin.get();
    	  return 1;}
        else{
           
    
       if ( !bin_file.is_open() ) 
    	     cout << "the file couldn't be opened !" ;
    	else cout << "the file is opend successfully !" ;
    
     
    scan_bin();  // get all the information needed
    
    
      
    // printing the RESERVED_WORDS list
    cout << endl;
    for(int i =0 ; i < reserved_size ; i++)
      cout << symbols_list[i] << " - " <<  symbols_ID[i]<< endl;
    
    	cout << "\n \n press any key to exit";
    	cin.get();
    
    	return 0; // main end
    	} 
    }
    //----------------------------------------------------------
    //__________________________________________________________
    
    void scan_bin(){ 
    // this function scans the bin file to get all the information inside
     int row=0;
     int col=0;
     char ch;
     char temp[20];
      
     /*while(!bin_file.eof())
      { bin_file.get(ch);
        col=0;
    	while ( !is_Sep(ch) & !input.eof() & ch!=-1)
    	{   temp[col++]=ch;		  
    		 bin_file.get(ch); }
    	     temp[col] = '\0';
    
    		if(!strcmp(temp , "[RESERVED_WORDS]"))
    		{ get_Reserved();  // calling the reserved words scanning function
    		continue;}
    		  
    		if(!strcmp(temp , "ID]"))
    		   set_ID();
      }*/
    
     while(!bin_file.eof())
      { bin_file.getline(temp ,40);
        	
    		if(!strcmp(temp , "[RESERVED_WORDS]"))
    		 get_Reserved();  // calling the reserved words scanning function
    
    		if(!strcmp(temp , "[SYMBOLS]"))
    		 get_Symbols();  // calling the reserved symbols scanning function
    				  
    		if(!strcmp(temp , "ID]"))
    		   set_ID();     // this function sets all the data related to the IDs
      }
    
    
    }
    void get_Symbols()
    {//  the symbols scanned here should be under SYMBOLS tag in bin_file
     // and saved the corsponding ID for each word in reserved_id array 
     // if new tag bracket faced , the function will exit	
     int row=0;
     int col=0;
     char ch;    // temporary char for scanned character
     char temp[3]; // temporary array for scanned symbol
      
      while(!bin_file.eof())
      { bin_file.get(ch);
    		while ( ch!=10 & !input.eof() & ch!=-1)
    				{  if( ch=='[' | ch==']')
    						return;
    					temp[col++]=ch;		  
    					bin_file.get(ch); }
    
    		temp[col] = '\0';
    		strcpy(symbols_list[row] , temp);  // copying the word into the list
            col=0;
    		row++;
    	
      }
    
    }
    //----------------------------------
    
    void get_Reserved()
    {//  the tokens scanned here should be under RESERVED_WORDS tag in bin_file
     // and saved the corsponding ID for each word in reserved_id array 
     // if new tag bracket faced , the function will exit	
     int row=0;
     int col=0;
     char ch;    // temporary for scanned character
     char temp[20]; // temporary for scanned word
      
      while(!bin_file.eof())
      { bin_file.get(ch);
    		while ( ch!=10 & !input.eof() & ch!=-1)
    				{  if( ch=='[' | ch==']')
    						return;
    					temp[col++]=ch;		  
    					bin_file.get(ch); }
    
    		temp[col] = '\0';
    		strcpy(reserved[row] , temp);  // copying the word into the list
            col=0;
    		row++;
    	}
    
    }
    //----------------------------------
    
    void get_Tokens()
    {int row=0;
     int col=0;
     char ch;
     char temp[20];
      
      while(!bin_file.eof())
      { bin_file.get(ch);
    
      
    	 while (ch!=-1 & ch!=' ' & ch!='+' & ch!='.' & ch!=10 & !input.eof())
          {   temp[col++]=ch;		  
              bin_file.get(ch);
    	  }
    
    	  temp[col] = '\0';  
    	  strcpy(reserved[row] , temp); // copy the word to the list
    	  col=0;
    	  row++;
    	    
      
      }
    
    }
    
    
    //--------------
    const int is_Sep(char c) 
    {  //This function checks the symbol c if in the separator 
       // using sizeof for flexible add in the array
    	for(int i=0 ; i<sizeof(sep_list) ; i++)
    		if(c==sep_list[i])
    			return 1; // in the list
     return 0; // not in the list
    }
    
    
    //-----------------
    
    void set_ID()
    {//here we get the ID for each tokens type that under [ID] tag
     // if you want to add a tag , you should add it in the ID list
        int col=0 ;
    	int row= 0;
    	int index;
        char temp[20];
    	int ID_value=0;
        char ch;
    	char line[51];
    	 while(!bin_file.eof())
    	{ bin_file.get(ch);
    		while (  !input.eof() & ch!=-1 &ch!=10)
    				{  if( ch=='[' | ch==']')
    						break;
    					line[col++]=ch;		  
    					bin_file.get(ch); }
    
    		line[col] = '\0';
    		for(index = 0 ;line[index]!='=' ; index++)
    		  temp[index] = line[index];
    		temp[index++]='\0';
    		ID_value=0;
    		for(; line[index]!='\0' ; index++)
    		{ID_value*=10;
    		ID_value+=line[index] - 48;}
    
    		col=0;
    		row++;
    
    		// assigning values to ID list
    		if(!strcmp(temp,"RESERVED_WORDS"))
    			Reserved_ID = ID_value;
    		else if(!strcmp(temp,"SYMBOLS"))
    			Symbols_ID=ID_value;
    		else if(!strcmp(temp,"NAMES"))
    			Names_ID=ID_value;
    
      }
    
    // setting the corrospnding reserved_ID values
    for(index =0 ; index <reserved_size ; index++)
    {reserved_ID[index] = Reserved_ID + index; 
     symbols_ID[index] =  Symbols_ID + index;
    }
    
    }

    thanks for interest

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    40
    >reserved[row] is a string. This should be:

    because its a string it should be copied by strcpy
    is there a way to over this bug !
    thanks

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You should decide whether you want to use C++ strings or C style strings (character arrays). In most C++ programs the C++ string class is better, but it is up to you. I would think it would be better to be consistent than to use some of each.

    I'm guessing your problem is in get_Reserved(). Notice that it will be called just before you read in "[SYMBOLS]". Also notice that in that function you return when the character is '[' or ']'. So what is probably happening is that you are reading up until and including the '[' in "[SYMBOLS]". That means that it isn't there the next time you call getline, it has been read and discarded by cin.

    You could use peek() instead of get() to check the next character in the stream without getting it, or you could use extra logic to remember that you read in an opening bracket.

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    40
    oh ya, thank you , its right .but I thought that getline gets the entire line regarding of the char.

    about the solution. I can insert end flag in the stream itself but its not efficient . please can you guide me in using peek() according to my program?

    thank you alot Daved

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    In that particular place in your code, before calling bin_file.get(ch);, you would use bin_file.peek() to look at the next character in the stream without actually getting it. If the character was '[', then you can return as you do now. If not, you can call bin_file.get(ch); as you do now.

    How you want to structure it within your while loop is up to you, your code is a little hard to read and I don't have time to figure it out and think it through myself. Just know that you should be calling peek and checking its value for '[' before calling get. I'm guessing you might need to use this technique in other places in the code as well, but see if you can get this part working first.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    1. Your indentation is pretty erratic, which makes it harder than it need be to follow what's going on.

    2. while(!bin_file.eof()
    See the FAQ for why this is bad.

    3. if( ch=='[' | ch==']'
    You REALLY want to use || here (and likewise && where you've used &)
    There is a big difference between the logical || and the bitwise | operators.

    4.
    char temp[20];
    ..
    bin_file.getline(temp ,40)
    It really does no good at all to lie about the size of buffers.

    5. char temp[3];
    Did you count the \0?
    This only stores two characters.
    All your loops which build a string one char at a time should check the length as they go.

    6. ch!=10 & !input.eof() & ch!=-1
    a) where did you get -1 from ?
    b) for 10, use the char constant '\n'
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Nov 2006
    Posts
    40
    Dear Salem , Thank you for this valuable notes.

    1. The indentation is 'damged' while copying the code. see the file
    2.I don't know why this is bad. I got this from an example of this forum
    3.please if you can tell me that difference
    4.you are right. this may from changing in code errors
    5.yes. I may face , >= , != ....... and I should count them in
    6.a. I got -1 from debugging the code . from the debugger
    b. its the same !!

    thank you again . please can you tell me how can I change the file path of the input object. I mean , If the 'char path[30]' holds the path , how can I assign this path to the object and reopen it again.
    Last edited by GSalah; 11-08-2006 at 02:31 PM.

  11. #11
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    The indentation used in the file doesn't look very good either.
    In some places you use a single space for indenting and in some places you use a tab.
    And this syntax isn't very nice:
    Code:
    int function(int a)
     {code}
    Use this:
    Code:
    int function(int a){
        code
    }
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > 2.I don't know why this is bad. I got this from an example of this forum
    I got mine from here - http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    Where did you copy yours from?

    > 3.please if you can tell me that difference
    I thought I did, maybe try your book as well?

    > 6.a. I got -1 from debugging the code . from the debugger
    So that's fine for your current compiler, what happens when you change to something else?

    > b. its the same !!
    That wasn't the point - which do you find easier to read?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Registered User
    Join Date
    Nov 2006
    Posts
    40
    thank you Salem. you didn't answer me, how can I change the file path ?

    thank you all , I need this notes.
    Last edited by GSalah; 11-08-2006 at 02:52 PM.

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You can call input.open(path).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  15. #15
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    That code, when you finish it, needs two things

    1: Tidying up, indentation

    2: Whitespaceing needs addressing. Too many sections of code seem to squashed up.
    Double Helix STL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Help calling function is asm
    By brietje698 in forum C++ Programming
    Replies: 24
    Last Post: 12-06-2007, 04:48 PM
  3. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM