Thread: C Help

  1. #61
    Registered User
    Join Date
    Nov 2007
    Posts
    38
    Honestly I dont
    Which is why Im asking. Oh and I corrected the "/n" and switched it to "\". But yeah when I remove the entire else statement it works fine. I dont get it

  2. #62
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That's because your brackets makes the compiler think it belongs to the FOR loop. That's why we use identation. See, it's very easy to see now that I intended it properly.

  3. #63
    Registered User
    Join Date
    Nov 2007
    Posts
    38
    Thats interesting. I never honestly thought indentations had such a big impact on how the compiler looks at things. Ill go ahead and make the necessary adjustments so they match what u did, and see what happens.

    But the reason why I never thought about is is because I have worked with Java and Scheme, and never really had a problem with indentations...

  4. #64
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The compiler doesn't care about indentation - but YOU do. It makes it so much easier to read. You've basically put the else-statement in the wrong place, and it's very easy to see with proper indenting.

  5. #65
    Registered User
    Join Date
    Nov 2007
    Posts
    38
    Alright well, lets see what happens...

  6. #66
    Registered User
    Join Date
    Nov 2007
    Posts
    38
    Ok I see it!

  7. #67
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    This is one reason that Python is a very good language to start learning in - indentation is part of the "syntax" there, so if you write:
    Code:
    ...
       if (x > y)
          print x
       print y
    then print x is part of the if-statement, print y isn't, purely based on indentation. So to get the correct behaviour, you HAVE to indent correctly.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #68
    Registered User
    Join Date
    Nov 2007
    Posts
    38
    I honestly don't see it...

    and I asked a friend of mine who has some experience with C and he doesnt see it either...

    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    { 
    
    	FILE *fp;
    	char arr[200];
    	int filename;
    	int numLines;
    	const int MAX_LINES = 1;
        for (filename=1;filename<argc;filename++)
        { 
                fp=fopen(argv[filename], "r");
                if (fp !=NULL)
                {
               
                         for(numLines=1;numLines<=MAX_LINES;numLines++)
                         {
                     
                                 fgets(arr,200,fp);
                                 fputs(arr,stdout);
                         }
                }
    	  
    
        }
          else
    	 fprintf(stderr, "the file %s doesn't exist/n", argv[filename]);
       
    
                 
    }
    I moved the else statement, but it gives me the same parse error.
    So I guess Im stuck again...

    I have a feeling Ill end up kicking myself cuz its something simple

  9. #69
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by CrazyShellsSlam View Post
    I honestly don't see it...

    and I asked a friend of mine who has some experience with C and he doesnt see it either...

    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    { 
    
    	FILE *fp;
    	char arr[200];
    	int filename;
    	int numLines;
    	const int MAX_LINES = 1;
        for (filename=1;filename<argc;filename++)
        { 
                fp=fopen(argv[filename], "r");
                if (fp !=NULL)
                {
               
                         for(numLines=1;numLines<=MAX_LINES;numLines++)
                         {
                     
                                 fgets(arr,200,fp);
                                 fputs(arr,stdout);
                         }
                }
    
    		else
    			fprintf(stderr, "the file &#37;s doesn't exist/n", argv[filename]);	  
    
        }
    
       
    
                 
    }
    I moved the else statement, but it gives me the same parse error.
    So I guess Im stuck again...

    I have a feeling Ill end up kicking myself cuz its something simple
    Dude, already told you. Look above. Move the else after the IF.

  10. #70
    Registered User
    Join Date
    Nov 2007
    Posts
    38
    Oh alright...sorry guess I missed that. Well I guess I learned a thing or two about this. As long as it makes me a better programmer I guess. Thanks alot guys.

Popular pages Recent additions subscribe to a feed