Thread: While Loop does not have an output

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    4

    While Loop does not have an output

    Dear FIB + Others,

    I have typed the while loop as mentioned in the book "OOP in Turbo C++" by "Robert Lafore".

    I am supposed to get a prompt to input and and an output of what I typed. However, there is no cout << variable mentioned in the book.

    Code:
    // demonstration of while loop
    // as typed in the Bloodshed IDE
    
    #include <cstdlib> // winxp terminal handling lib
    #include <iostream>
    
    using namespace std; // may be something to do with winxp > terminal
    
    int main(int argc, char *argv[])  // win Xp terminal arguments
    {
        unsigned int i = 1 ;      
        while(i != 0)
        cin >> i;
       
        // Win XP terminal calls
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    Did I miss anything? Thanks in anticipation.

    Best Regards

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    361
    What your program is doing is this..

    It sets i = to 1, then the while loop checks to see if I is not equal to 0 and it executes the line after that while I is not equal to 0, so it should keep asking for input. Also..there is no cout to print the output.

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        unsigned int i = 1 ;      
        while(i != 0) {
        cin >> i;
        i--;
    }
       
        cin.get();    
        return EXIT_SUCCESS;
    }
    Try not to use system(), it is a bad command and should be avoided, search the boards if you want to know the details of why it is bad.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
    cout << "hello world" << endl;
    Just change the words to suit what you want them to say?
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-01-2008, 10:09 AM
  2. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  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. OUTPUT TO PRINTER using c++ ???
    By howhy in forum C++ Programming
    Replies: 12
    Last Post: 09-01-2005, 09:36 PM
  5. for loop or while loop
    By slamit93 in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2002, 04:13 AM