Thread: obvious code does not compile

  1. #1
    Registered User
    Join Date
    Jul 2009
    Location
    San Jose
    Posts
    4

    obvious code does not compile

    Hi everybody,
    this is my first post here...I choose Kernighan , Ritchie Book to follow and refresh my knowledge in C and this site forum to accompany this process...
    I am using Microsoft Visual C++ 2008 to run the codes.
    the following code is basically an example from K,R Book and is for counting words and characters in a text.It does not compile and I spent a whole day but didnt figure out whats wrong

    Code:
    #include <stdio.h>
    #define in 1;
    #define out 0;
    
    void main(void){
    	int c=0;
    	int w=0;
    	short int state=out;
    	char ch;
    	ch=getchar();
    
    	while((ch=getchar())!='\n'){
    		c++;
    		if(ch==' ' || ch=='\t')
    			state=out;
    		else if(state==out){   //Line 16 which is source of all errors.
    			state==in;
    				w++;
    			}		
    		}	
    	printf("Totally %d words containing %d characters.",w,c);
    	}
    it displays following errors which as I check the syntax, its allready correct but again raise errors which I cant stand anymore !
    main.cpp(16) : error C2181: illegal else without matching if
    main.cpp(16) : error C2143: syntax error : missing ')' before ';'
    main.cpp(16) : error C2059: syntax error : ')'

    can anyone help whats the problem with this compiler ? Its the first examples of the book and too disappointing if it want to continue like this..

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    The problem is the stray semicolen in the macro, that is, after the macro expansion you have:

    Code:
    else if(state==0;)
    At any rate, you really shouldn't be using a macro, as it will prevent you from using in/out as function names, variables, etc. Use macros only when it cannot be avoided. A better way is to simply use a constant:

    Code:
    const int in = 1, out = 0;
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Also you should change your main() declaration to:
    Code:
    int main(void)
    and return 0 at the end of your main function.

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    One more thing to note is this line
    Code:
    state==in;
    this should be replaced by an assignment operator(=), I guess.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  5. #5
    Registered User
    Join Date
    Jul 2009
    Location
    San Jose
    Posts
    4
    Thank you very much....I correct them just now and it works.I should be more accurate reading and typing codes.

    BTW , I have used Eclipse editor before for java and it was just Great and helpful.It helps you with braces by highliting them , formats the code so you dont miss anything and so many other simple bur powerful features.You cant compare the producivity that Eclipse brings to you with MS VS.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by arash View Post
    It helps you with braces by highliting them
    So does VS.

    formats the code so you dont miss anything
    VS does automatic indentation for you. Were you thinking of something else too?

    and so many other simple bur powerful features.
    Such as?

    You cant compare the producivity that Eclipse brings to you with MS VS.
    Yes, you can.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Jul 2009
    Location
    San Jose
    Posts
    4
    Eclipse highlights braces and pranthesis , so you always will know which brace is matching which one by placing your cursur on that brace or pranthesis.MS VS as far as I know , just highlight when you first type a brace/parantheisis and after that forgets about that.
    Eclipse have options that automatically import/include header files you've missed, if you use a function from that library.Eclipse has refactoring ,which is of great use.
    Maybe MSVS has also, but it is not clear where it is. Eclipse makes life so easy so you just concentrate on your problem and dont waste time finding this and that option ...
    I am not fanatics about MS or Eclipse.But this is obvious in my opinion that IBM eclipse is a far better editor than MS VS..
    I should confess that I am not pro in programming,but some time ago I spend a lot of time developing java in eclipse and it was such a great Editor.

  9. #9
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Eclipse highlights braces and pranthesis , so you always will know which brace is matching which one by placing your cursur on that brace or pranthesis.MS VS as far as I know , just highlight when you first type a brace/parantheisis and after that forgets about that.
    All you have to do to find the matching brace is press ctrl + ] and it takes you there without having to scroll up/down like in eclipse; And the parens are done automatically. Looks pretty easy to me.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't think it's a far better editor. You do mention some features VS lacks, which could be nice, but not super required. I'll admit that those are nice features and because of them, Eclipse is a good editor, but then again, you can't compare editors purely on that.
    It's subjective, put simply.

    As for the individual features:
    - Re: parenthesises - it's true it only highlights them when you type them, but that's often enough. You should not nest them tightly together, because it makes code hard to read. It's better to use newlines and spaces so you don't really need those features in the first place.
    - re: Refactoring - Unfortunately, it lacks refactoring for C/C++. Thankfully, this should come in a coming version. Or you could find/use some 3rd party plugin.
    - re: Includes - VS is missing this. Nice feature it is, though.

    Quote Originally Posted by Spidey View Post
    All you have to do to find the matching brace is press ctrl + ] and it takes you there without having to scroll up/down like in eclipse; And the parens are done automatically. Looks pretty easy to me.
    Except that keyboard combination may be difficult on non-US keyboards. But hey, it's reassignable.
    Last edited by Elysia; 07-23-2009 at 04:19 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    I think people get used to the features their IDE supports, and then they begin to rely on those features. This is what commonly causes the "My IDE is the best" ideology. If another IDE doesn't have the features you've come to rely on, then it can't be as good as the one you currently use.

    Personally, the best IDE for me is vi. I realize most people would disagree with vi being the best, but I've been using it so long that all the key shortcuts are second nature to me. I feel slow and awkward when I have to use a different IDE these days. It doesn't mean those IDE's are not as good, it just means they are not as good for me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code won't compile
    By monkles in forum C Programming
    Replies: 3
    Last Post: 05-28-2009, 01:45 PM
  2. Compile Errors in my Code. Can anyone help?
    By DGLaurynP in forum C Programming
    Replies: 1
    Last Post: 10-06-2008, 09:36 AM
  3. This code won't compile in Red Hat Linux 9
    By array in forum Linux Programming
    Replies: 7
    Last Post: 04-27-2004, 06:30 AM
  4. How do they compile code for an OS ?
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 49
    Last Post: 03-28-2002, 12:16 AM
  5. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 12:51 PM

Tags for this Thread