Thread: While is acting strangely

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    101

    While is acting strangely

    Code:
    #include <iostream>
    
    using namespace std;
    
    int choice(0);
    void display (int);
    
    int main()
    {
        cout<<"Please enter the times table you would like to see : ";
        cin>>choice;
    
        while( choice >13 );
    
        if (choice>0)
            display (choice);
        else
            return 0;
    }
    
    void display(int)
    {
        cout <<endl<<endl;
        cout <<"Here is the "<<choice<<" times table :"<<endl;
    
        for(int x=1;x<13;x++)
            cout<<choice<<" times "<<x<<" = "<<choice*x<<endl;
    }
    The code should display a times table if 'choice' is less than 13 ie up to a 12 times table, but the while only works if i make it 'while choice >13' if I make it '<13' it just hangs but compiles OK.

    Also the program is exiting after the first table disply, shouldn't it keep going until I input '0'?

    Sorry if this is really simple I have spent an hour trying to get it to run properly!!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > while( choice >13 );
    Maybe use braces rather than ; to indicate what you want to loop?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    101
    Thanks Salem, it works fine now, especially after moving lines 10 and 11 within the choice loop as well.

    I just knew it was something easy!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fprintf behaving strangely
    By cwmccart in forum C++ Programming
    Replies: 6
    Last Post: 07-17-2008, 09:56 AM
  2. OpenGL - Lights behave strangely
    By Frobozz in forum Game Programming
    Replies: 5
    Last Post: 06-06-2004, 02:00 PM
  3. being a GPL ***** feels strangely fufilling...
    By moi in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 08-06-2002, 02:45 PM
  4. printf acting strangely
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 06-01-2002, 11:57 PM
  5. char* behaves strangely
    By Seron in forum C++ Programming
    Replies: 7
    Last Post: 01-12-2002, 04:04 PM