Writing and reading code from a text file Using file stream object, write a program to read an external text file ("myCourse.txt") which contains all the course name and numbers you are taking this semester, and to generate an analysis (total number of courses) into an external file ("myCourseSummay.txt"). 


This is a past question my teacher has been hinting at us to practice, so my friend and I made this and wanted to know if would be acceptable. Personally, I think we messed the writing to the .txt file.

#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include&lt;string&gt;
using namespace std;

int intro()
{
    cout &lt;&lt; "Hello World!";
    return 0;
}

int main()
{
    ofstream myCourse;
    myCourse.open("myCourse.txt");
    char course1[20], course2[20], course3[20], course4[20];

    cout &lt;&lt; "Enter number of courses. max of 4" &lt;&lt; endl;
    cout &lt;&lt; "Enter 1st course: ";
    cin &gt;&gt; course1;
    myCourse &lt;&lt; course1 &lt;&lt; endl;
    cout &lt;&lt; "Enter 2nd course: ";
    cin &gt;&gt; course2;
    myCourse &lt;&lt; course2 &lt;&lt; endl;
    cout &lt;&lt; "Enter 3rd course: ";
    cin &gt;&gt; course3;
    myCourse &lt;&lt; course3 &lt;&lt; endl;
    cout &lt;&lt; "Enter 4th course: ";
    cin &gt;&gt; course4;
    myCourse &lt;&lt; course4 &lt;&lt; endl;

    myCourse.close();

    ifstream myCourseSummary;
    string word;
    int count = 0;
    myCourseSummary.open("myCourseSummary.txt");

    while(!myCourseSummary.eof())
    {
        myCourseSummary &gt;&gt; word;
        count++;
    }

    cout &lt;&lt; "Total number if courses is(are) " &lt;&lt; count &lt;&lt; "!";
    myCourseSummary.close();

    cin.get();
    return 0;
}
eof inside a loop condition

See why is eof inside a loop condition considered wrong? You do not want to do:

while (!myCourseSummary.eof())
{
    myCourseSummary &gt;&gt; word;
    count++;
}


You do want to do:

while (myCourseSummary &gt;&gt; word)
{
    count++;
}


Arbitrary length course names

You are reading coureses into a char[20]. What if I have a 25-character course name? What about 50? What if it has a space in it? You need to handle all of these conditions, so you should prefer to std::getline a std::string. 

Actually solve the problem

The problem statement is:


  read an external text file (“myCourse.txt”) which contains all the course name and numbers you are taking this semester


But you are prompting the user for course names and writing them to a file. You should read the file directly.

Also:


  generate an analysis (total number of courses) into an external file (“myCourseSummay.txt”)


You are currently writing into "myCourse.txt" and reading from "myCouresSummary.txt". The question asks you to read from the first, and write a number into the latter. That is:

std::ifstream myCourse("myCourse.txt");
// do some reading
// come up with what count is

std::ofstream myCourseSummary("myCourseSummary.txt");
myCourseSummary &gt;&gt; count;


You should be able to handle any number of courses. Just read from the file until it's done.