Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I can\'t seem to figure out why I am getting an error. My goal is to get this to

ID: 3854163 • Letter: I

Question

I can't seem to figure out why I am getting an error. My goal is to get this to display in a .txt file. How would I go about doing so?

#include <iostream>
using namespace std;

int main()
{
    int code;
    char user;
    ofstream fout;

    fout.open("C:\practice\employee.txt");

    cout << " Find the Employee App ";

    do
    {
        cout << " Please enter the employee code: ";
        cin >> code;

        while(code <= 0 || code >5)
        {
            cout << "      ***Invalid code***" << endl;
            cout << " Please enter the employee code: ";
            cin >> code;
        }

        switch(code)
        {
        case 1:
            cout << "    Employee name: Sam" << endl;
            break;
        case 2:
            cout << "    Employee name: Jack" << endl;
            break;
        case 3:
            cout << "    Employee name: Sue" << endl;
            break;
        case 4:
            cout << "    Employee name: Sue" << endl;
            break;
        case 5:
            cout << "    Employee name: Mary" << endl;
            break;
        }

        cout << " Would you like to search again (Y/y or N/n)? ";
        cin >> user;

        if (user == 'N' || user == 'n')
        {
            cout << " Good Bye!";
            break;
        }

    }while(user == 'Y' || user == 'y');

    fout.close();


    return 0;
}

Explanation / Answer

Problems with your code

1) You were missing the header file <fstream> which is required for file handling.

2) You were creating the file in C partition. If your OS is installed in C drive then it would not permit the C++ compiler to create or delete anything in C partition. So change the partition to D or any other partition other than C.

3) You were just creating the text file but not writing anything into it. So you must write in it in every case block by the statement, fout >> name , where name is the name of the employee you want to write.

Corrected C++ Code

#include <iostream>
#include<fstream>
using namespace std;
int main()
{
int code;
char user;
ofstream fout;
fout.open("D:\practice\employee.txt");
cout << " Find the Employee App ";
do
{
cout << " Please enter the employee code: ";
cin >> code;
while(code <= 0 || code >5)
{
cout << " ***Invalid code***" << endl;
cout << " Please enter the employee code: ";
cin >> code;
}
switch(code)
{
case 1:
cout << " Employee name: Sam" << endl;
fout<<"Sam ";
break;
case 2:
cout << " Employee name: Jack" << endl;
fout<<"Jack ";
break;
case 3:
cout << " Employee name: Sue" << endl;
fout<<"Sue ";
break;
case 4:
cout << " Employee name: Sue" << endl;
fout<<"Sue ";
break;
case 5:
cout << " Employee name: Mary" << endl;
fout<<"Mary ";
break;
}
  
cout << " Would you like to search again (Y/y or N/n)? ";
cin >> user;
if (user == 'N' || user == 'n')
{
cout << " Good Bye!";
break;
}
}while(user == 'Y' || user == 'y');
fout<<" ";
fout.close();

return 0;
}

NOTE: Please note that if you change the partition then you must have a folder/directory in it named practice to put the text file in it else your program will not produce the file. So you must create the folder in the appropriate partition ( D/E/F ) with name practice or change the folder/directory name with any other name which is available in the partition.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote