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.
#include <iostream>
usingnamespace 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;
}
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.