Thread: Loop Problem

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    23

    Loop Problem

    There is no error at least that Pelles C can find. I am just learning how loops work, and I am trying to figure out how to print out 1 - 99 without a bijillion printfs. Here is the code.
    Code:
    #include <stdio.h>
    
    int main( )
    {
        int x=1;
    while(x<100);
        printf("%d\n",x);
        x = x + 1;
    getchar();
    return 0;
    }
    Like I said, no error code.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Look closely at the end of your while loop.

    Jim

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    161
    Some errors.
    Should be:

    Code:
    #include <stdio.h> int main(void){ int x=1; while(x<100){ printf("%d \n",x); x = x + 1; //you can also us x++; } getchar(); return 0; }

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    23
    It's probably some super simple thing that I would see right away if I weren't a beginner. And by the way, let me redo that with comments.
    Code:
    /*
    Programmer : Caleb Spiess
             Date : 12/18/11
    Description : The program is supposed to output 1 - 99.  It doesn't work yet.
    ***************************************************
    */
    #include <stdio.h>
    
    int main( ) //main body
    {
        int x=1; //declares x to be 1
    while(x<100); //keeps the loop going while x is less than 100
            printf("%d\n",x); //this should print x every loop
        x = x + 1; //this should add 1 to x every loop
    getchar(); //this makes sure that the program doesn't breeze by the user : the idea is that they can press any key to continue
    return 0; //returns a value, indicating to the program that there was success
    }

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    23
    Wow. Thanks, FloridaJo. I was right - that was amazingly simple. I will remember that syntax forever, mostly because it caused me so much trouble not to.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Gigabitten View Post
    There is no error at least that Pelles C can find. I am just learning how loops work, and I am trying to figure out how to print out 1 - 99 without a bijillion printfs. Here is the code.
    Code:
    #include <stdio.h>
    
    int main( )
    {
        int x=1;
    while(x<100);
        printf("%d\n",x);
        x = x + 1;
    getchar();
    return 0;
    }
    Like I said, no error code.

    Lesson #1 ... "Compiles" does not mean "Works".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [C] for loop problem
    By SirSig in forum C Programming
    Replies: 5
    Last Post: 10-16-2011, 01:31 PM
  2. loop problem
    By sababa.sababa in forum C Programming
    Replies: 6
    Last Post: 12-20-2009, 02:56 PM
  3. Problem with do while loop
    By tru.cutru in forum C Programming
    Replies: 2
    Last Post: 06-29-2008, 10:36 PM
  4. loop problem
    By djhabeeb in forum C++ Programming
    Replies: 4
    Last Post: 12-08-2005, 07:57 AM
  5. problem with eof loop
    By Invincible in forum C++ Programming
    Replies: 2
    Last Post: 05-19-2002, 07:27 PM

Tags for this Thread