Program not working

Can someone inform me on what im doing wrong? thank you!

#include <iostream>
using namespace std;

int main()
{

int num1,num2;
cout << "Enter your first number: ";
cin >> num1;
cout << "Enter your second number: ";
cin >> num2;
cout << num1; "+"; num2; "="; << num1 + num2 << endl;

system("pause");
return 0;
}
Please use code tags and specify what error you're getting.

Your line
cout << num1; "+"; num2; "="; << num1 + num2 << endl;
should look like this:
cout << num1 << "+" << num2 << "=" << num1 + num2 << endl;
Thank you for the quick response. Your answer helped me tremendously; however ive come across another issue. The errors i am getting are:
error C2296: '%' : illegal, left operand has type 'float'
error C2297: '%' : illegal, right operand has type 'float'
the line of code that contains the modulus is giving me the error

#include <iostream>
#include <cmath>
using namespace std;

int main()
{

float num1,num2,num3;
cout << "Enter your first number: ";
cin >> num1;
cout << "Enter your second number: ";
cin >> num2;
cout << num1 << "+" << num2 << "= " << num1 + num2 << endl;
cout << num1 << "-" << num2 << "= " << num1 - num2 << endl;
cout << num1 << "/" << num2 << "= " << num1 / num2 << endl;
cout << num1 << "*" << num2 << "= " << num1 * num2 << endl;
cout << num1 << "%" << num2 << "= " << num1 % num2 << endl;
cout << "(" << num1 << "+" << num2 << ")" << "^2" << "= " << (num1 + num2) * (num1 + num2) << "\n" << endl;
cout << "Pythagorean's Theorem" << endl;
cout << "A" << "^2" << " + " << "B" << "^2" << " = C^2" << "\n" << endl;
cout << num1 << "^2" << " + " << num2 << "^2" << " = C^2" << endl;
num3 = (num1 * num1) + (num2 * num2);
cout << num3 << " = " << "C^2" << endl;
cout << sqrt (num3) << " = " << "C" << endl;

system("pause");
return 0;
}
You can't perform a modulo operation on floats. It is only defined for ints, for obvious reasons.
thank you for replying,. I have tried to assign the "nums" as integers; but when i do that, the square root function at the end does not work... i get the error

error C2668: 'sqrt' : ambiguous call to overloaded function
could be 'long double sqrt(long double)'
or 'float sqrt(float)'
or 'double sqrt(double)'

I must be missing something?
The error message is quite clear: function sqrt() is not defined for ints. When you pass an int as the argument, the compiler doesn't know whether it should convert it to a long double, a float or a double.

You could make num3 one of those types or cast it to the desired type, although there really is no need to make num3 an int.
You could also make your own sqrt() function. Just make sure the types it takes are different.

-Albatross
you could try
sqrt(static_cast<double>(your_number),
which forces the conversion of your_number from type int to type double.
but know that doing something like
1
2
3
double num = 4.2;
static_cast<int>(num);
std::cout<< "nums value is :"<< num;

will cause a loss of information because 4.2 becomes 4 since you are converting a lower type to a higher type
eg a double is higher than a float is higher than a int
look here for some more info on type casting
http://www.cplusplus.com/doc/tutorial/typecasting/
Last edited on
1. sqrt() can only be used with data of type double.
2. You need to include the <cmath> header file to use sqrt().
3. Maybe consider learning the basics before jumping ahead to using functions.
1. sqrt() can only be used with data of type double.


Actually, as the error message posted by the OP points out, sqrt() is defined for doubles, long doubles and floats. If it was defined only for doubles, the compiler would accept an int argument, because there would be only one conversion possible (int to double, of course).
Last edited on
Topic archived. No new replies allowed.