Loop/bool

This is a discussion on Loop/bool within the C++ Programming forums, part of the General Programming Boards category; Can anyone help me fix this problem I have in my program it keeps displaying the same cout statement no ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    5

    Loop/bool

    Can anyone help me fix this problem I have in my program it keeps displaying the same cout statement no matter what output.

    Code:
    // InvalidBinDr.cpp 
    
    #include<iostream>
    #include<string>
    #include<cctype>
    using namespace std;
    
    // FILL IN InvalidBin PROTOTYPE: 
    
     
    int main()
    {
    	// Variables local to main
    	bool inValid=true;	// True if an invalid binary # is entered
    	string input;// The user entered string
    
    while (true) {
        cout << "Enter a binary number (8 digits or less): ";
        getline(cin, input);
        if (input.size() <= 8) break;
        cerr << "You entered more than 8 digits.  Please re-enter your number." << endl;
    }
    
    
    
    	if(inValid)
    		cout << input << " is not a binary number." << endl;
    	else
    		cout << input << " is a binary number." << endl;
    
    
    
    	return 0;
    }
    
    // FUNCTION InvalidBin RETURNS TRUE IF THE STRING ENTERED BY THE
    // USER DOES NOT REPRESENT A VALID 8-BIT BINARY NUMBER OTHERWISE
    // IT RETURNS FALSE, (i.e. the string has 8 bits that are 1's or 
    // 0's only).

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    5
    anyone?

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,447
    > anyone?
    For someone who creates 2 threads, and bumps them in 10 minutes, no.

    need some help Bool and loops

    How To Ask Questions The Smart Way
    Questions are answered when we get around to answering them, not by you demanding every few minutes for special attention.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    May 2009
    Location
    Birmingham, UK
    Posts
    1
    You never modify the value of inValid. You set it to true at the top of the file and then later test it with an if(). Your code will *always* execute
    Code:
    cout << input << " is not a binary number." << endl;
    because inValid is *always* true.

    Is Salem a certain Mr. C who used to work at S*l*din?

Popular pages Recent additions subscribe to a feed

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