By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,482 Members | 3,157 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,482 IT Pros & Developers. It's quick & easy.

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

P: 2
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
Share this Question
Share on Google+
1 Reply


P: 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.