What is wrong with my code?

This is a discussion on What is wrong with my code? within the C Programming forums, part of the General Programming Boards category; Hi, this code should make a c file(it wont compile it) by my preferences... Code: #include <stdio.h> #include <string.h> #define ...

  1. #1
    Registered User yann's Avatar
    Join Date
    Sep 2009
    Location
    Zagreb, Croatia
    Posts
    186

    What is wrong with my code?

    Hi, this code should make a c file(it wont compile it) by my preferences...
    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define txt ".txt"
    #define dat ".dat"
    #define c ".c"
    #define cmd1 "electropulse"
    #define cmd2 "weight"
    #define cmd3 "exit"
    #define cmd4 "phi"
    #define cmd5 "pi"
    #define cmd6 "time"
    #define cmd7 "print"
    
    int var[100];
    int var2[100];
    int searchn = 0;
    int lookn = 0;
    char pn[100];
    
    FILE *f;
    
    
    //look for
    int ep(){
        	fprintf(f, "int ep(){\n search for eps\n }\n");
        	return;
    }
    
    int we(){
        	fprintf(f, "int we(){\n   measure weight;\n   return;\n   }\n");
        	return;
    }
    
    
    //search for
    int phi(){
    	fprintf(f, "int phi(){\n    search for phi;\n  return;\n   }\n");
    	return;
    }
    
    int pi(){
    	fprintf(f, "int pi(){\n    search for pi;\n    return;\n   }\n");
    	return;
    }
    int e(){
    	fprintf(f, "int e(){\n    search for e;\n   return;\n   }\n");
    	return;
    }
    
    //body
    int pre(){
    	fprintf(f, "#include <stdio.h>\n #include <string.h>\n\n");
    	return;
    }
    
    int m(){
    	fprintf(f,"int main(){\n");
    	if(var[0] == 1) { fprintf(f, "ep();"); }	
    	if(var[1] == 1) { fprintf(f, "we();"); }
    	if(var2[0] == 1) { fprintf(f, "(phi);"); }
    	if(var2[1] == 1) { fprintf(f, "(pi);"); }
    	if(var2[2] == 1) { fprintf(f, "(e);"); }	
    	fprintf(f,"}\n");
    	return 0;
    }
    
    
    
    
    int main() {
    	printf("Project name: ");
    	scanf("%27[a-zA-Z0-9_-]", pn);
    	strcat(pn,c);
    	f = fopen(pn,"w");
    	pre();
    	while(pn != cmd3 || searchn<2){    	
    	printf("search for\n");	
    	scanf("%27[a-zA-Z0-9_-]", pn);	
    	if(pn==cmd1){
            ep();
    	var[0]=1;    	
    	}
    	if(pn==cmd2){
            we();
    	var[1]=1;	
    	}
    	searchn++;	
    	}
    	while(pn != cmd3 || lookn<2){    	
    	printf("look for");	
    	scanf("%27[a-zA-Z0-9_-]", pn);
    	if(pn==cmd4){
            phi();
    	var2[0]=1;    	
    	}
    	if(pn==cmd5){
    	pi();
    	var2[1]=1;
    	}
    	if(pn==cmd6){	
    	e();		
    	var2[2]=1;	
    	}
    	lookn++;	
    	}	
    	m();
    	fclose(f);
    	return 0;
    }
    But something is wrong in the red part, instead of waiting for my input, it goes to infinite loop, anyone can help me?
    Arduino rocks!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If it won't compile, why not? Compile with warnings and errors turned on.


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

  3. #3
    Registered User yann's Avatar
    Join Date
    Sep 2009
    Location
    Zagreb, Croatia
    Posts
    186
    o, MY(this) program does compile with no errors, but it makes another c file, which is another story...
    Arduino rocks!

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Of course it makes a c file. You're telling it to:
    Code:
    #define c ".c"
    ...
    	strcat(pn,c);
    	f = fopen(pn,"w");
    What did you expect?


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

  5. #5
    Registered User yann's Avatar
    Join Date
    Sep 2009
    Location
    Zagreb, Croatia
    Posts
    186
    I know it does... But, read my first post, there aren't any problems with that, the problem is somewhere here:

    Code:
    	while(pn != cmd3 || searchn<2){    	
    	printf("search for\n");	
    	scanf("%27[a-zA-Z0-9_-]", pn);	
    	if(pn==cmd1){
            ep();
    	var[0]=1;    	
    	}
    	if(pn==cmd2){
            we();
    	var[1]=1;	
    	}
    	searchn++;	
    	}
    This goes to a infinite loop of printing "search for" but it shouldn't...
    Arduino rocks!

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    ==, !=, etc are numeric comparisons -- so when used with a char pointer, the addresses are compared, not the strings.

    To compare strings, use strcmp:
    Code:
    while((strcmp(pn,cmd3) !=  0) || (searchn<2))
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User yann's Avatar
    Join Date
    Sep 2009
    Location
    Zagreb, Croatia
    Posts
    186
    OK, but still I have the same problem)=.
    Arduino rocks!

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by yann View Post
    I know it does... But, read my first post, there aren't any problems with that, the problem is somewhere here:
    Make up your mind!

    First you said... "It doesn't compile..."
    Then you said... "Oh wait, no it does. But it's not making the right file, it's making a .c file and that's wrong!"
    Then you said... "No, wait, that's right, but something else is wrong!"

    Learn to ask smart questions! How To Ask Questions The Smart Way


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

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by yann View Post
    OK, but still I have the same problem)=.
    This is where those "printf" statements come in handy for debugging:
    Code:
    printf("\npn=\"%s\"\n"),pn);   fflush(stdout);
    put that at the beginning of the loop so you can see what's happening.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Registered User yann's Avatar
    Join Date
    Sep 2009
    Location
    Zagreb, Croatia
    Posts
    186
    Quote Originally Posted by quzah View Post
    Make up your mind!

    First you said... "It doesn't compile..."
    Then you said... "Oh wait, no it does. But it's not making the right file, it's making a .c file and that's wrong!"
    Then you said... "No, wait, that's right, but something else is wrong!"

    Learn to ask smart questions! How To Ask Questions The Smart Way


    Quzah.
    Well, maybe you didn't understand me in a proper way, I said "it makes a c file(it doesn't compile IT)" I didn't said that my program doesn't compile...
    Arduino rocks!

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Ok, so it still remains that you didn't explain your issue at all. You didn't say what it was supposed to do compared to what actually happens. If you have a problem, you need to say:

    "I'm trying to do _this_ but _this_ happens instead."

    You also want && not ||.


    Quzah.
    Last edited by quzah; 10-10-2009 at 06:34 AM.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User yann's Avatar
    Join Date
    Sep 2009
    Location
    Zagreb, Croatia
    Posts
    186
    Well, pn is still name.c, because I haven't changed it yet, what is wired is that program doesn't stop when it gets to scanf function, or when the searchn is higher than 2...
    Arduino rocks!

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by yann View Post
    Well, pn is still name.c, because I haven't changed it yet, what is wired is that program doesn't stop when it gets to scanf function, or when the searchn is higher than 2...
    That's because you used OR.


    Quzah.
    Last edited by quzah; 10-10-2009 at 06:38 AM.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User yann's Avatar
    Join Date
    Sep 2009
    Location
    Zagreb, Croatia
    Posts
    186
    Yes, now I fixed that, but it still skips my input...
    Arduino rocks!

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So where's your revised code? Post a short version without all those function calls that we don't care about. Put in some printf statements to show what your actual variables and input are showing at the area you get stuck. Seriously, you need to ask questions wisely, and learn to debug, even if it's just by using printf. My guess is it doesn't like what you're typing (your scanf isn't doing what you expect), or there's a newline stuck in your input.


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

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's wrong with my code?
    By x2x3i5x in forum C Programming
    Replies: 6
    Last Post: 09-28-2009, 11:52 AM
  2. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  3. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 09:11 AM
  4. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 05:32 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM

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