Parsing and Matching....

This is a discussion on Parsing and Matching.... within the C++ Programming forums, part of the General Programming Boards category; I some problem to solve this is our 2nd mid-term quiz in my c++ class. this is my current code: ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    12

    Parsing and Matching....

    I some problem to solve this is our 2nd mid-term quiz in my c++ class.

    this is my current code:

    Code:
    #include<iostream>
    #include<string.h>
    #include<conio.h>
    #include<ctype.h>
    
    using namespace std;
    
    main()
    {
        char string[30];
        int x,counter,len;
        cout<<"enter string: ";
        gets(string);
        len=strlen(string);
       for (x=0; x<=len-1; x++)
        {    
            if ('string'=='(')
            {
                counter=0;
                cout<<"\ncounter: "<<counter;
                cout<<"\nValid match";
             }
             else if ('string'==')')
             {
                 counter=1;
                 cout<<"\ncounter: "<<counter;
                 cout<<"\nInvalid match";
             }
         }         
             
         
        getch();
    }
    my instructor said that the program will only search for the '(' and ')'. if the entered string is only '(' or ')' then it is invalid match. now, it the entered string is '()' then it is a valid match. more examples is.. if the user inputs '(())' the program will count how many '(''s and how many ')'s.. then after that is subtract the number of '(' to the number of ')'... how will i gonna do these conditions??? please...

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    >> if ('string'=='(')
    The use of 'string' there is wrong. Do you know how to get a character from the string? You are looping as if you want to loop through each character, so start by changing the 'string' to get the character at index x.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    > gets(string);
    eww eww eww eww - unclean!
    Read the FAQ - gets() is the world's most dangerous function.

    Besides, in C++, you should be using a std::string (not a char array) and a getline() method for reading input in a nice safe manner.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    ima n00b, ok? orion-'s Avatar
    Join Date
    Aug 2005
    Location
    alberta, canada
    Posts
    55
    why should you use a std::string insted of char array in c++?

  5. #5
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    Safe, optimal and handy. Not to mention subject to the STL algorithms.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Quote Originally Posted by orion-
    why should you use a std::string insted of char array in c++?
    The way I had it explained to me is: a hacker can enter text that is longer than your char array's size and cin>> will happily read it all in(as long as there are no spaces). The hacker can put executable code in the text, and after it is read into memory, the code can somehow execute later. For instance, try this:
    Code:
    char str[3];
    cin>>str; ///abcdefg
    cout<<str[3]<<endl;
    If you enter a string with longer than 3 characters, cin reads it in and places it memory--even though it is memory your program doesn't own. In other words, cin>> will write data past the end of the array.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    >> Not to mention subject to the STL algorithms.
    Actually, the standard library algorithms are generic enough to work with C style arrays just like they do with C++ containers. Of course, C++ strings are still preferred for the other reasons.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Parsing a flat file to a SQl Server database?
    By Michael71 in forum C Programming
    Replies: 0
    Last Post: 01-16-2007, 11:45 AM
  2. C++ Text Parsing
    By LrdChaos in forum C++ Programming
    Replies: 2
    Last Post: 09-04-2002, 08:16 PM

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