Why do this loop stops..!

This is a discussion on Why do this loop stops..! within the C++ Programming forums, part of the General Programming Boards category; I have tried to print the Characters.. but loop stops at 20 as i wanted but next it again ask ...

  1. #1
    Registered User jawwadalam's Avatar
    Join Date
    May 2002
    Posts
    131

    Why do this loop stops..!

    I have tried to print the Characters.. but loop stops at 20 as i wanted but next it again ask for character hit for each character till 26 ... there it stops completely.... what..is this..

    Code:
    #include <iostream.h>
    #include <conio.h>
    
    int main(void)
     {
      clrscr();
    
      cout<<"Number:\tResult"<<endl;
      cout<<"------:\t------"<<endl;
    
       for (int i = 0;i<256;i++)
        {
          cout<<i<<":\t"<< char(i)<<endl;
          if(i / 20)
           {
    	cout<<"Hit any key to continue...";
    	getch();
           }
        }
      cout<<"Asci Chart ended."<<endl;
    
      getch();
      return 0;
     }
    One day you will ask what more important to you..
    I will say my life..
    and You will leave me with even knowing
    that
    You are my Life (L)

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Ok, take a look at your statement..

    if( i / 20 )

    I think you are just trying to be silly with this. It would be much easier to read if you simply put

    if( i == 20 )

    Your way works...because 20 / 20 = 1, however this may not be immediately intuitive to another programmer. Anyways, when you are in that if statement look at what you are doing. You print out a line, then you wait for a key press. You never do anything to exit the for loop so it just keeps going. If you want to quit , add a break statement after getch( ); So it may look like this.

    Code:
    if( i == 20 ) // two equals for comparison
    {
      cout << "Press any key to continue.";
      getch( );
      break;
    }

  3. #3
    Registered User jawwadalam's Avatar
    Join Date
    May 2002
    Posts
    131
    Originally posted by MrWizard
    Ok, take a look at your statement..

    if( i / 20 )

    I think you are just trying to be silly with this. It would be much easier to read if you simply put

    if( i == 20 )

    Your way works...because 20 / 20 = 1, however this may not be immediately intuitive to another programmer. Anyways, when you are in that if statement look at what you are doing. You print out a line, then you wait for a key press. You never do anything to exit the for loop so it just keeps going. If you want to quit , add a break statement after getch( ); So it may look like this.

    Code:
    if( i == 20 ) // two equals for comparison
    {
      cout << "Press any key to continue.";
      getch( );
      break;
    }
    What do i am intending to do with the if condition that if the loop prints 20 values then it must stop and ask to character input so i can check the output of .. all values... because in my program.... i am using <<endl after every "cout" to display the code and the character...
    One day you will ask what more important to you..
    I will say my life..
    and You will leave me with even knowing
    that
    You are my Life (L)

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Okay...well that works when i = 20....because 20 / 20 is 1 which is analagous to true. But when i = 40 you will get 2 as the result... not good! You want

    if( i % 20 == 0 )

    That will give you every 20th one.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  5. #5
    Registered User jawwadalam's Avatar
    Join Date
    May 2002
    Posts
    131
    That problem solved.. thanx... but now it stoped at first 0 and then 20 ... but one problem still in the program is that it stops.. at 26... do not displays values till 255 ?????
    Last edited by jawwadalam; 11-14-2002 at 08:30 PM.
    One day you will ask what more important to you..
    I will say my life..
    and You will leave me with even knowing
    that
    You are my Life (L)

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Your code displayed all of them for me. Take out the break because obviously that isn't what you wanted there. Also change that one line to this...

    if( ( i % 20 == 0 ) && ( i != 0 ) )

    That'll take care of the case when i is 0.

  7. #7
    Registered User jawwadalam's Avatar
    Join Date
    May 2002
    Posts
    131
    Thanx.. for help.... but it did not showed after 25...... I 'm using tubro C/C++ 3.0
    Last edited by jawwadalam; 11-14-2002 at 09:10 PM.
    One day you will ask what more important to you..
    I will say my life..
    and You will leave me with even knowing
    that
    You are my Life (L)

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Dunno what to say then, works fine on mine. Just walk through using a debugger and see whats going on in there! Good Luck!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-01-2008, 10:09 AM
  2. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 01:13 AM
  3. A somewhat bizzare problem!!! - WHILE LOOP
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 03-31-2006, 07:19 AM
  4. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 04:45 PM
  5. Replies: 1
    Last Post: 11-19-2001, 04:45 PM

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