How to have the output not have a negative number

My question for this is how do I get the output to not have a number number for example:

Input first distance...
Meters: 3
Centimeters: 56
Millimeters: 2

Input second distance...
Meters: 5
Centimeters: 39
Millimeters: 6

The first distance is 3562 mm.
The second distance is 5396 mm.
The difference is 1834 mm.

This is my desired output but for some reason I keep on getting -1834 instead of 1834. If anyone can help me that would be great thanks!




#include <iostream>

using namespace std;



int NumMillimeters(int,int,int); //Function prototype



int main()
{

//variables
int meters; //to hold meters
int centimeters; //to hold centimeters
int millimeters; //to hold millimeters
int secondMeters; //to hold the second meter input
int secondCentimeters; //to hold the second centimeter input
int secondMillimeters; //to hold the second millimeter input
int firstDistance; //to hold the first distance in units of millimeters
int secondDistance; //to hold the second distance in units of millimeters
int difference; // the difference of the first and second difference

//ask the user for the meters, centimeters, and millimeters.
cout << "Input first distance...\n";
cout << "Meters: ";
cin >> meters;

cout << "Centimeters: ";
cin >> centimeters;

cout << "millimeters: ";
cin >> millimeters;


firstDistance = NumMillimeters(meters, centimeters, millimeters);
//Call to function NumMillimeters and put the value in firstDistance.



//ask the user for the second set of meters, centimeters, and millimeters.
cout << "\nInput second distance...\n";
cout << "Meters: ";
cin >> secondMeters;

cout << "Centimeters: ";
cin >> secondCentimeters;

cout << "millimeters: ";
cin >> secondMillimeters;


secondDistance = NumMillimeters(secondMeters, secondCentimeters, secondMillimeters); //Call to function NumMillimeters and put value in secondDistance.

//Print the equivalent distance in millimeters of each distance.
cout << "\nThe first distance is " << firstDistance << " mm.\n";
cout << "The second distance is " << secondDistance << " mm.\n";

difference = (firstDistance - secondDistance); //Calculate the difference of the two distances.

cout << "The differenece is " << difference << " mm.\n"; //Print the difference of the two differences.

return 0;

}

int NumMillimeters(int meters, int centimeters, int millimeters)
{

int distance; //To hold the distance with in millimeters.

return distance = (meters * 1000) + (centimeters * 10) + millimeters;
//Calculate the conversion to millimeters and put it in distance.


}


Last edited on
Use abs found in <cstdlib> to compute the absolute value.

http://www.cplusplus.com/reference/cstdlib/abs/
Last edited on
include a header file <cstdlib> and use abs
OP: can't see anything out of place:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Input first distance...
Meters: 1932
Centimeters: 45
millimeters: 67

Input second distance...
Meters: 1834
Centimeters: 78
millimeters: 12

The first distance is 1932517 mm.
The second distance is 1834792 mm.
The differenece is 97725 mm.

Process returned 0 (0x0)   execution time : 21.074 s
Press any key to continue.


PS: though be aware that with ints you will not be able to handle decimal inputs
for some reason I keep on getting -1834 instead of 1834.


That's absolutely correct. 3562 - 5396 = -1834
closed account (48T7M4Gy)
if(difference < 0){ difference *= -1; }
Topic archived. No new replies allowed.