in trying to create a program that will only accept binary digits

I'm trying to know how can i validate an input if it's valid for binary digit or not.

thanks in advance for helping.
You can get input as a string, and look if it has more character than 1 and 0
can you give me a sample syntax for that just the initialization part. thnx
Something like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

int main() {
	std::string s;
	std::cout << "Enter binary digit: ";
	std::cin >> s;

	for (unsigned int i = 0; i < s.length(); i++) {
		if (!(s[i] == '0' or s[i] == '1')) {
			std::cout << "It is not binary digit: " << s[i] << std::endl;
		}
	}

	return 0;
}
Are you talking about a char calculation or an int calculation? Everything has a binary equivalent. But not every binary sequence has a char or int equivalent.
I think he wants for user to input "1001010" and see if it is binary digit or not. That user could not enter 1101210 or 124fgf.
shinigami is right...that's what i want to know...
this is my code for now... and your syntax shinigami is totally conflicting with other functions.
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
if(mode==1)
        {
            system("cls");
            cout<<"-->>You are in Binary Mode!\n\n\n";
            do
            {
                cout<<"Enter a Binary Digits: ";
                cin>>binary;
         
            }while(array[i]<0&&array[i]>1);
            do
            {
                cout<<"Choose Conversion:";
                cout<<"\n\n[1]Decimal\n[2]Octal Digit";
                cout<<"\n[3]Hexadecimal\n[4]All";
                cout<<"\nChoice: ";
                cin>>choice;
                if(!cin||(!(choice==1||choice==2||choice==3||choice==4)))
                {
                    cout<<"Invalid Choice! Try Again!\n";
                    cin.clear();
                    cin.ignore();
                    Sleep(1000);
                }
            }while(!cin||(!(choice==1||choice==2||choice==3||choice==4)));
            //start of binary to decimal
            if(choice==1)
            {
                cout<<"\n\n"<<binary<<" (in Binary) = ";
                do
                {
                    array[i++]=binary%10;
                    binary=binary/10;
                    ctr++;
                }while(binary>0);
                pro=1;
                for(i=0;i<ctr;i++)
                {
                    array2[i]=array[i];
                    array[i]=pro;
                    pro=pro*2;
                    if(array2[i]==1)
                    sum=sum+array[i];
                }
                cout<<sum<<" (in Decimal)";
            }
It's not obvious what the variable names represent, since there are no variable definitions here.

But just looking at the beginning of the code, there's this:

1
2
3
4
5
    do
    {
        cout<<"Enter a Binary Digits: ";
        cin>>binary;
    } while (array[i]<0 && array[i]>1);

I've no idea what array and i represent. But since none of them are changed inside the loop, it doesn't make sense to use them as the condition.

More importantly, regardless of value, array[i] cannot simultaneously be negative and a positive number bigger than 1. Hence the while condition is always false.

That's as far as I got in looking at the code, it makes no particular sense up to that point, I can't say whether the rest is ok or not (though I could guess).
that's why i'm asking because i can't figure out on what to put in my condition like i said i want to validate the input
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
do
            {
                cout<<"Enter a Binary Digits: ";
                cin>>binary;
            /*    do
                {
                    array[i++]=binary%10;
                    binary=binary/10;
                    ctr++;
                }while(binary>0);
                for(i=0;i<ctr;i++)
                {
                    if(array[i]<0&&array[i]>1)
                    {
                        cout<<"Invalid Input!\n";
                        cin.clear();
                        cin.ignore();
                    }
                }*/
            }while(array[i]<0&&array[i]>1);

this is the validation code that i come up with.
and your syntax shinigami is totally conflicting with other functions.

Yes it is. But you asked
I'm trying to know how can i validate an input if it's valid for binary digit or not.

Not to make a code you can use in your program. And my code validates it.

But my code use std::string and your use int (I guess).
And you don't use any functions. But it would be ease if you make your code into several small functions.
if you use int as binary digit, you can get only number as big as int. In my system it would be number 1111111111. Can get any larger number. But if you take a string, you can get binary off any size. But you won't be able to use it in int calculations like you do in your code.
if you take a string, you can get binary off any size. But you won't be able to use it in int calculations

It's true you couldn't use the string directly in calculations.
But its simple enough to convert the binary string to a real integer, either by using a built-in function such as strtol(), or by writing a short piece of code to do the same job.

Although you could put forward arguments in favour of either approach, personally I'd take the string as my first choice.

http://www.cplusplus.com/reference/clibrary/cstdlib/strtol/
Last edited on
I made if free time

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <iostream>
#include <string>
#include <sstream>

std::string get_binary();
std::string get_base16();
bool is_binary(std::string);
int to_decimal(std::string, int);
int string_to_int(std::string);
int base16_number(std::string);

int main() {
	std::string s_number = "";
	int i_number = 0;
	int base_from = 2;
	int base_to = 10;

	std::cout << "Choose base of number you want to enter: ";
	std::cin >> base_from;

	if (base_from == 2) {
		s_number = get_binary();
	}
	else if (base_from == 16) {
		s_number = get_base16();
	}

	std::cout << "Choose base of number to convert to: ";
	std::cin >> base_to;

	if (base_to == 10) {
		i_number = to_decimal(s_number, base_from);
	}

	std::cout << "Base" << base_from << " number " << s_number << " = " << i_number << " base" << base_to << " number.\n";
	return 0;
}

std::string get_binary() {
	std::string binary = "";
	while(true) {
		std::cout << "Enter binary digit: ";
		std::cin >> binary;
		if (is_binary(binary)) {
			return binary;
		}
		else {
			std::cout << "Number " << binary << " is not binary number.\n";
		}
	}
}

std::string get_base16() {
	std::string s_number = "";
	while(true) {
		std::cout << "Enter base16 number: ";
		std::cin >> s_number;
		if (true) {
			return s_number;
		}
		else {
			std::cout << "Number " << s_number << " is not base16 number.\n";
		}
	}
}

bool is_binary(std::string binary) {
	for (unsigned int i = 0; i < binary.length(); i++) {
		if (!(binary[i] == '0' or binary[i] == '1')) {
			return false;
		}
	}
	return true;
}

int to_decimal(std::string number, int base) {
	int last_number = 0;
	std::string s = "";
	std::string all_numbers = "0123456789";
	for (unsigned int i = 0; i < number.length(); i++) {
		s = number[i];
		if (all_numbers.find(s) != std::string::npos) {
			last_number = last_number * base + string_to_int(s);
		}
		else {
			if (base == 16) {
				last_number = last_number * base + base16_number(s);
			}
		}
	}
	return last_number;
}

int string_to_int(std::string number) {
	std::stringstream ss(number);
	int i;
	ss >> i;
	return i;
}

int base16_number(std::string number) {
	if (number == "A") {
		return 10;
	}
	else if (number == "B") {
		return 11;
	}
	else if (number == "C") {
		return 12;
	}
	else if (number == "D") {
		return 13;
	}
	else if (number == "E") {
		return 14;
	}
	else if (number == "F") {
		return 15;
	}
	else {
		return 0;
	}
}
Topic archived. No new replies allowed.