Absolute value function?

I am writing a program to a number closest to 100 from a list of numbers. I am not familiar with the syntax. Would I declare int=x, and x (abs=100)?
check math.h library
http://www.cplusplus.com/reference/clibrary/cmath/
(look for fabs function)
Ok, so I've got some code on this. I'm trying to get the value closest to 200, but what I am getting is way off. Any thoughts?



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

int main ()

{
ifstream InFile;
ofstream OutFile;
int num;
int diff;
int closest;


InFile.open("E:\\CFiles\\DataFile2.txt");
OutFile.open("E:\\Answers.txt");
InFile>>num;
closest=num;
diff=abs(num-200);

while (InFile)

{
InFile>>num;
if (abs(num-200<diff)) //Sets condition
{
closest=num;
}
}

cout<<"The closest value is= "<<num<<endl;
InFile.close(); // I was asked to include this code by instructor
InFile.clear ();
}

It is not important whether your program does what you want it has to do. It is important whether your program is successfully compiled.:)
So is your program compiled without errors?
Last edited on
Nevermind, got it! I forgot to plug in the flash drive with the file on it...Lol. I am a n00b indeed.
I think that this statement is invalid.

if (abs(num-200<diff))
Last edited on
Yeah, I had to rework the code a little bit, but its working now. Thanks!
My congratulations! I think C Plus Plus and C Minus Minus will give C Zero Zero.:)
Topic archived. No new replies allowed.