Hello, I have been trying to get this program to work for a while now with no luck and I was hoping that someone could clear things up. My problem is the program always gives the
failure to open file message so I can't tell if anything else works or not. Can anyone tell if I programed it that way or if it just doesn't like my files. If you notice any glaring errors with my logic I wouldn't mind the feedback but I think I can fix it myself if I can see the results the program gives.
/*----------------------------------------------------*/
/* */
/* This program will sort a file of integers into an */
/* array then search for an integer that has been */
/* chosen by the user. */
#include<iostream>
#include<cmath>
#include<fstream>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
// Declare and initialize objects.
const int N=100;
string filename;
ifstream file;
int count[N], k, n(0), b(0), s(0), t, find, hold, min, minindx;
// Ask user for file to search.
cout << "Please insert the name of the file" << endl;
cin >> filename;
file.open(filename.c_str());
if(file.fail())
{
cerr << "The file you selected could not be" <<
" opened properly" << endl;
system("Pause");
}
else
{
// Ask for value to find.
cout << "Please enter the number you are looking for." << endl;
cin >> find;
// Fill the array
while (!file.eof() && n<=99)
{
file >> count[n];
++n;
}
if(n=100)
{
cout << "Sorry there were at least 100 numbers " <<
"in the file," << endl;
cout << "only the first 100 will be checked" << endl;
}
// Sort the array in accending order.
for(s=0; s<N; s++)
{
min=count[s];
minindx=s;
for(t=s+1; t<N; t++)
{
if (count[t]<min)
{
min=count[t];
minindx=t;
}
}
if(min<count[s])
{
hold= count[s];
count[s]= min;
count[minindx]= hold;
}
}
// Search for the number.
while(b<=49)
{
if(find= count[n-b] || count[n+b])
{
cout << "Value found." << endl;
break;
}
else if(b<49)
{
++b;
}
else if (b=49)
{
cout << "Value not found." << endl;
break;
}
}
file.close();
system("pause");
}
}
Thank you for your time.