Question

: Files and Exceptions: Write a program to create and use a grade book for a...

: Files and Exceptions: Write a program to create and use a grade book for a course. The gradebook is created as a file named as “Course_Name.dat” and includes the following information about students: Student ID, First Name, Last Name, and Grade. Assume the following structure for the gradebook file:1.Students’ records are separated by a new-line character.2.No field includes any white-space character, and fields are separated by space.3.The order of the fields is the same for all the records.Based on the above explanation, write a program that shows a menu including the following options to the user, and responds according to the user selection.Press 1 to create a new gradebook file. Press 2 to search a student in an existing gradebook file. Press 3 to add a student record. If the user selects 1, he/she is asked for the gradebook file name and a new file is created. In case the file already exists, user can choose to keep it or delete all the contents of the existing file.If the user selects 2, he/she is asked for the file name and in case the file name exists, he/she can enter a student ID to search. In case the file doesn’t exist, the program throws an exception.If the user selects 3, he/she is asked for the file name and in case the file already exists, the information of the student is read from the keyboard and the student record is appended to the file. In case the file doesn’t exist, the program throws an exception.

IN C++ WITH MICROSOFT VISUAL STUDIO 2017 IF POSSIBLE PLEASE

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>

using namespace std;

// This is helper function to spilt a line by whitespaces
vector<string> spilt(const string &line){
    
    string buf; // Just a temp buffer
    stringstream ss(line); // String stream

    vector<string> tokens; // Array/Vector of words

    while (ss >> buf)
        tokens.push_back(buf); // Add each word in array

    return tokens;
}

// This is helper function to show menu to user
void show_menu(){
    
    cout << "\n\n";
    cout << "1. Create new Gradebook file\n";
    cout << "2. Search a student in existing record\n";
    cout << "3. Add a student record\n";
    cout << "4. Exit\n";
    cout << "Please select one choice\n";
}


// This function is to create new book
void create_new_book(){
    
    string filename;
    cout << "Enter a file name\n";
    cin >> filename;
    
    ofstream file(filename,std::ios_base::app);
    if(!file)
    {
        // Throw Exception if file could not be opened
        throw runtime_error("Could not open file");
    }
    file.close();
}

// This function is to seach student id in the book
void search_book(){
    
    string filename;
    cout << "Enter a file name\n";
    cin >> filename;
    
    ifstream file(filename);
    if(!file)
    {
        // Throw Exception if file does not exists
        throw runtime_error("Could not open file");
    }
    
    string search_param; // Student ID which needs to be searched
    cout << "Enter Student ID to search\n";
    cin >> search_param;

    string line;
    // Read file line by line
    while (getline( file, line ))
    {
        // Create a array/vector of words from single line
        vector<string> words = spilt(line);
        
        // word[1] (2nd positon is the student id value. Array starts with 0, so word[1])
        // check is given student id matches the student id in current line
        if(search_param == words[1]){
            // Print the whole line
            cout << line;
            break;
        }
    }
    file.close();
}

// This function is to add new record to existing file
void add_record(){
    
    string filename;
    cout << "Enter a file name\n";
    cin >> filename;
    
    ofstream file(filename, std::ios_base::app);
    if(!file)
    {
        // Throw Exception if file does not exists
        throw runtime_error("Could not open file");
    }
    
    string student_id, first_name, last_name, grade;
    cout << "Enter Student ID, First Name, Last Name, Grade\n";
    cin >> student_id >> first_name >> last_name >> grade;
    
    // Add the contents to the file
    file << "Student_ID " << student_id << " First_name " << first_name << " Last_name " << last_name << " Grade " << grade << endl;
    file.close();
}

// This is main function
int main() {
    
    // variable which keep hold of the option selected
    int option = 0;
    do {
        show_menu();
        cin >> option;
        
        switch (option){
            case 1:
                create_new_book();
                break;
            case 2:
                search_book();
                break;
            case 3:
                add_record();
                break;
            case 4:
                break;
            default:
                cout << "You entered a wrong option\n";
        }
    }while (option != 4);
   return 0;
}
Add a comment
Know the answer?
Add Answer to:
: Files and Exceptions: Write a program to create and use a grade book for a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • How can I create Loops and Files on Java? • Create a program that reads a...

    How can I create Loops and Files on Java? • Create a program that reads a list of names from a source file and writes those names to a CSV file. The source file name and target CSV file name should be requested from the user • The source file can have a variable number of names so your program should be dynamic enough to read as many names as needed • When writing your CSV file, the first row...

  • (Create a text file) Write a program to create a file named Exercise17_01.txt if it does...

    (Create a text file) Write a program to create a file named Exercise17_01.txt if it does not exist. Append new data to it if it already exists. Write 150 integers created randomly into the file using text I/O. Integers are separated by a space.

  • Your assignment is to write a grade book for a teacher. The teacher has a text file, which includ...

    Your assignment is to write a grade book for a teacher. The teacher has a text file, which includes student's names, and students test grades. There are four test scores for each student. Here is an example of such a file: Count: 5 Sally 78.0 84.0 79.0 86.0 Rachel 68.0 76.0 87.0 76.0 Melba 87.0 78.0 98.0 88.0 Grace 76.0 67.0 89.0 0.0 Lisa 68.0 76.0 65.0 87.0 The first line of the file will indicate the number of students...

  • Java program Program: Grade Stats In this program you will create a utility to calculate and...

    Java program Program: Grade Stats In this program you will create a utility to calculate and display various statistics about the grades of a class. In particular, you will read a CSV file (comma separated value) that stores the grades for a class, and then print out various statistics, either for the whole class, individual assignments, or individual students Things you will learn Robustly parsing simple text files Defining your own objects and using them in a program Handling multiple...

  • You will create a class to keep student's information: name, student ID, and grade. The program...

    You will create a class to keep student's information: name, student ID, and grade. The program will have the following functionality: - The record is persistent, that is, the whole registry should be saved on file upon exiting the program, and after any major change. - The program should provide the option to create a new entry with a student's name, ID, and grade. - There should be an option to lookup a student from his student ID (this will...

  • (Create a binary data file) Write a program to create a file named CreateBinaryFile.dat if it...

    (Create a binary data file) Write a program to create a file named CreateBinaryFile.dat if it does not exist. Append new data to it if it already exists. Write 50 integers created randomly into the file using binary I/O.

  • Really need help from 11 on: Create the directory structure IFT383FinalExam/Activities/Activity1 in your home directory. Using...

    Really need help from 11 on: Create the directory structure IFT383FinalExam/Activities/Activity1 in your home directory. Using the cat command, create a file named classRoster with the following fields, separated by a comma. Student ID First Name Last Name Grade Program of Study ASURITE ID (username) Add three records to your file. Display the contents of the file. Move the file classRoster to the directory Activity1. Go to the Activity1 directory. Display the directory you are in. Add read, write and...

  • Write a contacts database program that presents the user with a menu that allows the user...

    Write a contacts database program that presents the user with a menu that allows the user to select between the following options: (In Java) Save a contact. Search for a contact. Print all contacts out to the screen. Quit If the user selects the first option, the user is prompted to enter a person's name and phone number which will get saved at the end of a file named contacts.txt. If the user selects the second option, the program prompts...

  • ****THIS IS A 2 PART QUESTION! I ONLY NEED THE ANSWER TO PART 2 ON HOW...

    ****THIS IS A 2 PART QUESTION! I ONLY NEED THE ANSWER TO PART 2 ON HOW TO SEND THE DATA SERIALIZED**** Write a c# program that stores student grades in a text file and read grades from a text file.  The program has the following GUI: There are four buttons: Create File, Save Case, Close File and Load File.  Initially, only the Create File and Load File buttons are enabled.  If the user clicks the Create File button, a Save File Dialog window...

  • Write a program that performs the following: 1. Presents the user a menu where they choose...

    Write a program that performs the following: 1. Presents the user a menu where they choose between:              a. Add a new student to the class                           i. Prompts for first name, last name                           ii. If assignments already exist, ask user for new student’s scores to assignments              b. Assign grades for a new assignment                           i. If students already exist, prompt user with student name, ask them for score                           ii. Students created after assignment will need to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT