Reputation: 89
I`m trying to divide any number in .5, if dividing the number in .5 and my remain is 0 the number will raises up to the next. But if not, it will down to the next.
When I try to do that I get a issue in the line 39. Somebody can help me.
Thanks
//
// main.cpp
// Promedio
//
// Created by Oscar Espinosa on 3/27/15.
// Copyright (c) 2015 IPN ESIME Ticoman. All rights reserved.
//
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int ed, cv, qa, em, h, poo;
float prom, red;
string nom, ape;
cout << " Introduce tus datos" << endl
<<"Nombre: ";
getline(cin, nom);
cout << "Apellidos: ";
getline(cin, ape);
cout << "Introduce las siguientes calificaciones" << endl
<< "Ecuaciones Diferenciales: ";
cin >> ed ;
cout << "Calculo vectorial: ";
cin >> cv ;
cout << "Quimica apilcada: ";
cin >> qa ;
cout << "Electricidad y magnetismo: ";
cin >> em;
cout << "Humanidades II: ";
cin >> h;
cout << "Programacion orientada a objetos: ";
cin >> poo ;
prom = (ed+cv+qa+em+h+poo)/6.00;
if (prom%.5 == 0) // Semantic issue invalid operands to binary expression ('double' and 'double')
{
ceil(prom);
red = ceil(prom);
}
else
{
floor(prom);
red = floor(prom);
}
cout << nom << " " << ape << " tu promedio es " << prom << " y se redondea a " << red;
return 0;
}
Upvotes: 0
Views: 615
Reputation: 181
Modulo (%) can only be used with integer values. You can use fmod but if you meant to work on integer values, perhaps such a trick can help:
if (10*static_cast< int >(prom)%5 == 0)
Upvotes: 1
Reputation: 403
@Oscar Espinosa
you cannot use %(modulus) operator with double values
So its showing the error invalid operands in expression..
try using the fmod(x,y)
function..it will work
Upvotes: 4
Reputation: 56567
The issue is pretty clear: you don't define var
before the if(var==0)
. In C++, you need to define the variable before its first use. Pay attention to what the compiler is telling you. In case of g++:
error: 'var' was not declared in this scope
which I think it's pretty explicit!
PS: don't modify the code after an answer, as you end up confusing everyone. In the actual code, you are passing 0.5
as the modulus operator argument. That operator takes an int
. You need other way of testing whether a float
is a multiple of 0.5
. In particular, you should also pay attention to roundoff errors.
Hint: a float
x
is a multiple of 0.5 if and only if 2*x
is an integer. So 2*x - floor(2*x)
must be close to zero, i.e. abs(2*x - floor(2*x)) < 1e-12
to avoid floating point errors.
See related question: Checking if float is an integer
Upvotes: 0