I'm having problems with this c++ program.

I need to read a file containing years on each line, and determine if the year is a leap year.

The isLeap function looks fine, i'm just having problems actually reading the file and implementing the function.

Can anyone help me out?

#include <iostream>
#include <stdio.h>
#include <fstream>
#include <string>

using namespace std;

char *isLeap(int year) {

    char *result = new char[20];

    if (year <= 1) {
        sprintf(result, "%d error\n", year);
    } else {
        if (year % 400 == 0) {
            sprintf(result, "%d yes\n", year);
        } if (year % 100 == 0) {
            sprintf(result, "%d no\n", year);
        } else if (year % 4 == 0) {
            sprintf(result, "%d yes\n", year);
        } else {
            sprintf(result, "%d no\n", year);    
        }
    }

    return result;
}

int main() 
{
    
    char filename;
    int year;
    
    cout << "Please enter the filename: " << endl;
    cin >> filename >> endl;
    
    while (filename != EOF)
    {
        cout << filename<< endl;
        return 1;
    }
    
    return 0;
}

You never attempt to read a file. You read in a filename, but there is no attempt to read the file.

char filename;

filename needs to be a string, not a char.

cin >> filename >> endl;

Don't read in the endl. Just read in the filename.

cin >> filename;

You need to set up an ifstream object and open it. You'll open it with the filename.

ifstream inputFile;
inputFile.open(filename.c_str());

You can read from the file into an int variable using the >> operator, then call your function, passing it that variable.

Lots of good ifstream tutorials out there. Google "C++ ifstream tutorial".

Ok thanks for the tips.

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.