C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 6th, 2003, 12:02 AM
Jollygreen Jollygreen is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 1 Jollygreen User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Binary to Decimal Code, needing help.

After a rather long break from programming in C I recently started again. After brusing up on it I decided to write this code off the top of my head.

I'm not quite sure exactly what's wrong with it, I've spent a few hours just staring at it. Theoretically, when run it prompts you to enter the binary to be converted. (currently only 4 digits for simplicity's sake.) and then return a value after the calculations have been made. In reality, At a DOS prompt it asks for the Binary and after that has been entered it encounters a fatal error.

I've broken up the code into sevral functions after writing it in one big Chunk. If you can debug this simple program, please please e-mail me with the code and a short explanation of what was wrong. My E-mail is thewiccakid@hotmail.com , thank you.
-JG

PHP Code:
#include <stdio.h>
#include <string.h>
int buns[4];
int weiner[4];
int a 0;
int yz;

void calculate_1(){
for (
04y++){
    
weiner[y] = buns[y]*2^(y);
}}

int calculate_2(){
for (
04y++) {
    
weiner[y] = z;
   
+= z;
}
return 
0;
}

int results(){
printf("The decimal equivalent of %d is:\n"buns);
printf("%d"a);
return 
0;
}

main ()
{
printf("Please input the Binary to be converted to decimal:\n");
scanf("%d %d %d %d"buns[0], buns[1], buns[2], buns[3] );
calculate_1();
calculate_2();
results();


Reply With Quote
  #2  
Old July 6th, 2003, 12:58 AM
helado helado is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 56 helado User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
First off, move your variables into main and then pass and return your arrays or data. Also, put a type on main. void main or int main with a return 0 at the end of main will suffice.

Second, your problem I believe lies in calculate_2:

weiner[y] = z;

should be switched to read.

z = weiner[y];

but why don't you just do

a += weiner[y];

Third, the ^ isn't doing what you want it to do. It's actually a bitwise xor operation.

Here's some code I dug up that does it for any length of number entered. Only problem is that you still reach the limit of an int (32bit or so) on the input. Since you're brushing up on C again it's always good to look at different methods, etc.

Code:
#include <stdio.h>
#include <string.h>

int main()
{
	int count, total, i, j, tmp;
	char buffer[256];

	total = 0;

	printf("Please enter a number: ");
	scanf("%s", buffer);
	count = strlen(buffer);

	for (i = 0; i <= count; i++)
	{
		if (buffer[count-i] == '1')
		{
			tmp = 1;
			for (j = 1; j < i; j++)
				tmp *= 2;
			total += tmp;
		}
	}

	printf("\nNumber is: %d\n", total);
	
	return 0;
}

Last edited by helado : July 6th, 2003 at 01:02 AM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Binary to Decimal Code, needing help.


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT