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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Terry Archer
    New Member
    • Feb 2011
    • 2

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

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

    Code:
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    using namespace std;
    
    const int OUNCES_PER_POUND = 16; 
    const double POUNDS_PER_KILOGRAM = 2.2;
    
    int main()
    {
        int Pounds;
    	int Ounces;
    	double Kilograms;
        double totalPounds;
        double totalOunces;
    
    	ifstream fin;
    	ofstream fout;
    
    	fin.open("inData.txt");
        fout.open("outData.txt");
    
    	fin >> Kilograms;
    
    	totalPounds = static_cast<double>(Kilograms * POUNDS_PER_KILOGRAM);
        totalOunces = static_cast<double>(Pounds * OUNCES_PER_POUND);
        Pounds = static_cast<int>(totalOunces / OUNCES_PER_POUND + 0.5);
    	Ounces = static_cast<int>(totalPounds % OUNCES_PER_POUND);
    		
    	fout << Pounds << "\nPounds";
    	fout << Ounces << "\nOunces";
    	fin.close();
    	fout.close();
    
    return 0;
    }
    Last edited by Niheel; Feb 2 '11, 09:17 PM. Reason: code tags
  • tdlr
    New Member
    • Jan 2011
    • 22

    #2
    You cannot use % with double. Change totalPounds to an integer type like long.

    Comment

    Working...