Thread: [c]Pointers
View Single Post
Old 12-17-2006, 05:25 PM   #1 (permalink)
zero2
Junkie
 
zero2's Avatar
 
[c]Pointers

I kind of hate to ask so many times for help, but I'm really struggling at this.

I need to write a function, that allows a user to enter a string -- a pointer to char, and converts it to binary digit string or another pointer to char.

For instance, a user can type in any of the following, and that would be acceptable values.

a)"10100010001111"
b)" 001011100"
c)"11110010 "

My output is supposed to look like this:

Enter a binary string to continue or q to quit:
123ab46
The given string is not convertible!

Enter a binary string to continue or q to quit:
1010110100
The given string is converted to
1010110100

Enter a binary string to continue or q to quit:
q
Thank you!

So far this is what I got for my code, and after looking at it, I know it's really really wrong. I think where I'm going wrong is that instead of a loop, perhaps a switch statement would be better, but maybe I'm wrong. I also need to use pointers, but since I'm more comfortable with arrays, I want to try it with arrays, and later figure out how to do it with pointers.


Code:
#include <stdio.h>
/* Function Declaration */
int convertToBinaryVersionA(char cAry[]);

int main(void)
{
	int iA;
	char cAry[81];
	
	iA = convertToBinaryVersionA(cAry);
	return 0;
}

/**
* Function Name: convertBinaryVersionA()
*
*/
int convertToBinaryVersionA(char cAry1[])
{
	int i, j;
	int iSize;
	char cTest[81];
	int iVal;	
	
	printf("Enter a binary string to continue or q to quit: ");
	gets(cAry1);
	
	j = 0;
	
	for (i = 0; i < iSize; i++)
		{
			if ( cAry1 [i] == '0' || cAry1 [i] == '1' ) //testing if value is a binary digit
					 {
							cTest [j] = cAry1[i];//storing binary digit into array
							j++;
							iVal = 1;//return 1 if input is a binary #
	
						}
			else
				{    
                             for ( i= 0; i < iSize; i++)
	                            {
		                          printf("%3c", cAry1[i]);
	                            }
                            printf("The given string is not convertible");
					iVal = 0;//return 0 if input is not a binary #
				}
		}
      printf("The given string is converted to: ");	
	for ( j= 0; j < iSize; j++)
	{
		printf("%3c", cTest[j]);//values
	}
	return iVal;
}
zero2 is offline  
 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37