2016-11-28 10 views
0

私の質問は、プログラムに何が間違っているのか、また指定した出力ファイルに書き込まない理由を調べることです。関数を含むカレンダープログラムを.txtファイルに出力する方法

私はofileを使用してテキストを出力しますが、正しく返されません。

// This function returns the number of days in a month 
int numOfDaysInAMonth(int m, int y) 
{ 
    if (m == 1) 
    return(31); 
    else if (m == 2 && y % 4 != 0) 
    return(28); 
    if (m == 2 && y % 4 == 0) 
     return(29); 
    else if (m == 3) 
    return(31); 
    else if (m == 4) 
    return(30); 
    else if (m == 5) 
    return(31); 
    else if (m == 6) 
    return(30); 
    else if (m == 7) 
    return(31); 
    else if (m == 8) 
    return(31); 
    else if (m == 9) 
    return(30); 
    else if (m == 10) 
    return(31); 
    else if (m == 11) 
    return(30); 
    else if (m == 12) 
    return(31); 
} 

    // It takes the number of the month and prints outs the name of the month and the frame of the calander 
void printMonth(int m) 
{ 
    ofstream ofile; 
    ofile.open("calendar.txt"); 

    if (m == 1) 
    { 
    ofile << "January" << endl; 
    } 
    else if (m == 2) 
    { 
    ofile << "February" << endl; 
    } 
    else if (m == 3) 
    { 
    ofile << "March" << endl; 
    } 
    else if (m == 4) 
    { 
    ofile << "April" << endl; 
    } 
    else if (m == 5) 
    { 
    ofile << "May" << endl; 
    } 
    else if (m == 6) 
    { 
    ofile << "June" << endl; 
    } 
    else if (m == 7) 
    { 
    ofile << "July" << endl; 
    } 
    else if (m == 8) 
    { 
    ofile << "August" << endl; 
    } 
    else if (m == 9) 
    { 
    ofile << "September" << endl; 
    } 
    else if (m == 10) 
    { 
    ofile << "October" << endl; 
    } 
    else if (m == 11) 
    { 
    ofile << "November" << endl; 
    } 
    else if (m == 12) 
    { 
    ofile << "December" << endl; 
    } 


    ofile << " S M T W T F S" << endl; 
    ofile << "_____________________" << endl; 
} 

// Helps with the skipToDay function 
void skip(int i) 
{ 
    ofstream ofile; 
    ofile.open("calendar.txt"); 

    while (i > 0) 
    { 
    ofile << " "; 
    i = i - 1; 
    } 
} 

// This function prints out the days in the month after the header for each month 
void printDaysofMonth(int numDays, int &weekDay) 
{ 
    ofstream ofile; 
    ofile.open("calendar.txt"); 

    int day = 1; 
    skipToDay(weekDay); 
    while (day <= numDays) 
    { 
    ofile << setw(2) << day << " "; 
    if (weekDay == 6) 
    { 
     ofile << endl; 
     weekDay = 0; 
    } 
    else weekDay = weekDay + 1; 
    day = day + 1; 
    } 
} 

// Prints spaces in monthly calander 
void skipToDay(int d) 
{ 
    return skip(3 * d); 
} 
+0

私はあなたが使用するときに何を見ていないことを不平を言っていると推定[ばかげ 'システム(「一時停止」);'ハック](http://stackoverflow.com/questions/1107705/systempause-why-違いますか)。ファイルを 'close()'する必要があります。 –

+0

@SamVarshavchikシステムをご使用いただきありがとうございます(「一時停止」)。ハック、残念ながら私は初めての学習者に過ぎません。これは、プログラムを終了するための講義でどのように教えられているかです。 –

答えて

0
void printDaysofMonth(int numDays, int &weekDay) 
{ 
    ofstream ofile; 
    ofile.open("calendar.txt"); 
    ... 
} 

int main() 
{ 
    ofstream ofile; 
    ofile.open("calendar.txt"); 
    printDaysofMonth(...); 
    ofile << "data..."; 
    ... 
} 

ファイルは既に開かれています。第1のストリームは、第2のストリームが書いたものがあればそれを上書きする。参照によってofstreamを渡すようにする機能を変更します。

void printDaysofMonth(ofstream &ofile, int numDays, int &weekDay) 
{ 
    ... 
} 

int main() 
{ 
    ofstream ofile; 
    ofile.open("calendar.txt"); 
    printDaysofMonth(ofile, ...); 
    ... 
} 

あなたはプログラムを短くするために、配列内のデータを保持することにより作ることができ、他の改良点があります。タイピングが減ると、潜在的なエラーが少なくなります。

int is_leap_year(int y) 
{ 
    return (y % 4) == 0 && y % 100 != 0 || y % 400 == 0; 
} 

int numOfDaysInAMonth(int m, int y) 
{ 
    int days = 0; 
    if(m >= 1 && m <= 12) 
    { 
     const int days_in_month[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; 
     int days = days_in_month[m - 1]; 
     if(m == 2 && is_leap_year(y)) 
      days++; 
    } 
    return days; 
} 

const char* get_month_name(int m) 
{ 
    const char *month_name[] = { 
     "January", "February", "March", "April", "May", "June", 
     "July", "August", "September", "October", "November", "December" }; 
    if(m >= 1 && m <= 12) 
     return month_name[m - 1]; 
    return "month name error"; 
} 

void printMonth(ofstream &ofile, int m) 
{ 
    ofile << get_month_name(m) << "\n"; 
    ofile << " S M T W T F S" << endl; 
    ofile << "_____________________" << endl; 
} 
+0

@Barmak Shemiraniありがとう、これは私の多くを助けた。あなたが示したように私はすべての機能を変更しましたが、それは自分で解決したいくつかの問題のほんの一部でしか機能しませんでした。もう一度、ありがとう。 –

+0

問題ありません。ところで、うるう年の計算を変更する必要があります。これは 'y%4'ではありません。更新を参照してください。 –

関連する問題