469,623 Members | 899 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,623 developers. It's quick & easy.

How to resolve "illegal,left operand has type 'double" error?

error C2296: '%' : illegal,left operand has type 'double'

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6. const int OUNCES_PER_POUND = 16; 
  7. const double POUNDS_PER_KILOGRAM = 2.2;
  8.  
  9. int main()
  10. {
  11.     int Pounds;
  12.     int Ounces;
  13.     double Kilograms;
  14.     double totalPounds;
  15.     double totalOunces;
  16.  
  17.     ifstream fin;
  18.     ofstream fout;
  19.  
  20.     fin.open("inData.txt");
  21.     fout.open("outData.txt");
  22.  
  23.     fin >> Kilograms;
  24.  
  25.     totalPounds = static_cast<double>(Kilograms * POUNDS_PER_KILOGRAM);
  26.     totalOunces = static_cast<double>(Pounds * OUNCES_PER_POUND);
  27.     Pounds = static_cast<int>(totalOunces / OUNCES_PER_POUND + 0.5);
  28.     Ounces = static_cast<int>(totalPounds % OUNCES_PER_POUND);
  29.  
  30.     fout << Pounds << "\nPounds";
  31.     fout << Ounces << "\nOunces";
  32.     fin.close();
  33.     fout.close();
  34.  
  35. return 0;
  36. }
Feb 2 '11 #1
1 11405
tdlr
22
You cannot use % with double. Change totalPounds to an integer type like long.
Feb 2 '11 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Kostatus | last post: by
1 post views Thread by Dom | last post: by
reply views Thread by =?Utf-8?B?UGF1bCBIYWdlcg==?= | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.