This one is so annoying(Problem with my while loop)

This is a discussion on This one is so annoying(Problem with my while loop) within the C Programming forums, part of the General Programming Boards category; Hi.I am stuck on this question for a week .i need alil help from you guys.... This question is taken ...

  1. #1
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356

    Angry This one is so annoying(Problem with my while loop)

    Hi.I am stuck on this question for a week .i need alil help from you guys....

    This question is taken from the book ( C How to program 3/e by Deitel & Deitel) from the chapter 3 , question 3.38.

    3.38 Write a program that prints 100 asterisks, one at a time. After every tenth asterisk, your program should print a newline character. (Hint: Count from 1 to 100. Use the modulus operator to recognize each time the counter reaches a multiple of 10.)

    Above is the question .To make the question more clear hear is what the output should be for the program.

    **********
    **********
    **********
    **********
    **********
    **********
    **********
    **********
    **********
    **********

    Above is the output for the program .Below is the code i wrote for this Question.

    #include <stdio.h>

    int main()
    {
    int cnt;
    cnt =0;
    clrscr();

    while ( ++cnt <= 100 ){
    printf ("*");
    if ( cnt == 10 )
    printf ("\n");
    cnt = 0
    }

    return 0;
    }

    My programm is giving me an infinite loop.

    Thanks alot

  2. #2
    Unregistered
    Guest
    Look at your code:

    int main()
    {
    int cnt;
    cnt =0;
    clrscr();

    while ( ++cnt <= 100 ){
    printf ("*");
    if ( cnt == 10 )
    printf ("\n");
    cnt = 0
    }

    return 0;
    }


    You are resetting cnt to 0 everytime that it reaches 10 - therefore it will never reach 100 to break out..

    Look at how to use the modulus operator....

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    You get an infinite loop because you keep setting cnt to 0. That way it will never reach 100 or even 10. You need to count up from 0 or down from 100 and check to see if the value is divisible by 10. Then depending on whether it is or not, print the corresponding character. The modulus operator is perfect for this:
    Code:
    #include <stdio.h>
    
    int main ( void )
    {
      int i = 100;
      while ( --i >= 0 )
        printf ( "%c", ( i % 10 ) ? '*' : '\n' );
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356

    thanks

    Thanks alot ..

  5. #5
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356

    i am sorry but i am a bit confused

    well the printf statement is a bit new to me

    help me out

    printf ( "%c", ( i % 10 ) ? '*' : '\n' );

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    The conditional operator is just like an if..else:
    Code:
    printf ( "%c", ( i % 10 ) ? '*' : '\n' );
    is equivalent to
    Code:
    if ( i % 10 )
      printf ( "%c", '*' );
    else
      printf ( "%c", '\n' );
    You can think of ? as if and : as else.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 10:44 AM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  4. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 08:14 AM
  5. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM

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