#include <iostream>
#include <cstdlib>
#include <cstddef>
#include <string>
#include "Array.h"
usingnamespace std;
usingnamespace list;
int main()
{
List<double>list(10);
list.add(1.1);
list.add(5.5);
list.add(2);
cout << list;
cout << "The number is in index: " << list.find(5.5) << endl;
char exit;
cout << "Enter character to exit: ";
cin >> exit;
return 0;
}
For some reason when I compile this code line 18 tells me that 5.5 is in index -1 (-1 is the value that the function "find" is supposed to send if it doesn't encounter the element). 5.5 is obviously in the list so it should return [positive] 1. What's the problem? I'm new with templates so perhaps it's something I don't know about yet.
Well, equality comparisons with floating point numbers are highly unreliable. My guess would be that there's some weird behavior going on behind the scenes involving the FPU and conversions between floating points types of different sizes. See what happens if you change the comparison to abs(item[i]-look_for)<0.0001.