Im suppose to read in a file (which it isn't doing) and get numbers form them to see if they pass or fail certain specifications. Can someone help?

the exact specifications are:
The company you work for, Acme Environmental Monitoring, runs sensors to test air quality at different locations. These readings need to be processed to grade the air according to the national air quality standards.

You need to write a program that will read a file of these readings, test them against the standards and output the ratings.

This program will require reading from a data file, doing some logical checking and calculations with the information in that file, and reporting the results.

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

using namespace std;

//we have two functions

bool in_compliance(float standard, float first, float second, float third);

string rating(bool ozone, bool no2, bool so2);

int main()
{
	bool ozone_passfail, no2_passfail, so2_passfail;
	string theRating;
	ifstream inFile;
	string fileName;
	float standardPF, ozonePF, no2PF, so2PF;
	string location;
	string ozoneStats;

	

	//Get filename from user
	cout << "Enter the filename of data to process --> ";
	cin >> fileName;

	//Open the file
	inFile.open(fileName.c_str());

		//Check if file opened
	while(!inFile.is_open())	
	{
		cout << "Sorry, that file does not exist!" << endl;
		cout << "Enter the filename of data to process --> ";
		cin >> fileName;
		
	}
	inFile.ignore(1, '\n');
	//Read the location and store in string variable
	getline(inFile, location);
		//Read standard,three sample values and make sure we skip the rest of the line
	inFile.ignore(1, '\n');
	getline(inFile, ozoneStats);

			
	ozone_passfail = in_compliance(standardPF, ozonePF, no2PF, so2PF);
	no2_passfail = in_compliance(standardPF, ozonePF, no2PF, so2PF);
	so2_passfail = in_compliance(standardPF, ozonePF, no2PF, so2PF);
	
	//Repeat for so2, no2
	if(ozone_passfail == true)
		cout << "ground ozone (ppm)" << ozoneStats << "in compliance" << endl;
	else
		cout << "ground ozone (ppm)" << ozoneStats << "out of compliance" << endl;
	
	if(no2_passfail == true)
		cout << "nitrogen dioxide (ppm)" << ozoneStats << "in compliance" << endl;
	else
		cout << "nitrogen dioxide (ppm)" << ozoneStats << "out of compliance" << endl;

	if(so2_passfail == true)
		cout << "sulfur dioxide (ppm)" << ozoneStats << "in compliance" << endl;
	else
		cout << "sulfur dioxide (ppm)" << ozoneStats << "out of compliance" << endl;
	
	
		
		//Output location has some rating
		theRating = rating(ozone_passfail, no2_passfail, so2_passfail);
	cout << "Rating of " << location << "is" << theRating << endl;



	return 0;
}

string rating(bool ozone, bool no2, bool so2)
{
	string result;
	int count;
	
	count = 0;
	
	if(ozone == true)
		count++;
	if(no2 == true)
		count++;
	if(so2 == true)
		count++;
	if(count == 3)
		result = "Gold Star";
	if(count == 2)
		result = "Silver Star";
	if(count == 1)
		result = "Ok";
	if(count == 0)
		result = "Failing";
	
	return result;

}

bool in_compliance(float standard, float first, float second, float third)
{
	int count;
	count = 0;
	if(first <= 0 || second <= 0 || third <=0)
		return false; 


	if(standard >= first)
		count++;
	if(standard >= second)
		count++;
	if(standard >= third)
		count++;
	if(count >= 2)
		return true;
	else
		return false;
}

Dani AI

Generated

: the core problems visible in the posted snippet are (1) the input file loop never re-opens/clears the stream after a failed attempt, (2) the numeric variables are never read from the file (they remain uninitialized), (3) the same string/values are reused for every pollutant output, and (4) in_compliance is called with the wrong arguments. was right to ask about conversion/casting — parsing text into floats must be done explicitly (std::istringstream or >>) and the code should check stream state after each read.

A small, robust pattern for opening a file and parsing a numeric line (adapt to actual file layout) is shown below. This demonstrates re-trying the open, reading a full line, and extracting numbers with an istringstream so the numeric values are actually available to pass to in_compliance.

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

std::ifstream open_retry() {
  std::string name;
  std::ifstream fin;
  while (true) {
    std::cin >> name;
    fin.open(name.c_str());
    if (fin) return fin;
    std::cerr << "Cannot open '" << name << "'. Try again: ";
    fin.clear();
  }
}

// usage: read one line with text, then a line with numbers and parse them
// then call in_compliance(standard, v1, v2, v3) for each pollutant.

Quick checklist to fix the program logic:

  • Initialize variables and always parse numeric fields from the file (check iss >> success).
  • When a filename is rejected, call inFile.clear() and re-open; do not just prompt without reopening.
  • Avoid using ignore(1,'\n') as a replacement for skipping a whole line — use getline or ignore(std::numeric_limits<std::streamsize>::max(), '\n').
  • Call in_compliance with the pollutant-specific standard and its three samples, and print the correct sample string for that pollutant (do not reuse ozoneStats for NO2/SO2).

The parsing + validation approach above (istringstream, check failbit, explicit calls with correct values) will resolve the "file not read" symptom and make the compliance/rating logic reliable.

Where exactly does it not work? Also, I do not see casting anywhere, or where you are getting your floats for that matter; are you converting your strings to floats? I would use the <sstream> class to extract floats personally but frankly I'm a bit befundled by your code.

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.