Basic compiling problem for a beginner

This is a discussion on Basic compiling problem for a beginner within the C Programming forums, part of the General Programming Boards category; Im having problems with this code compiling: Code: #include <stdio.h> int main(void) { int i = 0, n = 0, ...

  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,475
    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.
    I'm just trying to be a better person - My Name Is Earl

  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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,593
    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, 05: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21