Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

Binary Converter in C++ -- doesnt work


ghost's Avatar
0 0

I made a small program that is supposed to convert a string into binary and then post it but it doesnt work and it keeps saying segmentation fault but i dont see where the problem is. Here is my source code: PS: I want to use my own function for converting to binary. thanks :)

#include <stdio.h>
#include <stdlib.h>

char binaryconv(char letter)
{
	char modul[512], next[512];
	int is;
	for(is = 0; next != 1; is++)
	{
		if(is <= 0)
		{		
			modul[is] = (int)letter % 2;
			next[is] = (int)letter / 2;
		}
		else
		{
			modul[is] = next[is-1] % 2;
			next[is] = next[is-1] / 2;
		}
	}

	return modul;
}

int main(int argc, char *argv[])
{
	char redo, string[512], binary[512];
	int len, i;
	BEGIN:
	printf("Please enter a phrase that you would like to convert to binary: \n");
	scanf("%s", string);
	len = strlen(string);
	for(i=0; i<len;i++)
	{
		binary[i] = binaryconv(string[i]);
	}
	printf("The string is: %s\n\n", binary);
	scanf("would you like to go again(y/n): %c", redo);

	switch((int)redo)
	{	
		case (int)'y':
			goto BEGIN;
			break;
		case (int)'n':
			break;
	}
	return 0;
}```

any help would be appreciated.