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.

Recommended Answers

All 6 Replies

The first thing you need to do is check your computer's file system and see if the file name you enter is in the same directory as where your program is running. If it is somewhere else then you need to specify the full path to the file, such as c:\Prograzm Files\somethere. If the path and/or filename contains spaces, as I posted, then you can use the >> operator of cin, but you will have to use getline().

I didn't know that could matter so thanks for the info, but it didn't seem to make a difference. What bugs me with this problem is what I have seems to match my book. I forgot to mention earlier though, is it possible for an anti-virus program (Norton for my pc) to stop it from working?

If Norton were the cause then you would have gotton a Norton error message. If file.open() fails the only reason on MS-Windows is that the file is not in the directory you think it is. What compiler are you using?

I am using Microsoft Visual C++ 2010 Express. Also, I do get a message from Norton telling me it quarentined the program but I tell it to restore it and it lets me run the program.

I got that stupid message too, so I disabled it. If you are running Norton 360, click that green tool on your desktip and it will bring up a Norton window. On that window select menu item Settings, then Antivirus link. From there you can turn off SONAR Protection.

With VC++ 2010 Express (which I also use) you have to put the file you want to open in the same folder as the program's *.cpp files.

Alright it worked,awesome! Thank you very much for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.