Alright, I have some questions.

1. This code does make some sense right?
2. I keep getting my failure to open file message, did I make the program do that or are my test files just not working?
3. How do I make the program acknowledge - and + as characters when sorting through the characters?

/*------------------------------------------------------------------------------*/
/* Page177 Problem12                                                            */
/*                                                                              */
/* This program will filter data and point out and tally any non-integer,       */
/* - sign, or + sign characters.                                                */ 

#include<iostream>
#include<fstream>
#include<string>

using namespace std;

int main()
{
	// Declare objects.
	ifstream datain;
	string filename;
	ofstream dataout;
	double mistakes(0), t;
	
	// Ask user for file name.
	cout << "Please enter the name of the file you want filtered." << endl;
	cin >> filename;

	// Assign datin a file
	datain.open(filename.c_str());

	// Check for opening errors
	if (datain.fail())
	{ 
		cerr << "Sorry, the file could not be opened properly."; 
	}

	while(!datain.eof())
	{
		datain >> t;
		
		if( (t=0) || (t=1) || (t=2) || (t=3) || (t=4) || (t=6) || (t=7)
			|| (t=8) || (t=9))
		{ dataout << t; }
		else
		{
			++mistakes;
			dataout << "Illegal character " << t << "found.";
		}
	}

	dataout << "There were " << mistakes << " illegal characters found.";



}

Recommended Answers

All 4 Replies

t=0 should be t==0 (the first assigns 0 to t, the second checks for equality), etc., but in this case since t is a double, it is tough to compare with 0 or 1, etc, due to the inexact representation of doubles. What does your data file look like?

Thank you for your answer. Unfortunately,this program is based on a problem from a book that gives no indication of the type of file that will be filtered.

I would read the data in as a std::string using getline and go through character by character (or using datain.get() to read the file in character by character) rather than dealing with doubles (especially if there are +/- and other characters thrown into the mix). You can use the functions in <cctype> to figure out if the characters are numbers, or simply use something like

char c = 'd';
if((c >= 'a' && c <='z') || (c >='A' && c <='Z')|| etc.)
    //etc.

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.