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.                                                */ 


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

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

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

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

7 Years
Discussion Span
Last Post by Craftknight

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?

Edited by jonsca: n/a


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.)
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.