how to limit text 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// 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).





Last edited on
help anyone?
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.
ok so I have

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// 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).


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.
I will typically have something like
1
2
3
4
5
6
7
8
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.

Hope this helps.
hmm still not fixed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// 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;
}

   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 . . .
*/
my suggestion is this the code will be cleaner this way:

do {
cin >> a;

if(a.size() > 8)
cerr << "You entered more than 8 digits. Please re-enter your number." << endl;

} while(a.size() > 8);
cout << "Thank you for entering a valid input.\n" << endl;

so it will solve all the code cause it's kinda messy your way(23 lines vs. 5)
Get rid of lines 24 through 30.

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.

20 if ((input.size() <= 8) && (input.find_first_not_of( "01" ) == string::npos)) break;

Remember, get the input, check it, and then continue.
You cannot get the same input multiple times...

Hope this helps.

@hoshiro
Please pay attention to the previous responses in this thread...
Last edited on
Topic archived. No new replies allowed.