Read and writing .txt files in C++

73 Views Asked by At

I have a .txt file named "COP1334C.txt" and it has this inside:

-----------------------------------------
COP1334C - Introduction to C++ Programming
Prof. Mary Taboada
Summer 2022
Arthur Allen

92
Arlette Blum
97
Ilan Crest
76
Cliff Do

87
Christina Francois

97
Matthew Garcia

81
William Irons

52
Saphire Jones
84
Amari Louis

80
Alexandra Macri
78
Sebastian Orwell
85
Marlene Pinheiro
85
Adam Quest
76
Jean Ross
100
Carla Smith

60
Cin Tian
95
Elizabeth Urich
100
Ramon Vargas
73
Ryan Walters
100
-------------------------------------------------------------

And the next thing under this message is my C++ code, for some reason it does not open the .txt file and I would like to know why exactly it does not open the file.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main() {
    int choice = 0;

    while (choice != 2) 
    {
        // Display menu
        cout << "Menu:\n";
        cout << "1. Process Grades Summary report\n";
        cout << "2. Quit\n";
        cout << "Enter your choice (1 or 2): ";
        cin >> choice;

        if (choice == 1) 
        {
            string fileName;
            cout << "Enter the name of the file (COP1334C.txt): ";
            cin >> fileName;

            ifstream inFile(fileName);
            if (!inFile.is_open()) 
            {
                cout << "Error: Unable to open file " << fileName << ".\n";
                continue;
            }

            string studentName;
            int grade;
            int totalGrade = 0;  // Sum of grades
            int highest = -1;    // Initialize to a value lower than any possible grade
            int lowest = 101;    // Initialize to a value higher than any possible grade
            int studentCount = 0;

            // Read student data from the file
            while (getline(inFile, studentName) && inFile >> grade) {
                inFile.ignore(); // Ignore newline character

                // Display student name and grade
                cout << studentName << "\t\t" << grade << "\n";

                // Sum grades
                totalGrade += grade;

                // Find highest and lowest grades
                if (grade > highest) {
                    highest = grade;
                }

                if (grade < lowest) {
                    lowest = grade;
                }

                // Count students
                ++studentCount;
            }

            inFile.close();

            // Display summary
            if (studentCount == 0) 
            {
                cout << "No student data found.\n";
            }
            else {
                // Calculate and display highest, lowest, and average grades
                double average = static_cast<double>(totalGrade) / studentCount;

                cout << "\nHighest Grade: " << highest << "\n";
                cout << "Lowest Grade: " << lowest << "\n";
                cout << "Average Grade: " << average << "\n\n";
            }
        }
        else if (choice != 2) {
            cout << "\nInvalid choice. Please enter 1 or 2.\n";
        }
    }

    cout << "\nExiting the program.\n Goodbye!\n";
    return 0;
}

I am expecting this output:

--------------------------------------------

Course Summary App ...

Choose one of the following options
        1. Process Grades Summary report.
        2. Quit.
Option: 1

Grades Summary Report ...

Enter name of file: COP1334C.txt

--------------------------------------------------

COP1334C - Introduction to C++ Programming

Prof. Mary Taboada               Term: Summer 2022


List of Students
--------------------------------------------------
Arthur Allen                  92
Arlette Blum                  97
Ilan Crest                    76
Cliff Do                      87
Christina Francois            97
Matthew Garcia                81
William Irons                 52
Saphire Jones                 84
Amari Louis                   80
Alexandra Macri               78
Sebastian Orwell              85
Marlene Pinheiro              85
Adam Quest                    76
Jean Ross                     100
Carla Smith                   60
Cin Tian                      95
Elizabeth Urich               100
Ramon Vargas                  73
Ryan Walters                  100

Highest Grade: 100
Lowest  Grade: 52
Average Grade: 84.1

--------------------------------------------------

Choose one of the following options
        1. Process Grades Summary report.
        2. Quit.
Option: 2

Good Bye ...
0

There are 0 best solutions below