need help in string compare

This is a discussion on need help in string compare within the C++ Programming forums, part of the General Programming Boards category; uhh hey guys i have some problem in string compare. here's a portion of my code. here's the what i ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    9

    need help in string compare

    uhh hey guys

    i have some problem in string compare. here's a portion of my code.

    here's the what i use to store the identifiers

    Code:
    string a[4];
    
       a[0]="if(i==1||i==x)";
       a[1]="else if(j==1||j==x)";
       a[2]="else if(j==i)";
       a[3]="while(x!=20)";
    and here's the execution code

    Code:
    if(a!=NULL)				
    //check if array is empty
    {
           for(int j=0; j<4; j++)                   
    //compare identifiers with tokens
                      {
                      if(a[j]== tokens[j])
    	{cout<<a[j];}
    
                          else
    						{cout<<tokens[j];}
                      }
    see the problem is that when i compile the program the system is hanged for whatever reason that i don't know. i asked a few friends and they say that the problem is with those codes i posted above. can somebody please tell me what's wrong with the codes??

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    well, first, assuming everythign else in your code is right, your logic is plain wrong:
    Code:
    for(int j=0; j<4; j++)
    //compare identifiers with tokens
    {
        if(a[j]== tokens[j])
        {
            cout<<a[j]; //equivalent to outputting tokens[j]
        }
        else
        {
            cout<<tokens[j];    //either way you get the same number.
        }
    }
    is the same as
    Code:
    for(int j=0; j<4; j++)
    {
        cout<<tokens[j];
    }
    second, you need to format your code better than that...
    http://www.codeguru.com/Cpp/misc/sam...icle.php/c4693
    Last edited by major_small; 03-18-2005 at 03:32 AM. Reason: comments, colors, and spacing. and a shameless plug for CG's Syntax Hlt
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >if(a!=NULL)
    >//check if array is empty
    This doesn't work for strings. Maybe you need a variable to keep track of the number of identifiers, and then use that in your for-loop:
    Code:
           for(int j=0; j<num_identifiers; j++)
          //compare identifiers with tokens

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    9
    can u explain more regarding "keep track of the number of identifiers"? i'm kinda lost.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >can u explain more regarding "keep track of the number of identifiers"? i'm kinda lost.
    Code:
    string a[4];
    
       a[0]="if(i==1||i==x)";
       a[1]="else if(j==1||j==x)";
       a[2]="else if(j==i)";
       a[3]="while(x!=20)";
    Here you have an array of 4 strings. Now if there will always be 4, then your for-loop should work:
    Code:
           for(int j=0; j<4; j++)                   
              //compare identifiers with tokens
              {
                  if(a[j]== tokens[j])
                     //More code
    But on the other hand, if there won't always be 4, then you need a variable to keep track of how many identifiers are stored in the array.

    Another option is to use a vector of strings. In this case you can use the size() function to determine how many strings are in the vector:
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    using namespace std;
    
    int main()
    {
       vector<string> a;
    
       a.push_back("if(i==1||i==x)");
       a.push_back("else if(j==1||j==x)");
       a.push_back("else if(j==i)");
       a.push_back("while(x!=20)");
    
       for (int i=0; i<a.size(); i++)
          cout << a[i] << endl;
    
    }

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    9
    thanks swoopy =)..i'll give it a try

  7. #7
    Registered User
    Join Date
    Feb 2005
    Posts
    9
    i figure out that i actually have major problem concerning my program. so i think it's best if i post the entire code so that someone can actually help me out. here's the entire code:

    Code:
    #include <iostream>
    #include <conio>
    #include <string>
    #include <vector>
    #include "tokenizer.h"
    
    using namespace std;
    
    void main()
    {
       
    	char menu;
       ifstream infile;
    
       do
       {
       	clrscr();
    
    		cout<<"\n\n\n";
    		cout<<"\n\t\t          * * * * * * * * * * * * * * * * * * * * * * * * * *  \n\n";
       	cout<<"\n\t\t\t                   TESCAGEN: BB vs WB\n";
    		cout<<"\n\n\t\t\t            Press S to Start the Program\n\n";
          cout<<"\n\n\t\t	    	    Press Q to Quit the Program\n";
       	cout<<"\n\t\t          * * * * * * * * * * * * * * * * * * * * * * * * * * \n";
    
          cout<<"\n\n\t\t\t\t Please enter your choice: ";
          cin>>menu;
    
          if(isalpha(menu))
          switch(menu)
          {
          	case 'S':
             case 's':
    
             infile.open("ass.txt");
    
             string line;
             vector <string> tokens;
             bool endOfFile = infile.eof();
             int count =0;
    
             vector <string> a;
       		a.push_back("if(i==1||i==x)");
       		a.push_back("else if(j==1||j==x)");
       		a.push_back("else if(j==i)");
       		a.push_back("while(x!=20)");
    
             while(!endOfFile)
             {
             	endOfFile = getStringAndTokens(infile, line, tokens);
    
                if(!endOfFile)
                {
                	count++;
                   cout<<count<<")"<<line<<endl<<endl;
    
                   if(!a.empty())
                   {
                   	for(int i=0; i<a.size(); i++)
                   	{
                         if(a==tokens)
                         {
                            a.push_back("if(i==1||i==x)");			       //compare tokens with identifiers
                          }
                   	}
                   }
    
    
    
                   	for(int i=0;i<tokens.size();i++)										  //tokenizing words
                   	{
                   		cout<<tokens[i]<<endl;
                   	}
    
                   		cout<<"\n-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n"<<endl;
    
                   		ifstream(infile);
                   		system("Pause");
    
                }																						  //end if !eof
             }																							  //end while !eof
    
             getch();
             break;
    
          }																								  //end switch
          else
            	cout<<"\t\t Wrong key in. Please try again.\n\n";
      }while(menu!='q'&& menu!='Q');																  //end do
    }

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Can you explain what your program is trying to do? It's impossible to tell from your code.

    I don't think it's really supposed to compare lines of code, but assuming this is true:
    Code:
    >               if(!a.empty())
    >               {
    >               	for(int i=0; i<a.size(); i++)
    >               	{
    >                     if(a==tokens)
    >                     {
    >                        a.push_back("if(i==1||i==x)");			       //compare tokens with identifiers
    >                      }
    >               	}
    >               }
    Here you would need a double nested for-loop:
    Code:
                   if(!a.empty())
                   {
                   	for(int i=0; i<tokens.size(); i++)
                   	{
                       for (int j=0; j<a.size(); j++)
                       {
                          //compare tokens with identifiers
    
                          if (tokens[i] != a[j])
                          {
                             a.push_back(tokens[i]);		
                          }
                       }
                   	}
                   }
    But like I said, there's no way to tell without knowing what the program should do.

  9. #9
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    Use the string class and strcmp honey.

    if(strcmp(myString,"Hello Krakkles.")==0) //strcmp() returns 0 if they're the same.

  10. #10
    Registered User
    Join Date
    Feb 2005
    Posts
    9
    my program is supposed to act as a test case generator. first of all, it will read a file and tokenize each words. then if it comes accross an identifier it will generate a particular test case for that identifier. i hope my explanation is good enough for you to understand.

  11. #11
    *this
    Join Date
    Mar 2005
    Posts
    498
    just looking at ur code would be much easier with the use of functions, especially to grab numbers from a file.

  12. #12
    Registered User
    Join Date
    Feb 2005
    Posts
    9
    i have solved the problems with comparing string thanks to swoopy . anyway, now i'm having problems to get it to display those identifiers when it is encountered. here's what i've got so far:

    Code:
    #include <iostream>
    #include <windows.h>
    #include <conio>
    #include <string>
    #include <vector>
    #include "tokenizer.h"
    
    using namespace std;
    
    void end();
    void mainmenu();
    
    void mainmenu()
    {
       HANDLE hOut;
       hOut = GetStdHandle(STD_OUTPUT_HANDLE);
       SetConsoleTitle("TESCAGEN version 1.0");
    
    	cout<<"\n-------------------------------------------------------------------------------";
       cout<<"\n\t                  *** TESCAGEN: BB vs WB *** ";
       cout<<"\n-------------------------------------------------------------------------------\n";
    
       cout<<"\n\n[S] - Start Program\n";
       cout<<"[Q] - Quit Program\n";
    }
    
    void main()
    {
    	char menu;
       ifstream infile;
    
       do
       {
       	clrscr();
       	//SetConsoleTextAttribute(hOut, BACKGROUND_BLUE | BACKGROUND_INTENSITY);
          //SetConsoleTextAttribute(hOut, BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY);
    
          mainmenu();
    
          cout<<"\n\nSelect an option: ";
          cin>>menu;
    
          if(isalpha(menu))
          switch(menu)
          {
          	case 'S':
             case 's':
    
             infile.open("ass.txt");
    
             string line;
             vector <string> tokens;
             bool endOfFile = infile.eof();
             int count =0;
    
             /*string i[4];
    
             i[0]="if(i==1||i==x)";
       		i[1]="else if(j==1||j==x)";
       		i[2]="else if(j==i)";
       		i[3]="while(x!=20)";*/
    
             vector <string> a;
    
             a.push_back("if(x<5||x>20)");
       		a.push_back("if(i==1||i==x)");
       		a.push_back("else if(j==1||j==x)");
       		a.push_back("else if(j==i)");
       		a.push_back("while(x!=20)");
    
             while(!endOfFile)
             {
             	endOfFile = getStringAndTokens(infile, line, tokens);
    
                if(!endOfFile)
                {
                	count++;
                   cout<<count<<")"<<line<<endl<<endl;
    
                   /*compare identifiers and tokens*/
                   if(!a.empty())
                   {
                   	for(int i=0; i<tokens.size(); i++)
                   	{
                      	for(int j=0; j<a.size(); j++)
                         {
                         	if(tokens[i] != a[j])
                            {
                            	a.push_back(tokens[i]);
                            }
    
                            else if(a[j] != tokens[i])
                            {
                            	a.push_back(a[j]);
                            }
                         }
                      }
                   }                                 		
    
                   	for(int i=0;i<tokens.size();i++)										  //tokenizing words
                   	{
                   		cout<<tokens[i]<<endl;
                   	}
    
                   		cout<<"\n-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n"<<endl;
    
                   		ifstream(infile);
                   		system("Pause");
                }						
    }										
             getch();
             break;
    
          }												
          else
            	cout<<"\t\t Wrong key in. Please try again.\n\n";
      }while(menu!='q'&& menu!='Q');
      	end();                                             							
    }		  
    
    void end()
    {
      clrscr();
      cout<<"\n\n\n\n\n\n\n\n\t\t\t Thank you for using this program!";
      cout<<"\n\n\n\t\t\t Coded by: Aizatul Afzan bt. Ibrahim\n";
      cout<<"\t\t\t Email: zaxry_rugrats83@yahoo.com";
      getch();
      exit(0);
    }
    can someone help me figures oyt what is wrong with the codes? i suspect that this portion of code is wrong:

    Code:
    else if(a[j] != tokens[i])
    {
           a.push_back(a[j]);
    }
    but like i said, i don't really know. please help me i'm extremely desperate right now.

  13. #13
    Registered User
    Join Date
    Feb 2005
    Posts
    9
    hi guys
    i'm sorry about the thread i've posted yesterday. that was obviously the wrong one. this is the correct one.

    Code:
    #include <iostream>
    #include <windows.h>
    #include <conio>
    #include <string>
    #include <vector>
    #include "tokenizer.h"
    
    using namespace std;
    
    void end();
    void mainmenu();
    
    void mainmenu()
    {
       HANDLE hOut;
       hOut = GetStdHandle(STD_OUTPUT_HANDLE);
       SetConsoleTitle("TESCAGEN version 1.0");
    
    	cout<<"\n-------------------------------------------------------------------------------";
       cout<<"\n\t                  *** TESCAGEN: BB vs WB *** ";
       cout<<"\n-------------------------------------------------------------------------------\n";
    
       cout<<"\n\n[S] - Start Program\n";
       cout<<"[Q] - Quit Program\n";
    }
    
    void main()
    {
    	char menu;
       ifstream infile;
    
       do
       {
       	clrscr();
       	//SetConsoleTextAttribute(hOut, BACKGROUND_BLUE | BACKGROUND_INTENSITY);
          //SetConsoleTextAttribute(hOut, BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY);
    
          mainmenu();
    
          cout<<"\n\nSelect an option: ";
          cin>>menu;
    
          if(isalpha(menu))
          switch(menu)
          {
          	case 'S':
             case 's':
    
             infile.open("ass.txt");
    
             string line;
             vector <string> tokens;
             bool endOfFile = infile.eof();
             int count =0;
    
             vector <string> ident;
    
             ident.push_back("if(x<5||x>20)");
       		ident.push_back("if(i==1||i==x)");
       		ident.push_back("else if(j==1||j==x)");
       		ident.push_back("else if(j==i)");
       		ident.push_back("while(x!=20)");
    
             vector <string> test;
    
             test.push_back("Possible test cases: x>5, x==5, x!=5, x<20, x==20, x!=20");
             test.push_back("Possible test cases: i<1, i>1, i!=1, i<x, i>x, i!=x");
             test.push_back("Possible test cases: j<1, j>1, j!=1, j<x, j>x, j!=x");
             test.push_back("Possible test cases: j<i, j>i, j!=i");
             test.push_back("Possible test cases: x<20, x>20, x==20");
    
             while(!endOfFile)
             {
             	endOfFile = getStringAndTokens(infile, line, tokens);
    
                if(!endOfFile)
                {
                	count++;
                   cout<<count<<")"<<line<<endl<<endl;
    
                   if(!ident.empty())				
    {
                   	for(int i=0; i<tokens.size(); i++)
                   	{
                      	for(int d=0; d<ident.size(); d++)
                         {
    								for(int c=0; c<test.size(); c++)
    								{
                         		if(tokens[i] != ident[d])
    									{
                            		ident.push_back(tokens[i]);
    									}
    									else if(ident[d]==test[c])
    									{
                            		ident.push_back(test[c]);
    									}
    								}
                         }
                      }
                   }
    			 			 
                   	for(int i=0;i<tokens.size();i++)			{
                   		cout<<tokens[i]<<endl;
                   	}
    
                   		cout<<"\n-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n"<<endl;
    
                   		ifstream(infile);
                   		system("Pause");
                }					
    }												
             getch();
             break;
    
          }																								       else
            	cout<<"\t\t Wrong key in. Please try again.\n\n";
      }while(menu!='q'&& menu!='Q');
      	end();                                             								  
    }										
    void end()
    {
      clrscr();
      cout<<"\n\n\n\n\n\n\n\n\t\t\t Thank you for using this program!";
      cout<<"\n\n\n\t\t\t Coded by: Aizatul Afzan bt. Ibrahim\n";
      cout<<"\t\t\t Email: zaxry_rugrats83@yahoo.com";
      getch();
      exit(0);
    }
    i'm trying to output the test cases when it a particular identifiers encountered. is this the correct way?

    Code:
    if(!ident.empty())			
    {
                   	for(int i=0; i<tokens.size(); i++)
                   	{
                      	for(int d=0; d<ident.size(); d++)
                         {
    								for(int c=0; c<test.size(); c++)
    								{
                         		if(tokens[i] != ident[d])
    									{
                            		ident.push_back(tokens[i]);
    									}
    									else if(ident[d]==test[c])
    									{
                            		ident.push_back(test[c]);
    									}
    								}
                         }
                      }
                   }

  14. #14
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309
    void main()
    Unless I'm missing something, that should be int main, if for no other reason than standerdization.
    To code is divine

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  2. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  3. problems with overloaded '+' again
    By Brain Cell in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2005, 05:13 PM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  5. "Operator must be a member function..." (Error)
    By Magos in forum C++ Programming
    Replies: 16
    Last Post: 10-28-2002, 01:54 PM

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