Ask your own question, for FREE!
Computer Science 14 Online
OpenStudy (christos):

C++ Help needed! The program I need to build is supposed to do this: http://screencast.com/t/qbp34sVfC

OpenStudy (christos):

// // main.cpp // week5-4-4 #include <iostream> #include <cmath> using namespace std; int main() { double a; cin >> a; if (sqrt(a)%1.0 == 0) cout << "It's an integer."; else cout << "It's NOT an integer. Sorry."; return 0; }

OpenStudy (christos):

This is the code I created for the problem. I don't know why it doesn't run. Can you help me spot the problem?

OpenStudy (espex):

You cannot use the modulus operator with a double.

OpenStudy (christos):

what is a "modulus operator" ?

OpenStudy (espex):

%

OpenStudy (espex):

Use this instead: if (fmod(sqrt(a),1.0) == 0)

OpenStudy (christos):

wait a min, but why I cant use % with doubles?

OpenStudy (espex):

Because it is designed to take an integer.

OpenStudy (christos):

hmm hold on lemme try

OpenStudy (anonymous):

You might also have trouble with the == it's always iffy to compare doubles. think of all the times a computer's told you 2.0 + 2.0 = 3.99999999

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!