FILE/IO QUESTION: Bug in my software ifstream does not extract integers from input file into variables in my software

  c++, file, fstream, ifstream

Summarize the Problem

I am creating a console application that calculates a dates value based on the starting date 1900 jan 1st and adds all the days between those two dates. In the problem/my program, I have to use File/IO. Demonstrated in my software below, I attempt to extract integers. However, nothing shows in my output file. When I debug, none of the integers get extracted from input the file. I am unsure of how to fix this problem since i’ve tried many solutions. (WRITING TO OUTPUT FILE IS FINE, CHOICE 1 WORKS GREAT, CHOICE 2 NOT SO MUCH).

Describe what you’ve tried

1.) Changing location of input file (file open correctly in working directory of cpp file)

2.) Switching the file i/o operations to outside of the do while loop (still fails to work)

3.) Check if file is open, check if it’s correctly working, and check if iostream flags are good, bad, fail etc.

4.) Multiple code-rewrites.

THE CODE IN QUESTION

#pragma once

#include<iostream>
#include<string>
#include<fstream>
#include"dayValue.h"

using namespace std;

int main()
{
    dayValue newDay;
    int day = 0, year = 0, month = 0;
    int choice = 0;
    ifstream inFile;
    ofstream outFile;

    inFile.open("dateInput.txt"); //Broken and trying to fix. Not too sure what issue is.
    if(inFile.fail())            //is it ok?
        { cout<<"Input file did not open please check itn";
        return 1;
        }
    outFile.open("dateOutput.txt");
    if(outFile.fail())            //is it ok?
        { cout<<"output file did not open please check itn";
        return 1;
        }
    
    
    cout << "Hi! This is Adam's day calculator program.n";
    cout << "Would you like to input your own date into the console or read dates from a file? nType 1 to input a date or 2 to access your files: ";
    do
    {
        cin >> choice;
        if (choice == 1)
        {
            cout << "Welcome to the day calculator program, can you please input a date for us to storen";
            cout << "Please input the month: ";
            cin >> month;
            cout << "Please input the day: ";
            cin >> day;
            cout << "Please input the year: ";
            cin >> year;
            newDay.setDate(year, month, day);
            newDay.print();
            outFile << "Your date: " << newDay.month << "/" << newDay.day << "/" << newDay.year << " is the weekday " << newDay.findWeekDay() << " and has a day value of " << newDay.findDayValue() << " (current day is inclusive) "; 
        }
        else if (choice == 2)
        {
            //newDay.setDate(year, month, day);
            inFile >> year >> month >> day;
            newDay.setDate(year, month, day);

            //do the loop while there are still numbers in the file
            while (inFile >> year >> month >> day)
            {
                outFile << "Your date: " << newDay.month << "/" << newDay.day << "/" << newDay.year << " is the weekday " << newDay.findWeekDay() << " and has a day value of " << newDay.findDayValue() << " (current day is inclusive) ";
                newDay.setDate(year, month, day);
                inFile >> year >> month >> day;
            }
        }
    } while(choice < 1 || choice > 2);
  
    
    

    cout << "n Thanks for using the day calculator applicationn (Check your output file for write)";
    inFile.close();
    outFile.close();


    
    return 0;
}

Picture of file location

Picture of actual text file in question

Source: Windows Questions C++

LEAVE A COMMENT