string rating(bool ozone, bool no2, bool so2)
{
    string result;
    int 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 = 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 problem is almost always that the program is trying to use a file stream before a file has actually been opened, or the filename/read method is wrong for the environment. As noted, open the file and verify it opened successfully; as suggested, show the code when asking for help. Below is a simple, robust pattern that avoids common pitfalls (filenames with spaces, wrong working directory, unread flags).

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

int main(int argc, char* argv[]) {
    std::string fname;
    if (argc >= 2) fname = argv[1];
    else std::getline(std::cin, fname); // allows spaces in the filename

    std::ifstream in(fname);
    if (!in.is_open()) {
        std::cerr << "Failed to open: " << fname << '\n';
        return 1;
    }

    // process the file using in...
}

Common troubleshooting points: make sure the program's working directory is what you expect (relative paths fail quietly), check file permissions, trim stray whitespace from the filename, and check/clear stream state if you reuse a stream (call clear() before reusing). If you prefer exceptions, enable them on the stream and catch std::ios_base::failure. For reference on correct usage and state-checking, see the std::ifstream documentation: std::ifstream reference.

Recommended Answers

All 2 Replies

One site rule use CODE TAGS, two inFile >> fileName; ? I just stopped reading there.

notice this in your code

// inFile is a file input stream, and it havn't open a file yet, so you got the error
inFile >> fileName;

I guess you wanna this:

// read user input fileName from command line
cin >> fileName;
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.