need some help Bool and loops

This is a discussion on need some help Bool and loops within the C++ Programming forums, part of the General Programming Boards category; Ok I am making a program that the user has to enter up to 8 digits to make a binary ...

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

    need some help Bool and loops

    Ok I am making a program that the user has to enter up to 8 digits to make a binary number so it they have to be 0 or 1. I need help in 2 spots how do I limit cin to only allow user to input up to 8 characters. and cant get my loops to work right. Please help me =)


    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 strVal; // The user entered string
    
    
    
    cout << "Please enter a string." << endl;
    cin >> strVal;
    
    if(inValid)
    cout << strVal << " is not a binary number." << endl;
    else
    cout << strVal << " is a binary number." << endl;
    while(strVal=="0" || strVal=="1")
    
    
    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
    ok have gotten this far now. now I cant get my loop to work correct. anyone help I bad at loops.
    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
    	char strVal;	// The user entered string
    
    
    
    string input;
    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;
    }
    cin >> strVal;
    
    
    	if(inValid)
    		cout << strVal << " is not a binary number." << endl;
    	else
    		cout << strVal << " 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).

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    5
    ok I corrected a few mistakes. But still cant get my loop to work right..

    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).
    Help anyone?

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I don't see any loop that actually tries to determine if the input is a binary number. Once you have obtained input that satisfies you, you'll have to loop to inspect each character of the string in turn and set inValid to false if the current character is not '0' or '1'.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with loops
    By fsufan22 in forum C++ Programming
    Replies: 7
    Last Post: 02-21-2008, 05:03 PM
  2. Loops: The 'any' inquiry
    By chubbs1900 in forum C++ Programming
    Replies: 6
    Last Post: 12-10-2007, 10:35 AM
  3. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 01:18 AM
  4. Issues with variables and for loops
    By Mr_Jack in forum C++ Programming
    Replies: 1
    Last Post: 01-10-2004, 08:48 AM
  5. overloaded *operator, decisions, and loops
    By ronin in forum C++ Programming
    Replies: 2
    Last Post: 02-12-2003, 10:22 AM

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