Possible loss of data?

This is a discussion on Possible loss of data? within the C++ Programming forums, part of the General Programming Boards category; I'm new to C++ and was trying to write a program that would ask for a number then return its ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    10

    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:
    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";
    		cin>>x;
    	}
    cout<<"The number you entered was "<<x<<"\n";
    cout<<"Its binary form is:\n";
    for (i=7;i>-1;i--)
    {
    	a=pow(2.0,i);
    	if ((x-a)>-1)
    	{   
    		x-=a;
    	    cout<<"1";
    	}
    	else
    		cout<<"0";
    }
    cout<<"\n\nPress enter to continue";
    fflush (stdin);
    cin.get();
    return 0;
    }

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

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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!
    CornedBee

    "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
    Location
    Singapore
    Posts
    21,310
    The warning probably come from this line:
    Code:
    x-=a;
    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:
    Code:
    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.

    EDIT:
    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>.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129

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, 01:59 AM
  3. Where's the EPIPE signal?
    By marc.andrysco in forum Networking/Device Communication
    Replies: 0
    Last Post: 12-23-2006, 07: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, 08:09 PM

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