Debugging question

This is a discussion on Debugging question within the C Programming forums, part of the General Programming Boards category; Hi guys, im kinda new here, so if i make a newbi mistake please go easy on me. Well Im ...

  1. #1
    o_0
    o_0 is offline
    Registered User
    Join Date
    Oct 2004
    Posts
    4

    Debugging question

    Hi guys, im kinda new here, so if i make a newbi mistake please go easy on me.

    Well Im trying to make a program that will convert binary to decimal. (I know there are thousands of these post already, but i still couldnt find the answers to my question .


    question # 1:
    How can i pervent people from entering numbers other than 1's and 0's ?

    question #2:
    For some reason when I enter the binary, Im only able to enter 5 digits or less, or else my program wront work.
    For e.g. i enter 11011 :: the output will be 27
    But when i enter 111101 :: the output will be 0


    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int array[1000];
    int getbinary(int binary);
    
    main()
    {
    	int temp;
    	int binary;
    	int power;
    	int decimal;
    	int cell;
    	cell = 0;
    	power = 1;
    	decimal = 0;
    	binary = getbinary(binary);
    
    	do{
    		temp = binary % 2;
    		if (temp==1)
    		{
    			array[cell] = power;
    		}
    		else
    		{
    			array[cell] = 0;
    		}
    		cell++;
    		power = power * 2;
    		binary = binary * .1;
    	} while(binary > 0);
    	for (cell=cell; cell>=0; cell--)
    	{
    		decimal = array[cell] + decimal;
    	}
    	printf("%d decimal\n", decimal);
    	getch();
    }
    
    int getbinary(int binary)
    {
    	printf("Please enter binary: ");
    	scanf("%d", &binary);
    	return binary;
    }
    Thx!!
    Last edited by o_0; 10-10-2004 at 12:15 PM.

  2. #2
    Registered User
    Join Date
    Oct 2004
    Posts
    31
    answ to 1 (i Think. I am a newbie too.. so )

    Code:
    int num;
    scanf("%d", &num);
    while((num != 0) || (num !=1)){
      printf("\n please print  0 or 1 :");
      scanf("%d",&num);
    }
    try this...

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    31
    your binary is my num

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    31
    please use
    Code:
     ....
    thnks

  5. #5
    o_0
    o_0 is offline
    Registered User
    Join Date
    Oct 2004
    Posts
    4
    to swagatob : hehe, where do i put that code?? I try to put it in the getbinary functions, but when I run it, the program will just keep on asking me to enter please print 0 or 1 no matter wut i enter.

  6. #6
    Registered User
    Join Date
    Oct 2004
    Posts
    31
    Code:
    while((num != 0) && (num !=1)){
      printf("\n please print  0 or 1 :");
      scanf("%d",&num);
    }
    as you can see that I am a newbee too

    EDIT: while loop changed.. it possibly should be and not or

  7. #7
    Registered User
    Join Date
    Oct 2004
    Posts
    31
    for 2 maybe use long rather than int... I am not sure though.....

  8. #8
    o_0
    o_0 is offline
    Registered User
    Join Date
    Oct 2004
    Posts
    4
    thx swagatob, i think i almost got it to work

    edit:

    oops, lol i dont know wut happen but it dont work again.
    ill follow ur direction again swagatob, and im not sure is this wut u wanted me to do?
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int array[1000];
    int getbinary(int binary);
    
    main()
    {
    	int temp;
    	int binary;
    	int power;
    	int decimal;
    	int cell;
    	cell = 0;
    	power = 1;
    	decimal = 0;
    	binary = getbinary(binary);
    
    	do{
    		temp = binary % 2;
    		if (temp==1)
    		{
    			array[cell] = power;
    		}
    		else
    		{
    			array[cell] = 0;
    		}
    		cell++;
    		power = power * 2;
    		binary = binary * .1;
    	} while(binary > 0);
    	for (cell=cell; cell>=0; cell--)
    	{
    		decimal = array[cell] + decimal;
    	}
    	printf("%d decimal\n", decimal);
    	getch();
    }
    
    int getbinary(int binary)
    {
    	printf("Please enter binary: ");
    	scanf("%d", &binary);
    	while((binary != 0) && (binary != 1))
    	{
    		printf("Please enter binary 0's or 1's: ");
    		scanf("%d", &binary);
    	}
    	return binary;
    }
    Last edited by o_0; 10-10-2004 at 02:05 PM.

  9. #9
    o_0
    o_0 is offline
    Registered User
    Join Date
    Oct 2004
    Posts
    4
    hi
    any more suggestion?
    ary would be appeciated

  10. #10
    ---
    Join Date
    May 2004
    Posts
    1,379
    suggestion for
    swagatob:

    use your edit button instead of posting new ones

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dev-C++: Problems with Breakpoint Debugging
    By Thileepan_Bala in forum C Programming
    Replies: 1
    Last Post: 01-17-2008, 09:48 AM
  2. Debugging book recommendation
    By dagans in forum Projects and Job Recruitment
    Replies: 1
    Last Post: 09-13-2005, 07:35 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM
  5. Debugging Question
    By Unregistered in forum C++ Programming
    Replies: 0
    Last Post: 12-19-2001, 04:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21