syntax error before { line 29 and syntax error before } line 45? gcc mac.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • abrown07
    New Member
    • Jan 2010
    • 27

    syntax error before { line 29 and syntax error before } line 45? gcc mac.

    Code:
    #include<stdio.h>
    int main(void)
    
    {
    	
    	double Yeild, Heads, Seedhead, Size, unit;
    		
    		printf("Please enter 1 for US customary Units or 2 for metric units: ");
    		scanf("%lf", &unit);
    		if(unit=1) 
    		{
    		
    		printf("Please give the number of heads per square foot: ");
    		scanf("%lf", &Heads);
    			
    	
    		printf("Please input the number of seeds per head: ");
    		scanf("%lf", &Seedhead);
    	
    		printf("Please idicate the number of seeds per pound: ");
    		scanf("%lf", &Size);
    	
    		Yeild= 726*Heads*(Seedhead/Size);
    	
    		printf("The potential grain yeild is: %.2f bushels/acre\n",Yeild);
    		return 0;
    		}
    		else(unit=2)
           {     //*line 29
    		 printf("Please give the number of heads per square meter: ");
    		 scanf("%lf", &Heads);
    		 
    		 printf("Please input the number of seeds per head: ");
    		 scanf("%lf", &Seedhead);
    		 
    		 printf("Please indicate the number of seeds per kilogram: ");
    		 scanf("%lf", &Size);
    		 
    		 Yeild= 726*(Heads*10.764)*(Seedhead/(Size/2.204));
    		 
    		 printf("The potential grain yeild is: %.2f kg/ha\n",Yeild);
    		 return 0;
    	    }
    		 
    }     //* line 45
    Last edited by Banfa; Feb 25 '10, 12:15 AM. Reason: Added [code] ... [/code] tags
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    if(unit=1)

    Don't you mean if (unit == 1) ?

    unit=1 will always be true.

    Ditto for the else.

    Common confusion between the equality and assignment operator.

    Comment

    • abrown07
      New Member
      • Jan 2010
      • 27

      #3
      I changed that however I am still getting the same error when i try to compile?

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        line 28 is not C/C++ syntax, look up the syntax of if/else if/else

        Comment

        • nohimn
          New Member
          • Feb 2010
          • 6

          #5
          else is not supposed to have an argument. Try else if.

          Comment

          • abrown07
            New Member
            • Jan 2010
            • 27

            #6
            compiller says there is a syntax error before printf at line 30 ( indicated )??

            #include<stdio. h>
            int main(void)

            {

            double Yeild, Heads, Seedhead, Size, unit;

            printf("Please enter 1 for US customary Units or 2 for metric units: ");
            scanf("%lf", &unit);
            if(unit==1)
            {

            printf("Please give the number of heads per square foot: ");
            scanf("%lf", &Heads);


            printf("Please input the number of seeds per head: ");
            scanf("%lf", &Seedhead);

            printf("Please idicate the number of seeds per pound: ");
            scanf("%lf", &Size);

            Yeild= 726*Heads*(Seed head/Size);

            printf("The potential grain yeild is: %.2f bushels/acre\n",Yeild);

            }
            else(unit==2)
            /* (ln30)*/ printf("Please give the number of heads per square meter: ");
            scanf("%lf", &Heads);

            printf("Please input the number of seeds per head: ");
            scanf("%lf", &Seedhead);

            printf("Please indicate the number of seeds per kilogram: ");
            scanf("%lf", &Size);

            Yeild= 726*(Heads*10.7 64)*(Seedhead/(Size/2.204));

            printf("The potential grain yeild is: %.2f kg/ha\n",Yeild);
            return 0;


            }

            Comment

            • abrown07
              New Member
              • Jan 2010
              • 27

              #7
              okay that helps thanks alot! Im new to this whole thing and its makin my head spin.

              Comment

              • donbock
                Recognized Expert Top Contributor
                • Mar 2008
                • 2427

                #8
                Edit: I just noticed that you have the same question in another thread; and that this answer was provided by somebody else in that thread. I just wasted my time answering this thread! Please don't double-post your questions.
                =============== =======

                Please use CODE tags -- they make it much easier for us to help you with your source code.
                Code:
                if (unit == 1)
                {
                ...
                }
                else (unit == 2)
                ...
                You want "else if (unit == 2)" and you want curly braces around the else-if clause. Better than that, you probably really want
                Code:
                switch (unit)
                {
                   case 1:
                      ...
                   break;
                   case 2:
                      ...
                   break;
                   default:
                      ...
                }

                Comment

                • Banfa
                  Recognized Expert Expert
                  • Feb 2006
                  • 9067

                  #9
                  Threads merged

                  Comment

                  Working...