maxloo2 - 1 month ago 5
C++ Question

# Calculate using int and output float?

``````//findSlope(twoPoints).exe
//finding the slope of line AB, using coordiantes of point A and B.

#include <iostream>

int main()
{
int a, b, c, d;

std::cout << "The X coordiante of A: ";
std::cin >> a;
std::cout << "\nThe Y coordiante of A: ";
std::cin >> b;
std::cout << "\nThe X coordiante of B: ";
std::cin >> c;
std::cout << "\nThe Y coordiante of B: ";
std::cin >> d;
std::cout << "\nThe slope of line AB = " << std::endl;

std::cout.setf(std::ios::fixed);
std::cout.precision(3);

//alternative= std::cout << fixed << setprecision(#) << answer << std::endl;

std::cout.unsetf(std::ios::fixed);

return 0;
}
``````

I am learning C++ and I tried to code a program that calculate the slope using the coordinates of two points.

I understand that if I use
`float`
for variables I declared for the coordinates, the result of the calculation would output as
`float`
with decimals. However, I wonder if I may still use
`int`
for user input so that I can ensure the inputs are integers.

Extra question: Would it be possible to convert a
`float`
presented in the form of "#.##" to "# #/#"? More like how we do mathematics IRL.

``````answer = (b-d)/(a-c*1.0);
``````answer = (b-d)/(a-(float)c);