Need Help with this!

This is a discussion on Need Help with this! within the C Programming forums, part of the General Programming Boards category; I am trying to make a program that will later one test myself by asking me standard idoms(this is not ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    2

    Need Help with this!

    I am trying to make a program that will later one test myself by asking me standard idoms(this is not a assignment this is me practicing for my midterm).

    I am trying to make a check to compare my user input answer compared to the correct standard idom

    I am currently working on a standard idom to "procces an array"

    size_t i;
    for(i = 0; i < n; i++)

    I am currently working on a regular expresion on the for loop part this is what I have.


    if(sscanf(line2, "%[^([a-z][]=[][0-9];[][a-z][]<[][a-z];[][a-z++])",holdLoop)==1)

    but this does not seem to work. Also if anyone has a site with C sscanf regular expresion that would be very helpful(since I got alot of standard idoms to write with different expresions.

    Here is my whole code.

    [code]
    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    void idiom1(char idiomOne[]);
    int main()
    {
    	char prompt[] = "Please Enter In the correct idiom";
    	char idomOne[] = "Standard idom to process an array";
    	
    	idiom1(idomOne);
    	return 0;
    }
    
    void idiom1(char idiomOne[])
    {
    	char varType[1024];
    	char line[1024];
    	char compare[] = "size_t i;";
    	char forLoop[] = "for(i = 0; i < n; i++)";
    	char holdLoop[1024];
    	char line2[1024];
    	char prompt[] = "Enter the loop part now: ";
    	int  i;
    	
    	while(1)
    	{
    		
    		printf("%s: ", idiomOne);
    	
    		if(fgets(line, 1024, stdin) == 0)
    		{
    			clearerr(stdin);
    			break;
    		}
    		
    		for (i = 0; line[i] != '\0'; i++)
    		{
    			if (line[i] == '\n')
    			{
    				line[i] = '\0';
    			}
    		}
    		
    		if(sscanf(line, "%[^[][a-z];", varType) == 1)
    		{	
    			if(!strcmp(varType, compare))
    			{
    				printf("This is correct:");
    				printf("%s",prompt);
    				if(fgets(line2,1024,stdin) == 0)
    				{
    					clearerr(stdin);
    					break;
    				}
    				if(sscanf(line2, "%[^([a-z][]=[][0-9];[][a-z][]<[][a-z];[][a-z++])",holdLoop)==1)
    				{
    					fprintf(stderr,"does this work");
    					if(!strcmp(holdLoop,forLoop))
    					{
    						printf("correct for line2");	
    					}
    					else
    					{
    						printf("NOT correct for line2");
    					}
    					
    				}
    			}
    			else
    			{
    				printf("NOT What VarType Held %s \n", varType);
    			}
    		}
    	}
    		
    	
    }
    Last edited by chobo2; 07-15-2006 at 12:58 AM.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    scanf() doesn't support regular expressions. To learn about scanf() and its format string look here: http://www.die.net/doc/linux/man/man3/sscanf.3.html
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Jul 2006
    Posts
    2
    Ok I guess there not called regular expresion but thats what they bascily are and is does have some types. I just don't know how many and what they look like.

    but what I did is completly allowed I believe it just does not work

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Just because it compiles doesn't mean it works. So "completely allowed" doesn't mean jack in this case.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Quote Originally Posted by chobo2
    I just don't know how many and what they look like.
    I posted a link that explains exactly how many there and exactly what they look like. How spoonfed do you need to be? Why ask for help if you're not going to listen?
    If you understand what you're doing, you're not learning anything.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,453
    > Ok I guess there not called regular expresion
    No, they're called scan-sets
    So [a-z][a-z] is still just [a-z]
    They're a very pale imitation of true regular expressions.

    > "%[^([a-z][]=[][0-9];[][a-z][]<[][a-z];[][a-z++])"
    Which means what exactly?
    None of the following bunches of characters?

    > for(i = 0; i < n; i++)
    Are you going to interpret this in some way (like a compiler would)?
    At the moment, it just seems like you're just using strcmp() to match the whole input with the predefined answer.

    You may as well just use fgets(), strip off the newline and then compare with your prepared answer.

    But if the prepared answer is
    "size_t i;";
    "for(i = 0; i < n; i++)";
    And you want to allow
    "int j;"
    "for ( j = 0 ; j < x ; j++ )"
    as being an equivalent answer based on patterns in the code, then you need another approach.

    > but what I did is completly allowed I believe it just does not work
    So long as the " " match at both ends of the string, the compiler won't give a monkey's what you pass to sscanf.
    And sscanf() has no means of returning "your scanset is bogus", it will just return with 0 or EOF.
    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.

  7. #7
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Pet peeve, use a thread title that relates to your question... "Need help with this!" that would probably be self evident with the contents of the thread. "Regex help" or "sscanf() help" would be more appropriate.

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    18
    Quote Originally Posted by Wraithan
    Pet peeve, use a thread title that relates to your question...
    i disagree. thread titles like, "Not Working!!!", and "HELP!!!" are usually a really good indication of the type of poster to expect. they are also a good indication of where the problem lies.....

Popular pages Recent additions subscribe to a feed

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