Thread: Help with loops

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    ------------
    Join Date
    Jun 2005
    Posts
    79
    I GOT IT!!! yay!!! lol...

    Code:
    #include <iostream>
    
    using namespace std;
    int main()
    {
    int number; // declare an integer named number
    
    do // start  a command
    {
    cout<<"Pick a number 1-5: ";
    cin>>number // get number
    cin.ignore(); // ignore decimals and spaces
    }
    while ( number == 0 or number >= 6 ); // if number isn't zero or is over 5
    }
    is a simple code i wrote to test out the do-while loops! that one i can tell how it is helpful, now to write one for the other ones... and i just reolized i comment a lot... at least i wount forget what each thing does!
    Last edited by Goosie; 06-21-2005 at 07:20 PM. Reason: fixing code tag...
    CAUTION: Newbie at programming!
    -------------------------------------------------
    Thanks everyone who helped me with all the questions, and utter lost-ness i happen to have... it is VERY VERY much appreciated

  2. #2
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Its great to keep it thoroughly commented.. it only helps, and it doesnt hurt

    Nice implimentation, I might go like this if you put error handling:

    Code:
      int number; // declare an integer named number
    
      while( number == 0 || number > 5 ) { // the symbols || is "or"
        cout << "Pick a number from 1-5: "; 
        cin >> number;
        cin.ignore();
    
        if( number == 0 || number > 5 ) {
          cout << "Error, incorrect choice: " << number;
        } else {
          cout << "Great! you chose: " << number << endl; 
        } // put an endl in there because youre wrapping back up there.
      }
    
      cin.get(); // used to hold the console window open
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

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. loops with incrementing strings .... ?
    By twomers in forum C++ Programming
    Replies: 1
    Last Post: 12-12-2005, 11:29 AM
  3. Evaluation of nested loops
    By Mister C in forum C Programming
    Replies: 2
    Last Post: 08-13-2004, 01:47 PM
  4. recoursion or loops?
    By Mecnels in forum C++ Programming
    Replies: 2
    Last Post: 01-14-2002, 12:09 PM
  5. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 04:28 PM