hallo i wrote a program with 3 different functions.
something wrong with her and i cant see it.

#include<stdio.h>
int devide9(int num);
int decimal_binar(int num);
int binar_decimal(int num);
int main()
{
int menu, num9, numd1, numb1, numd2, numb2, numfinal1;

	printf("\nHallo these are your options:\n");
	printf("1. Check if your number can be devide by 9 without residue.\n");
	printf("2. Decimal - binar converter.\n");
	printf("3. Binar - decimal converter.\n");
	printf("4. Exit.\n");
	scanf("%i", &menu);
	
	switch (menu)
	{
		case 1:
			printf("please enter a number:\n");
			scanf("%i", &num9);
			numfinal1 = devide9(num9);
			if (numfinal1 == 9)
				printf("the number can be devided by 9.\n");
			else
				printf("the number can not be devided by 9.\n");
			main();
		case 2:
			printf("please enter a number:\n");
			scanf("%i", &numd1);
			numb1 = decimal_binar (numd1);
			if (numd1<0)
				printf("%i (base 10) => -%i (base 2)\n", &numd1, &numb1);
			else
				printf("%i (base 10) => %i (base 2)\n", &numd1, &numb1);
			main();
		case 3:
			printf("please enter a number:\n");
			scanf("%i", &numb2);
			numd1 = binar_decimal(numb2);
			if (numd2<0)
				printf("%i (base 2) => -%i (base 10)\n", &numb2, &numd2);
			else
				printf("%i (base 2) => %i (base 10)\n", &numb2, &numd2);
			main();
		case 4:
			return(0);
			break;
		default: main(); break;

	}
	return(0);
}

int devide9 (int num)
{ 
	int numT, num1;
	if (num<0)
		num = num*-1;
	numT = 0;
	while (num > 0 );
	{
		num1 = num%10;
		numT = num1 + numT;
		num = num/10;
	}
	if (numT>9)
		numT = devide9 (numT);
	return (numT);
}

int decimal_binar (int num)
{	
	int counter, multi, i, numT, num1, numd1;
	if (num<=1023 && num >= -1023)
	{
	if (num<0)
		num = num*-1;
	counter = 0;
		numT = 0;
		while (num > 0 );
		{
			num1 = num % 2;
			num = num / 2;
			if (counter == 0)
				numT = num1;
			else
			{
				multi = 1;
				for (i=1; i<=counter; i++)
				{
					multi = 10 * multi;
				}
			}
			numT = multi * num1 + numT;
			counter = counter +1;
		}
	}
	else
	{
			printf("please enter a new number:\n");
			scanf("%i", &numd1);
			numT = decimal_binar(numd1);
	}
	return (numT);
}


int binar_decimal (int num)
{
	int num1, counter, hez1, hezT, i,numT, numb1;
	counter = 0;
	if (num >= -1111111111 && num <= 1111111111)
	{
		if (num < 0)
			num = num*-1;
		counter = 0;
		numT = 0;
		while (num > 0 );
		{
			num1 = num % 10;
			hez1 = hezT = 1;
			for (i=1; i<=counter; i++) 
			{
				hez1 = hezT;
				hezT = hez1*2;
			}
		numT = num1*hezT + numT;
		counter = counter + 1;
		num = num / 10;
		}
	}
	else
	{
		printf("please enter a new number:\n");
		scanf("%i", &numb1);
		numT = binar_decimal (numb1);
	}
	return (numT);
}

Recommended Answers

All 5 Replies

Use code tags and indent your code properly.

> POLINK: fatal error: Access is denied.
There's a good chance that the previous attempt at this code is still running, and that you can't overwrite a running programs' executable.

Oh, and don't call main() recursively, use a loop.

main() function cannot be called again and again.
Instead you can use another function in place of it or use

goto label;

main() function cannot be called again and again.
Instead you can use another function in place of it or use

goto label;

Use of goto is frowned upon. The only place where it's usage is justified is when you're in a nest of loops and wanna come out of it with the least amount of hassle. As Salem has already suggested, loop is the best way to go about this particular problem.

Use of goto is frowned upon. The only place where it's usage is justified is when you're in a nest of loops and wanna come out of it with the least amount of hassle. As Salem has already suggested, loop is the best way to go about this particular problem.

As i already stated above him to use the another function
goto() statements is the last option to use in the program when nothing works.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.