I need help with pointers in my program

This is a discussion on I need help with pointers in my program within the C++ Programming forums, part of the General Programming Boards category; Ok, so i'm working on a program that converts decimal numbers to binary numbers. The binary number is outputted as ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    9

    I need help with pointers in my program

    Ok, so i'm working on a program that converts decimal numbers to binary numbers. The binary number is outputted as a string. But the compiler (Turbo C 3.0) won't let me return a string from a function to the main part of the program.

    # include <iostream.h>

    //validinput function checks to see if the input is greater than 256
    //or if it is less than 0
    int validinput(int num)
    {
    if (num > 256 || num < 0)
    {
    return 0;
    }
    else
    {
    return 1;
    }
    }

    //dectobin function converts the decimal number to a binary number
    //written with a string
    char dectobin(int num)
    {

    int base2[] = {128, 64, 32, 16, 8, 4, 2, 1};
    char binarystring[9];
    char *pbin = binarystring;

    for(int x = 0; x > 8; x++)
    {
    if(num > base2[x])
    {
    pbin[x] = '1';
    num =- base2[x];
    }
    else
    {
    pbin[x] = '0';
    }
    }
    return binarystring;
    }

    //main program
    int main()
    {

    char binary[9];
    int num;

    while(!validinput(num))
    {
    cout<<"Enter an integer between 0 and 255: ";
    cin>> num;
    }

    binary = dectobin(num);
    cout<<"The binary number is: "<< binary;

    return 0;
    }

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    9
    that was awfully hard to read, not sure why it saved like that, let me attach that
    Attached Files Attached Files

  3. #3
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    I have changed your code so that it dosent return binarystring but passes binary as an argument instead, you also had an error in your for loop, you used > which will be false the first time you loop, also you forgot to add a '\0' character to the end of the string. compare the new code to the original code and spot the difference.
    I ran the program and it works but i don't think the binary values are correct, I didnt check though.
    Attached Files Attached Files
    Last edited by C_Coder; 12-01-2001 at 04:46 PM.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    9
    thanks for help! my main original code was the same as your revised on but without the null character, and i wasn't sure about that loop, it ran on my compiler, so i just went with it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need some pointers on program
    By noob2c in forum C++ Programming
    Replies: 7
    Last Post: 12-14-2003, 05:16 AM
  2. Date program starts DOS's date
    By jrahhali in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2003, 05:23 PM
  3. Replies: 2
    Last Post: 03-13-2003, 09:40 AM
  4. Help In Visualising Pointers ( C Program )
    By Evilelmo in forum C Programming
    Replies: 5
    Last Post: 01-25-2003, 01:48 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

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