looking for help

Problem: Write a program that uses a recursive function to return the number of occurrences of the digit 1 in a given binary string b. For
example, b = 100100010111 contains 6 occurrences of the digit 1. The result, the number of occurrences of the digit 1 in b, is output
to the screen, as part of the main function. Your submission should include a screenshot of the execution of the program using each
of the binary strings 001000001111, 000000000000 and 101001010000.

and here is code i had so far:
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int countOccurrences(string b, int index, int count);
int main()
{
int count = 0;
int index = 0;
string b = " ";
cout << "Enter a string: " << endl;

getline(cin, b);
count = countOccurrences(b, index, count);
cout << count << endl;
return 0;
}

int countOccurrences(string b, int index, int count)
{
if (index < b.length())
return count;
else if (b[index] == '1'){
count = count + 1;
return countOccurrences(b, (index + 1), count);
}
}

what is wrong with my code? any helps will be appreciate!
closed account (48bpfSEw)
if (index >= b.length())
Topic archived. No new replies allowed.