You are here:

C++/using fout

Advertisement


Question
Hi- I am very new at this and am using VBc++ free Beta version.

Am trying to get is leap information to print out in semi-table form to a text file which I can then print out. Have used #include <fstream> and ofstream fout ("output.txt"); but text file opens with nothing in it?  Any help you can give me with this is greatly appreciated- code below

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

bool is_leap (int year);
//return value if yr is a
//leap year, false otherwise

void display_info (int m, int year);
// sends leap year to txt file as table

void main()
{
  ofstream fout ("output.txt");
  int m, year, days;
  
  while (true)
  {
     cout << "Enter a month (1-12): ";
     cin >> m;

if ( m > 12 || m < 1 )
  {
     cout << "Month is invalid." << endl;
  }
else if (m==1 || m==3 || m==5 || m==7 || m==8 || m==10 ||m==12)
  {
     cout << "There are 31 days in ";
     if (m==1) cout << "January" << endl;
     else if (m==3) cout << "March" << endl;
     else if (m==5) cout << "May" << endl;
     else if (m==7) cout << "July" << endl;
     else if (m==8) cout << "August" << endl;
     else if (m==10) cout << "October" << endl;
     else
     cout << "December" << endl;
  }
else if (m==4 || m==6 || m==9 || m==11)
  {
     cout << "There are 30 days in ";
     if (m==4) cout << "April" << endl;
     else if (m==6) cout << "June" << endl;
     else if (m==9) cout << "September" << endl;
     else if (m== 11) cout << "November" << endl;
  }
else
  {
     cout << "For February, also enter the year: ";
     cin >> year;
     
       if (is_leap(year))
          {        
           days = 29;
        cout << "There are 29 days in February" << endl;
        fout << setw(6) << m << setw(12) << year << setw(18);
        fout << setw(24) << "leap year" << endl;
        fout.close();

     }
     else
     {
        days = 28;
        cout << "There are 28 days in February" << endl;
        fout << setw(6) << m << setw(12) << year << setw(18);
        fout << setw(24) << "not a leap year" << endl;
        fout.close();
        }
  }
     
  }
}
bool is_leap(int year)
{
   if (year % 400 == 0)
   {
       return true;
     
   }
   else if (year % 100 != 0)
   {
       return false;
   }
   else if (year % 4 == 0)
   {
       return false;
   }
   
  }  

Answer
Hello eileen, thank you for the question.

I compiled and ran your code with Visual C++ 2003. Aside from being stuck in an infinite while loop (the while(true) doesn't have a terminating condition), the output in the file was fine. Could you please give the the data you are using to produce this problem so I can replicated it?

There is one thing I don't understand. You have a while loop looping through this process. But, after you write the data out, you close the file if the month is February, and not for the other 2 options. If you do this, then select January or any other month, no data will be written because you did not reopen the file. If you do reopen the file, all existing data will be overwritten. You should probably wait to call close() on the fils stream until the very end of the program. Perhaps this is your problem?

If you could clarify a little bit, it would be extremely appreciated.

I hope this information was helpful.

- Eddie

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Eddie

Expertise

I can answer questions about the C++ language, object oriented design and architecture. I am knowledgable in a lot of the math that goes into programming, and am certified by ExpertRating.com. I also know a good deal about graphics via OpenGL, and GUIs.

Experience

I have completed numerous games and demos created with the C++ programming language. Currently employed as a software engineer in the modeling and simulation field. I have about 7 years experience.

©2012 About.com, a part of The New York Times Company. All rights reserved.