I have a program assignment in my C and Unix class that allows the user to enter two numbers in binary seperated by (*,+,-,or /). It then has to produce an answer and loop back to the beginning. It also has to have an option to exit. Can anyone help?

Sure, when you show your code and tell about your problems. Do not forget to push the CODE before pasting your code. Happy coding!

Yes we are always here to help you. you have to mention properly what is your problem exactly. I mean you have to submit your wrong code then we will help you in you way.

I have a program assignment in my C and Unix class that allows the user to enter two numbers in binary seperated by (*,+,-,or /). It then has to produce an answer and loop back to the beginning. It also has to have an option to exit. Can anyone help?

I only have the basic start of the program, because i do not know how to compute the input of the users. but here you go.

//////Kyle Haas///////
/////Program 4//////

//this program allows the user to do arithmatic in binary

#include <stdio.h>
int main();
unsigned choice, n;

printf("binary number(+,-,*,/)binary number\n ")
printf("type 'exit' to quit");
scanf("%d" &n);

if
(choice = exit);
return 0

else

this program allows the user to do arithmatic in binary

Google the theory of binary arithmetic and use the algorithm also search for bitwise operator.
go for http://www.cprogramming.com/

Start by adding print statements to be sure you are reading the input properly.

Program one step at a time.
First input. Make sure it works.
Next internal representation of the values -- print them. Make sure they are correct.
Next...

To begin with, you seem to be having trouble understanding the structure of functions such as main() . In the function implementation, you do not put a semi-colon at the end of the function header - you only do that with function prototypes, which you probably haven't covered yet. Also, you need to have braces around the body of the function, to mark where it begins and ends. The same applies to conditional statements such as if() - no semi-colon after the conditional, and the body of the conditional in braces. Finally, it is conventional to indent the code in one of several styles; I've demonstrated this below with a simple four spaces indent and flat braces.

//////Kyle Haas///////
/////Program 4//////

//this program allows the user to do arithmetic in binary

#include <stdio.h>
int main()
{
    unsigned choice, n;

    printf("binary number(+,-,*,/)binary number\n ")
    printf("type 'exit' to quit");
    scanf("%d" &n);

    if (choice = exit)
    {
        return 0;
    }
    else
       // what goes here?

}

You also can not assign value of exit at line 16 of edited code of previous poster to value of choice as it is undeclared and uninitialized. It should be compatible integer type variable for the assignment to succeed and to have nonzero value for if test to succeed.

I am still a beginner in the c language, so i am still working on the correct format of my programs. I still have to work out the format of this program, but i am having trouble finding how to code for the binary arithmetic.

I am still a beginner in the c language, so i am still working on the correct format of my programs.

Fair enough. We were all beginners, once.

I still have to work out the format of this program, but i am having trouble finding how to code for the binary arithmetic.

Well, here's the first big hint: don't read the data in as a number. That won't work for this, because, first off, the scanf() function has no way of reading in binary numbers; second, you need to be able to read the operator and the second binary value in, which means reading the whole thing in as a string and parsing it (breaking it into the individual values); and third, you need to be able to know when the user enters the word 'exit'.

You can not yet, because you need tested base forthe function. You must build from beginning. Of course you could use hard wired input for testing the logic for calculations first like I do, others do input routines first only printing it back to confirm you got it in the program correctly. But you should not never add more than one action and then prove that it works correctly.

To stress one more time. If you add statement to your code means thet your other code is proved bug free in basic level (allways some bugs go through the first test cases but whe you notice those you must then go several steps behind in your design process).

You may think it old fashioned way but most times it is usefull to take paper and pencil and draw few schetches and do it first yourself in paper, then explain to computer how to do it.

I see so will using the sscanf() work to read the binary inputs? And if so when using the sscanf function to read the inputed string do i have to put a %d for every 1 or 0 in the binary number?

#include <stdio.h>
int main()
unsigned choice, n;
	

	do
	{
	printf("Enter a number?\n");
	printf("(Yes(1) or No(2))\n");
	scanf("%u" &choice);
	}while (choice <1 || choice > 2);

switch(choice)

{
	case 1:
		printf("binary number(+,-,*,/)binary number\n ")
		sscanf("%d%d%d%d%d%d%d%d" &n); 
//do i have to put all of these %d's to read every place in the string?

Oh, my. No, I think that you've misunderstood me, as well as misunderstanding how sscanf() works. What I was recommending was that you read the input into a character string, preferably using fgets() , then interpreting the binary values and the operator.

How about we start with a simpler sub-task: reading in the binary values and printing them back out. This isn't as simple as it sounds, as you'll have to do some manual manipulation of the input if we're going to be using fgets() . Still, this should give you some idea of a good direction to go:

#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define BUFFER_SIZE 1023

int main()
{
    char choice = 'Y';
    unsigned length;
    char buffer[BUFFER_SIZE + 1];

    while (choice == 'Y')
    {
        printf("Enter a binary number: ");
        fgets(buffer, BUFFER_SIZE, stdin);

        buffer[BUFFER_SIZE] = '\0';        /* make sure that the string gets delimited */
        length = strlen(buffer);

        if (length < BUFFER_SIZE)
        {
            buffer[length] = '\0';        /* get rid of the trailing newline */
        }


        printf("You entered: %s\n", buffer);

        do
        {
            printf("Continue (Y or N) ");
            choice = toupper(fgetc(stdin));
            fgets(buffer, BUFFER_SIZE, stdin);   /* clear the system buffer */
        }
        while (choice != 'Y' && choice != 'N');

    }

    return 0;
}

Try compiling and running this and see if you understand how it works.

I had never used the fgets() function before. Your program makes a little sense to me, but from here do i get both numbers with fgets() and then do the arithmetic on them? and will fgets() store them so that i can get both numbers?

No, at least not immediately; what fgets() does is it reads a character string into a buffer, which you then would have to interpret as the binary values. You have to write a function which will convert the sub-strings into the actual integer values.

I'm surprised that you are being given such a difficult task at such an early stage; while this isn't much work for anyone who knows C, it's not the easiest thing in the world for someone just starting in the language.

Anyway, this DevShed thread discusses a function for converting a binary string representation into an actual binary value.

....
        buffer[BUFFER_SIZE] = '\0';        /* make sure that the string gets delimited */
...

Useless. By definition fgets() adds the '\0' for you. And it will go into buffer[BUFFER_SIZE-1] if you fill up the entire buffer.

Useless. By definition fgets() adds the '\0' for you. And it will go into buffer[BUFFER_SIZE-1] if you fill up the entire buffer.

Eh? looks up reference on fgets() Why, so it does... thank you for correcting me on this part. I think I may have been confusing it with some of the delimited string functions such as strncpy() (which does not automatically delimit the string). Damn, I've been making this mistake consistently, too... well, now I know.

I apologize to anyone misled by this error. Again, thank you, WaltP, for correcting me on this.

I just noticed another error in that code, I'm afraid: When removing the newline, it should be buffer[length - 1] , not buffer[length] . Rather careless of me, actually.

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.