Nested Loop problem

This is a discussion on Nested Loop problem within the C Programming forums, part of the General Programming Boards category; Hi Could anyone help me please Im only learning C Im surprised Ive got as far as I have! It ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    2

    Unhappy Nested Loop problem

    Hi Could anyone help me please
    Im only learning C Im surprised Ive got as far as I have! It works once - but it wont loop. So Ive been looking up nested loops and I just cant see where Im going wrong.
    and thanks if you're even reading this.
    Last edited by chead; 01-04-2005 at 09:30 AM.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,171
    Put your program inside CODE tags.
    If you understand what you're doing, you're not learning anything.

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Well, I think you might want to consider resetting your num variable to 0 when the user chooses to go again.

    Other than that, have a look at your variables - if you are using a variable, you should explicity initialise it before you use it - otherwise you are using 'junk' values, and you could get some unexpected results. In particular, your first while loop uses a variable which is not initialised. Also, initialising the variable num to 1 gives some problems as well. Test the program, and see if you can see what is wrong with it. Hint: the numbers are off by 1.

    ~/
    Last edited by kermit; 01-03-2005 at 05:46 PM.

  4. #4

  5. #5
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    It works, but it is not much better than what you had before. Do you code like that, with no indentation? It makes it much nicer to read if you indent your code.

    ~/

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,171
    is it a mess?
    Yes.

    Seriously though, if it's hard to follow then it's not good. You can see that after you start your while() loop everything kind of suddenly slams up against the left margin. Indentation should be used to let you easily see which block of code you're currently looking in. Indentation goes hand in hand with bracing style.

    Here's a link for you: http://burks.brighton.ac.uk/burks/foldoc/80/56.htm
    If you understand what you're doing, you're not learning anything.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    2
    Maybe I should have another variable after each if?

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,171
    I think I would probably do it something like this:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      int table;
      int num;
      int try;
      int ans;
      int another;
    
      puts("Welcome to Gill's Multiplication loopers tutorial");
    
      do
      {
        puts("Please choose a table");
        scanf("%d", &table);
        printf("You have chosen %d times tables\n", table);
    
        for(num = 1;num < 12;++num)
        {
          for(try = 0;try < 2;++try)
          {
            printf("%d * %d = ", table, num);
            fflush(stdout);
            scanf("%d", &ans);
    
            if(table * num == ans)
            {
              printf("well done on %s try!\n", try ? "2nd" : "1st");
              break;
            }
            else
            {
              if(try)
              {
                puts("WRONG WRONG WRONG YOU FOOL! on 2nd try");
                printf("The answer is %d\n", table * num);
              }
              else
              {
                puts("WRONG WRONG WRONG YOU FOOL! on 1st try");
                puts("Try again");
              }
            }
          }
        }
    
        puts("would you like to try another? y=1/n=0");
        scanf("%d", &another);
      } while(another);
    
      return 0;
    }
    See how much easier it is with proper indentation to tell which code block you're looking at?

    I'm not sure why you were using this crazy test (table==ans/num)&&(ans%num==0) when you can just check table * num == ans.
    Last edited by itsme86; 01-04-2005 at 12:54 AM.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with a loop
    By wiggsfly in forum C Programming
    Replies: 5
    Last Post: 11-30-2008, 12:45 PM
  2. whats the problem....
    By vapanchamukhi in forum C Programming
    Replies: 3
    Last Post: 09-05-2008, 01:19 PM
  3. do while loop problem
    By nelinda in forum C Programming
    Replies: 1
    Last Post: 11-30-2003, 09:29 AM
  4. output from nested while loop
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 08-22-2002, 10:30 AM
  5. Nested Loop
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 05-07-2002, 01:40 AM

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