Thread: Possible loss of data?

  1. #1
    Registered User
    Join Date
    Feb 2008

    Possible loss of data?

    I'm new to C++ and was trying to write a program that would ask for a number then return its binary form. However I couldn't get it to compile while trying to use integer powers, and had to use a double power function...however got a warning saying that there could be possible loss of data. After running the program a few times it seems to be fine, but how can I avoid the warning? Here is my code:
    #include "base10tobase2.h"
    using namespace std;
    int main()
    	int i,x=-1;
    	double a;
    	while ((x<0)||(x>255))
    		cout<<"Please enter a number less than 256:\n";
    cout<<"The number you entered was "<<x<<"\n";
    cout<<"Its binary form is:\n";
    for (i=7;i>-1;i--)
    	if ((x-a)>-1)
    cout<<"\n\nPress enter to continue";
    fflush (stdin);
    return 0;

    Also, as I am new to C++, I would appreciate any tips/advice you have?

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Basically, it's telling you that by assigning a double to an int, you're losing precision.

    Using pow() for this is wrong anyway. To get powers of two, you use bit shifting.
    All the buzzt!

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    The warning probably come from this line:
    x is an int, but a is a double, which can store a wider range of values than an int. If you really want this conversion to happen, you should document it with a type cast:
    x -= static_cast<int>(a);
    A few more things to note:
    1. You are using cout, cin and pow, so you should #include <iostream> and <cmath>. They might have been included in "base10tobase2.h", but it is better to include them, just in case.

    2. You should indent your code more consistently.

    3. fflush(stdin) is undefined. Use cin.ignore() instead.

    Ah yes, but since you can use CornedBee's suggestion, it would be better not to use pow and the type cast in the first place, upon which you also would not need to #include <cmath>.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Feb 2008
    Thankyou, I will make those changes (and look up bit shifting!). Thanks for the quick response!

  5. #5

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. conversion from double to float - loss of data
    By diyteam in forum C++ Programming
    Replies: 20
    Last Post: 03-04-2008, 02:59 AM
  3. Where's the EPIPE signal?
    By marc.andrysco in forum Networking/Device Communication
    Replies: 0
    Last Post: 12-23-2006, 08:04 PM
  4. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  5. Binary Tree, couple questions
    By scoobasean in forum C Programming
    Replies: 3
    Last Post: 03-12-2005, 09:09 PM