Thread: Basic compiling problem for a beginner

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    43

    Basic compiling problem for a beginner

    Im having problems with this code compiling:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
       int i = 0, n = 0, sum = 0;
    
           
       printf("\n%s", "Enter an integer. \n");  
       scanf("%d", &i);
    	    
       for (i > 0) 
    	{
    		n = (2 * i); 
    	   for (i = 1; i <= n; ++i)
    		   sum += i;               
    	 printf("sum = %d\n\n", sum); 
       }
    	
       /* for (i < 0) 
    	{
    	n = (2 * i) 
    	   for (i = 1; i <= n; ++i)
    		   sum += i;               
    	 printf("sum = %d\n\n", sum);); 
       }
     */
       
       
       return 0;
    }
    The errors are:
    v1.c: In function 'main':
    v1.c:15: error: parse error before ')' token
    v1.c: At top level:
    v1.c:33: error: parse error before 'return'

    line 15 is the line with "for (i > 0)". I commented out the second major for loop to simplify troubleshooting.
    What boneheaded thing did I do to get these parse errors?
    Any help appreciated,
    crazychile

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Alot is to do with your for loops.

    A for loop with no statements can be written as:

    Code:
    for ( ; i < 5;  )
    if you declared the control variable and the increment/decrement elsewhere in the program.
    Double Helix STL

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    for (i < 0) is invalid. Perhaps you meant while?

    http://www.cprogramming.com/tutorial/c/lesson3.html

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    43
    Both of your replies were very helpful. I need to write two versions of this. one using only for loops, and the other using only while loops. Your comments help me differentiate the two .

    Thank you very much!

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You seem to have mixed spaces and tabs in your code. This is not a good thing. As you can see, the indentation is all messed up. Try using one of them only.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem compiling simple code examples
    By Wintersun in forum Windows Programming
    Replies: 8
    Last Post: 08-14-2007, 10:30 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. Problem with extremely beginner exercise
    By Molokai in forum C++ Programming
    Replies: 11
    Last Post: 05-08-2007, 09:05 AM
  4. Visual Basic Adodc Problem
    By rahat in forum Windows Programming
    Replies: 1
    Last Post: 01-20-2002, 06:55 AM
  5. Problem with Borland 5.5 Compiling Windows Programs
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 08-28-2001, 09:04 AM