Thread: Loops Trouble

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    2

    Loops Trouble

    I am trying to generate a table of squares using for, while, and do loops. The loop will square every number between and including 1 and the number the user puts in. At the end of the for, while, and do loops, the user will be asked if they wish to do another table. Entering 'y' will prompt for another integer and so on.

    Alright, so here's my code:

    Code:
    #include <stdio.h>
    
    main (void)
    {
         int x, j;
         char choice;
         
             printf("Please enter a number (1-1000) to display the squares:");
             scanf("%d", &x);
             x = getchar ();
             
             j = 1;
             while ( j <= x)
             {
               j++;
              printf("Squares Table: %d             %d \n", j, j*j);
              
              }
             
    /*{   for (j <=x;);
    
             j++;
             printf("Squares Table: %d             %d \n", j, j*j);
             
             }
         */
         do
         {
              j++;
              printf("Squares Table: %d             %d \n", j, j*j);
              
              }
         while (j <=x);
         
         printf("Would you like to generate a squares table (y/n)?");
         scanf("%c", &choice);
         choice = getchar();                  /*Assign to character*/
         while (choice == 'y');
    }
    I'm having a problem with the 'y' restarting the loop (instead of just exiting). Also, no matter what number I enter, the loop always stops at squaring 12.

    And the for loop is just confusing the heck out of me.

    I'm sorry if this sounds like a lot - I'm very new at this and it is not treating me right. Thanks so much for your help.

  2. #2
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    First off, after getting the number the user inputs, you are making a call to getchar. This is retrieving the leftover '\n' in the buffer (ASCII code 13?) so x now contains 13.

    Structure of a for loop:
    Code:
     for( a = 0; a < b; a++ )
    {
            //Do Stuff Here
    }
    The first statement initialises a variable. The second statement is the statement which is tested each time the loop runs through and the third statement is what happens to the variable every time the loop runs through.

    The last part, 'while (choice == 'y');' is testing whether choice is y (which it isn't, it's a newline, see the first thing I said, you did it again here), but you haven't put any code for it to execute.

    You should put the whole program into a do while loop. Test whether the user wants to run it again near the end of the loop and the statement to test should be whether they input y or n.

    Oh, and also, main should be 'int main(void)' and the last statement of the program (before the last }) should be return 0. This returns 0 to the operating system, which means that it was executed successfully.
    Last edited by DeadPlanet; 04-17-2009 at 12:31 AM.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    2
    Thank you so much!

    I fixed my code as per your suggestions, and it works fine now. You were extremely helpful!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 01:18 AM
  2. having trouble using loops
    By wolverine12345 in forum C++ Programming
    Replies: 34
    Last Post: 11-28-2007, 06:30 PM
  3. trouble scanning in... and link listing
    By panfilero in forum C Programming
    Replies: 14
    Last Post: 11-21-2005, 12:58 PM
  4. a little trouble with for loops
    By melee in forum C Programming
    Replies: 6
    Last Post: 10-19-2004, 01:46 AM
  5. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 04:28 PM

Tags for this Thread