Counting 0's and 1's

I am trying to make a program where the user inputs binary, and the programs outputs the total number of leading 0's before each 1's.
For Example:
Input:00010101101
Output:3, 1, 1, 0, 1 If you look at number 3, the 3 means that there are 3 0's before there was a 1.

The problem is that I can't find a way for the code to read the 0's and 1's.
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
  #include <iostream>
using namespace std;

int main() {
	int length_cases,total=0;
	char length[1000];
                cout<<"Input: "
		cin>>length;//Gets the user's inputs, it is suppose to be in 1's and 0's
		for(length_cases=0;length[length_cases]!='\0';length_cases++)
		{
			if(length[length_cases]==0)
			{
				total+=1;//if the array in the char string is 0,
// it adds 1 to the total.
			}
			if(length[length_cases]==1)
			{
				cout<<total<<", ";//if the array is 1, it gets the 
//total number of 0's before it and outputs the total.
				total=0;
			}
		}
	}
	return 0;
}
Last edited on
As I have understood you are entering a string consisting of characters '0' and '1'

So you should compare elements of the array with these symbols that is for example

if(length[length_cases]== '0' )
I would suggest the same as vlad but also why the char array and not a string? because you have about 990 or so useless parts in that array unless someone is inputting that many values. you can also use getline( cin , STRING ); instead of cin if you do that method.
It worked perfectly! Thank you very much.
Topic archived. No new replies allowed.