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.
// InvalidBinDr.cpp
#include<iostream>
#include<string>
#include<cctype>
usingnamespace 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).
You can use string.length() to check the length. And to check for symbols other than 0 and 1 you'll need to iterate the string one character at a time checking for characters you don't allow. if (string[i]!='0' && string[i]!='1')
You cannot limit the number of characters a user inputs. You can either ignore the extra characters or tell the user their input is wrong and ask them to re-enter it.
1 2 3 4 5 6 7
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;
}
If you edit your post and put code tags around your code, it will be more legible.
// InvalidBinDr.cpp
#include<iostream>
#include<string>
#include<cctype>
usingnamespace 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).
Now Can someone help me with my loops/if statement so if user inputs the correct one it says not right cout instead of the same one over and over.
cout << "Please enter foo> " << flush;
while (true)
{
getline( cin, foo );
if (valid( foo )) break;
cout << "Foo must have quux to be valid.\nPlease enter foo> " << flush;
}
cout << "Thank you for entering a valid foo.\n";
This, of course, is an example. The specifics will vary depending on what foo is and how it is validated.
// InvalidBinDr.cpp
#include<iostream>
#include<string>
#include<cctype>
usingnamespace 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;
}
while (true)
{
getline( cin, input );
if (inValid) break;
cout << "Thank you for entering a valid input.\n" << endl;
}
cout << "input must be 0's or 1's to be valid.\nPlease enter input> " << flush;
return 0;
}
/*
Enter a binary number (8 digits or less): 10101010
input must be 0's or 1's to be valid.
Please enter input> Press any key to continue . . .
*/
/*
Enter a binary number (8 digits or less): 12345678
input must be 0's or 1's to be valid.
Please enter input> Press any key to continue . . .
*/
You need to test all of your conditions at line 20. Currently you are only checking the length. I suggest you use string::find_first_not_of() to check against non binary characters.