Write a computer program using the C Language which reads any base-10 number from the standard input (keyboard) and writes its binary and hexadecimal representation to the standard output (monitor).

this is what I have and I am super confused and need help please. and I have to create this in Visual Studio so please help me get through it please.

#include "stdafx.h"
#include "iostream"
#include "stdio.h"
#define n 10;
int size = 10;
int foo (int, double);

using namespace std;
int main (int argc, char* argv [])
{
    int x = 10; // this defines variable x of type int and assigns 10 as the initial value.
    printf ("The value is % d and the address is % x\n", x, & x);


    cout << "Enter an integer:";
    int n;
    cin`Inline Code Example Here`

Recommended Answers

All 13 Replies

lines 1-3: The names of standard C or C++ header files go in angle brackeks, not quotes. such as #include <stdio.h>

line 17: cin is a c++ function, not C. For C programs you have to use scanf()

Other than that, what are you confused about?

Minor point, but the correct printf() format string specifier for a pointer value is %p, which will show it in the correct format for a pointer on the given platform. While %x will show that value as a hex number, this isn't necessarily the correct way to specify a pointer value.

printf ("The value is %d and the address is %p\n", x, & x);

Also, as me.stober points out, cout and cin are C++ objects, and cannot be used in C. Similarly, the <iostream> header and using namespace directive are also specific to C++.

You have declared a function named foo(), but it isn't in the code given. I know you've been having some trouble working with functions, so let me ask: what is foo() meant to be for? Or was that an example the instructor used that you copied verbatim? The identifier foo isn't usually used in actual coding; rather, it is a meta-syntactic variable, a word which is used to stand in for some other, unspecified word. It is mostly seen when giving general examples of the structure of things like functions, without giving a real function name.

printf ("The value is %d and the address is %p\n", x, & x);

Another minor point, but to be strictly correct you must to cast the argument for %p to void* because that's the type printf() expects:

printf("The value is %d and the address is %p\n", x, (void*)&x);

Ah, thank you for that correction.

the thing I am confused is already been corrected but then I am stuck as I have no clue how to continue coding?

the thing I am confused is already been corrected but then I am stuck as I have no clue how to continue coding?

And this is supposed to give us the information we need to help you? Think about actually asking a question we can answer.

is it that you dont know how to change from decimal to binary or hexadecimal? or is it a coding problem

I dont know how to code the problem :'(

I did this wonder if its correct or not please correct me where I am wrong

// Convert a binary integer to a hexadecimal string
// added a test printf() you can remove later

# include <stdio.h>

void binary2hexadecimal (long binary, char * hexadecimal);

int main ()
{
    long binary;
    char hexadecimal [0x64];

    printf("\n\n Enter an integer value : ");
    scanf("%x",&binary);
    binary2hexadecimal(binary,hexadecimal);
    printf("\n the hexadecimal value of %x is &s \n",binary, hexadecimal);

    getchar(); // trap enter
    getchar(); // wait
    return 0;
}
//
//accepts a binary integer and returns hexadecimal coded string
//
void binary2hexadecimal(long binary, char *hexadecimal)
{
    int k = 0, n=0;
    int neg_flag = 0;
    int remain;
    int old_binary; // for test
    char temp [00110111];

    // take care of negative input
    if (binary < 0)
    {
        binary = - binary;
        neg_flag = 1;
    }
    do
    {
        old_binary = binary; // for test
        remain = binary %2;
        //whittle down the binary number
        binary = binary /2;
        //this is a test to show the action
        printf("%d/2 = %d remainder = %d \n", old_binary, binary, remain);
        // converts digit 0 or 1 to character '0' or '1'
        temp [k++] = remain + '0';
    } while (binary > 0);

        if(neg_flag)
            temp [k++] = '-';     // add - sign
        else

            temp [k++] = '  ';    // space

            //reverse the spelling
            while (k>=0)
                hexadecimal [n++] = temp [--k];

            hexadecimal[n-1] = 0; //end with NULL
        }

Is there anyone who is going to help me? or is this program created correctly?

Does it do what it's supposed to?

If so, it's probably good enough.
If not, tell us what the problems are. We can't help if we don't know what the problems are.

I posted the question together with the answer!!! do you yet not get the question?

Obviously not. Your last two posts were cryptic. You said the code is the answer -- meaning this is the correct solution. Then you ask if it's the correct solution. You already know the answer. It either works correctly or it doesn't. You never said one way or the other. What can we do if all you do is post riddles?

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.