Thread: Help with Loops

  1. #1
    Registered User
    Join Date
    Sep 2012
    Location
    Clemson, SC USA
    Posts
    19

    Help with Loops

    I'm just starting out with C and I'm off to a bad start. First off, this isn't homework, this is just me trying to familiarize myself with the idea of the Loops. I have two programs, neithither of which are compiling through the Virtual Box uBuntu and the terminal emulator. I got the code off of this site's tutorial.

    Any insight?

    This is the while loop.
    Code:
    #include <stdio.h>
    
    intmain()
    {
        int x = 0;
        
        while ( x < 10 ) {
            printf( "%d \n", x );
            x++;
        }
        getchar();
    }
    And here is the for loop.
    Code:
    #include<stdio.h>
    
    int main()
    {
        int x;
        for (x=0; x < 10; x++) 
        {
            printf( "%d\n", x );
        }
        getchar();
    }
    I'm aware that these should be two very easy concepts consdering the semester is only 3 weeks in.

    Thanks for any help!
    Robert

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    int main() should be returning 0 on it's last line of code. That's what the "int" means. (That 0 goes to the operating system, and is usually used to mean the program ran without run-time errors).

    "intmain" should be int main, in the first example, of course.

    And welcome to the forum, Robert!

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by Robert Harp View Post
    I have two programs, neithither of which are compiling through the Virtual Box uBuntu and the terminal emulator. Robert
    Why not? If it doesn't tell you why they don't compile, use something that will give you a list of errors. Like a real compiler.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #4
    Registered User
    Join Date
    Sep 2012
    Location
    Clemson, SC USA
    Posts
    19
    Appreciate the feedback, I'll give it a run.

    @WaltP - I'm a student at Clemson, I just use what they tell me to...

    EDIT: Works now. Thanks a bunch!
    Last edited by Robert Harp; 09-23-2012 at 01:01 AM.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by Robert Harp View Post
    I'm a student at Clemson, I just use what they tell me to...
    Not the point. Things that are broken make errors.

    Code:
    #include <stdio.h>
    
    intmain()
    {
        int x = 0;
    
        while ( x < 10 ) {
            printf( "%d \n", x );
            x++;
        }
        getchar();
    }
    
    C:\Users\Josh2\Documents\foo\main.c:4: warning: return type defaults to 'int'
    C:\Users\Josh2\Documents\foo\main.c: In function 'intmain':
    C:\Users\Josh2\Documents\foo\main.c:12: warning: control reaches end of non-void function
    Process terminated with status 0 (0 minutes, 0 seconds)
    0 errors, 2 warnings
    This is often an extremely useful list for people answering questions.

  6. #6
    Registered User
    Join Date
    Sep 2012
    Location
    Clemson, SC USA
    Posts
    19
    It gave a huge list of errors, I just brainfarted and didn't think about "intmain()" being "int main()" ... It worked fine after that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. converting for loops to while loops
    By nabhatt in forum C Programming
    Replies: 3
    Last Post: 02-16-2012, 09:45 PM
  2. Replies: 3
    Last Post: 06-01-2011, 04:19 PM
  3. loops, menu loops
    By gloworm in forum C Programming
    Replies: 17
    Last Post: 04-12-2010, 07:59 PM
  4. loops with if else
    By sweetly in forum C++ Programming
    Replies: 2
    Last Post: 10-07-2003, 03:26 AM
  5. loops
    By happy in forum C Programming
    Replies: 16
    Last Post: 11-17-2002, 09:34 PM