Program won't compile: Declaration syntax error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bowzer1999
    New Member
    • Aug 2008
    • 4

    Program won't compile: Declaration syntax error

    I am a beginner at C++ and would be very grateful if someone could simply tell me why my program will not compile.

    My program is as follows,

    #include <stdio.h>

    #define int 1ST VALUE = 23.0

    void main()
    {
    float 2nd_value;
    int 3rd_value, result:
    printf("Enter a value:");
    scanf("%c", 2nd_value;
    result=(1ST_VAL UE+2nd_value)/3rd_value);
    }

    The only error is,

    c(5,5): Declaration syntax error


    I will be extremely grateful of any help, thank you.
  • edwardrsmith
    New Member
    • Feb 2008
    • 62

    #2
    You are using macros incorectly. Instead of having
    Code:
    #define int var = 7
    it should look like
    Code:
    #define var 7
    When you use define, you are not creating a variable but marking something that the pre-compiler needs to take notice of. So when you compile the program, every instance of var that the pre-compiler runs accross it, it is replaced with 7.

    If you want a tutorial go to google, type in #define and look at the third link.

    Fixing that should take care of that error.

    After you fix that, depending on the compiler, you may have problems with variable names starting with numbers.

    Edward

    P.S. When it comes to programming, google is your best friend.

    Comment

    • Bowzer1999
      New Member
      • Aug 2008
      • 4

      #3
      I am not fully sure what you mean edward, could you please copy and paste the code i wrote with the correction then i can copy and paste that into C++ and hopefully ti will work, thanks

      Comment

      • edwardrsmith
        New Member
        • Feb 2008
        • 62

        #4
        Code:
        #include <stdio.h>
        
        #define VALUE_1  23.0
        
        void main(){
             float value2;
             int value3, result:
             printf("Enter a value:");
             scanf("%f", &value2);
             result=((VALUE_1 + value2)/value3);
        }
        There you go. I fixed a few other problems, such as using %c in scanf instead of %f and also changed the scanf line so it is passed a reference instead of a variable. I also changed the names so that they reflect a more apropriate naming style (one that should work on any compiler). Additionally I added the missing parantheses. Finally, I also changed the #define line so that it follows the apropriate style.

        I would really suggest taking a look at this site. The specific page I linked to discusses macros and the rest of the site is an amazing resource for c/c++ programming. There are other ones out there but this is one of my two favorites.

        Edward

        Comment

        • Bowzer1999
          New Member
          • Aug 2008
          • 4

          #5
          Thanks very much, i will indeed look at that site, i will get back you if i have any more problems as you seem to have a lot of experience in progamming.

          Rob

          Comment

          • Laharl
            Recognized Expert Contributor
            • Sep 2007
            • 849

            #6
            Also, do not use void main(). main() returns an int, so use int main() and return 0 at the end of the function.

            If you're really using C++, #include <cstdio>, not <stdio.h>, as that's the old (pre-1998) way (and you should probably be using <iostream> and namespace std anyway). If this is C, ignore that, as you're fine.

            Comment

            • Bowzer1999
              New Member
              • Aug 2008
              • 4

              #7
              Could you please correct the coded i posted and post back to me this way i can compare it to the code i had and see exactly what needs changing, thanks.

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                We can't do all your work.

                Just look at any C++ textbook for a few minutes.

                Comment

                Working...