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

Your goal it to implement an application that reads a file, modify its content.

ID: 3841028 • Letter: Y

Question

Your goal it to implement an application that reads a file, modify its content. and writes the modification back to the same file. The file includes 3 lines of integers The first line indicates the number of integers on the third line. The second line indicates which integer on the third line has been selected (active) And the thud line lists all the integers Your application should have a menu that is constantly displayed on the screen (see the menu section below) Right below the menu, the program should display list of all integers m the file (third line). Also it should show which integer is currently selected (active). The user should be able to select a menu item by entering its associated number or by pressing one of the indicated extended keys on the keyboard. "Invert" will invert an integer before the selected integer and makes the newly inverted integer active. The integer is typed by the user. If the list is full, insertion will not be possible "Delete deletes the active integer "Sort" sorts the list in ascending order The active integer after the sort is same as the active integer before the sort. "Select selects the next integer in the list. If the last integer is selected, this option selects the first item in the list. "Move right" moves the selected integer one position to the right If the last integer is selected, moving right will not be possible. "Move left" moves the selected integer one position to the left If the first integer is selected, moving left will not be possible. "Exit" ends the application Make sure to use the top-down design to break your program to many simpler tasks (at least one function per task) Do not use global variables. Make sure to check the special cases Your program should save the content of the file into an array, modify the array, and write back the content of the array into the file. Make sure to test your program completely before submission. Do not forget to add comments. Submit your well-documented C++ program via Canvas. Menu: 1. Insert "Insert" key 2. Delete "Delete" key 3. Sort "F2" key 4. Select "Down Arrow" key 5. Move Right " rightarrow " key ó. Move Left "Left Arrow" key 7. Exit "F1" key

Explanation / Answer

                 
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <conio.h>
#include <cctype>
using namespace std;
// displays the menu for user selection
void display_menu();
void display_numbers(int num_list1[1],int num_list2[1],int num_list3[],int num_items, int activeLocation);
// display the array of numbers with active location selected
void firstLine(ifstream& inputfile, int num_list1[1], int& num_items);
// get the first integer from from first line of the file to use to set initial number of items in the array
void secondLine(ifstream& inputfile, int num_list2[1], int& activeLocation);
// get the first integer from the second line of the file to get the initial active location in the array
void thirdLine(ifstream& inputfile, int num_list3[], int num_items);
// get maximum of 10 integers from the third line of the file
void moveRight(int& activeLocation, int num_items);
// moves the active location to the right and cannot go past the last integer
void moveLeft (int& activeLocation);
// moves the active location to the left and cannot go past the first integer
void insertNumber(int& activeLocation, int num_list3[], int num_list1[1], int& num_items);
// inserts a number into the array and make that the active location and cannot insert when the array is full
void deleteNumber(int& activeLocation, int num_list3[], int num_list1[1], int& num_items);
// deletes a number from the array that is the active location and makes the next number on the right the active location
void sortNumber(int& activeLocation, int num_list3[], int num_items);
// sorts the number from least to greatest and the active location follow the active location selected before the sort.
void selectNumber(int& activeLocation, int num_items);
// moves the active location to the right and once reach the end of the program the active location starts from the beginning integer
void checkInput(int& input);
// if the input is valid
void writeFile (ofstream& inputfile,int num_list3[],int num_items, int activeLocation);
// write the new numbers back into the file
int main()
{
    // declaration of variables and array
    ifstream inputfile;
    ofstream outputfile;
    string newlineCheck;
    int choice;
    int num_list1[1] = {0}, num_list2[1], num_list3[10], num_items = 0 , activeLocation = 0, scanCode1, scanCode2;
    inputfile.open("test.txt");
    cout << "Hi this program reads a file and takes in integers and being able to insert, delete, sort, or change the active location of an integer. "
    << "Also, being able to write the changes made back into the file.";
    // check if the fail can be opened
    if (inputfile.fail())
    {
        cout << "Input file opening failed" << endl;
        system("pause");
        exit(1);
    }
    firstLine(inputfile,num_list1,num_items); // call function to get number of items from file
    getline(inputfile,newlineCheck); // checks the second line
    secondLine(inputfile,num_list2,activeLocation); // call function to get the active location
    getline(inputfile,newlineCheck); // checks the third line
    thirdLine(inputfile,num_list3,num_items); // call function to get the list of integers
    cout << endl;
    // repeats the menu
    do
    {
        display_menu();
        display_numbers(num_list1,num_list2,num_list3,num_items,activeLocation);
        // get input from user the selection on menu without pressing enter
        int scanCode1 = getch();
        if (scanCode1 == 0 || scanCode1 == 224)
        {
            scanCode2 = getch();
        }
        if ( scanCode1 == 49 || scanCode2 == 82) // insert a integer
        {
            insertNumber(activeLocation,num_list3,num_list1,num_items);
            system("CLS");
        }
        if (scanCode1 == 50 || scanCode2 == 83) // deletes a integer
        {
            deleteNumber(activeLocation,num_list3,num_list1,num_items);
            system("CLS");
        }
        if (scanCode1 == 51 || scanCode2 == 60 ) // sort
        {
            sortNumber(activeLocation,num_list3,num_items);
            system("CLS");
        }
        if (scanCode1 == 52 || scanCode2 == 80) // select
        {
           selectNumber(activeLocation,num_items);
           system("CLS");
        }
        if (scanCode1 == 53 || scanCode2 == 77 ) // move right
        {
            moveRight(activeLocation,num_items);
            display_numbers(num_list1,num_list2,num_list3,num_items,activeLocation);
            system("CLS");
        }
        if (scanCode1 == 54 || scanCode2 == 75) // move left
        {
            moveLeft(activeLocation);
            display_numbers(num_list1,num_list2,num_list3,num_items,activeLocation);
            system("CLS");
        }
        if (scanCode1 == 55 || scanCode2 == 59) // exit
        {
        writeFile(outputfile,num_list3,num_items,activeLocation);
        inputfile.close();
        outputfile.close();
        cout << "End of program ";
        system("pause");
        break;
        exit(0);
        }
        else
        {
            system("CLS");
        }
    }while (scanCode1 != 55 || scanCode2 != 59);
    return 0;
}
void display_menu()
{
    cout << endl;
    cout << "1.Insert      "Insert" key " << endl;
    cout << "2.Delete      "Delete" key " << endl;
    cout << "3.Sort        "F2" key " << endl;
    cout << "4.Select      "Down Arrow" key " << endl;
    cout << "5.Move Right "Right Arrow " key " << endl;
    cout << "6.Move Left   "Left Arrow " key " << endl;
    cout << "7.Exit        "F1" key " << endl;
    cout << " ";
}
void display_numbers(int num_list1[1],int num_list2[1],int num_list3[],int num_items, int activeLocation)
{

    for (int i = 0; i < num_items; i++)
    {
        if (i == activeLocation)
        {
            cout << "[" << num_list3[i] << "]" << " ";
        }
        else
        {
            cout << num_list3[i] << " ";
        }

    }
    if (num_items == 0) // when the array is empty displays a []
    {
        cout << "[]";
    }
    cout << endl << endl;
}
void firstLine(ifstream& inputfile, int num_list1[1], int& num_items)
{
    // get the first number and save as the number of items which is the size of the array
    for(int i = 0; i < 1; i++)
    {
        if (inputfile.eof())
        {
            break;
        }
        inputfile >> num_list1[i];
        num_items = num_list1[i];
    }
}
void secondLine(ifstream& inputfile, int num_list2[1], int& activeLocation)
{
     // get the first number and save as the active location
    for(int i = 0; i < 1; i++) // secondline
    {
        if (inputfile.eof())
        {
            break;
        }
        inputfile >> num_list2[i];
        activeLocation = num_list2[i];
    }
}
void thirdLine(ifstream& inputfile, int num_list3[], int num_items)
{ // maximum of 10 integer is saved into a array
     for(int i = 0; i < num_items; i++)
    {
        if (inputfile.eof())
        {
            break;
        }
        inputfile >> num_list3[i];
    }
}

void moveRight(int& activeLocation, int num_items)
{
    if (activeLocation == num_items-1)
    {
    }
    else
    {
        if (num_items != 0) // prevents active Location from increasing when there is no items
        {
            activeLocation += 1;
        }

    }

}
void moveLeft (int& activeLocation)
{
    if (activeLocation == 0)
    {

    }
    else
    {
        activeLocation -= 1;
    }
}
void insertNumber(int& activeLocation, int num_list3[], int num_list1[1], int& num_items)
{
    int input;
    int tempArray[num_items]; // temporary array
    if (num_items < 10) // only inserts a integer if less than 10 items
    {
        num_items += 1; // makes the array size bigger to account for the new integer inserted
        for (int i = 0; i < num_items; i++)
        {
            if (i == activeLocation)
            {
                 for ( i == activeLocation; i < num_items; i++)
                 {
                    tempArray[i+1] = num_list3[i]; // numbers are moved to the right after the active location
                 }
            }
            else
            {
                tempArray[i] = num_list3[i]; // numbers before the active location
            }

        }
        checkInput(input); // get input from user
        tempArray[activeLocation] = input; // set the input new number as the new active location
        for (int i = 0; i < num_items; i++) // now put the new array back into the old one.
        {
            num_list3[i] = tempArray[i];
        }
    }
}
void deleteNumber(int& activeLocation, int num_list3[], int num_list1[1], int& num_items)
{
    int tempArray[num_items];
    for (int i = 0; i < num_items; i++)
    {
        if (i == activeLocation)
        {
            for ( i == activeLocation; i < num_items; i++)
            {
                tempArray[i] = num_list3[i+1];
            }
        }
        else
        {
            tempArray[i] = num_list3[i];
        }
    }
    if (num_items > 0) // makes sure the number of items is not negative
    {
    num_items -= 1;
    }
    if (num_items == activeLocation) // make the new active location after delete the number on the right
    {
    activeLocation -= 1;
        if (num_items <= 1) // when there is 1 item or less the active location is 0
        {
            activeLocation = 0;
        }
    }
    for (int i = 0; i < num_items; i++)
    {
        num_list3[i] = tempArray[i]; // put the new array back into the old one
    }
}
void sortNumber(int& activeLocation, int num_list3[], int num_items)
{
    int temp = 0;
    int saveLocation = num_list3[activeLocation];
    for (int i = 0; (i < num_items-1);i++) // sort the number
    {
        for(int i = 0; (i < num_items-1);i++)
        {
            if (num_list3[i+1] == saveLocation) // keep the active location at the number before the sort.
            {
                activeLocation = i+1;
            }
            if (num_list3[i] > num_list3[i+1])
            {
                temp = num_list3[i];
                num_list3[i] = num_list3[i+1];
                num_list3[i+1] = temp;
            }
        }
    }
}
void selectNumber(int& activeLocation, int num_items)
{
    if (activeLocation == num_items-1) // reset back to first integer when reached the last integer
    {
        activeLocation = 0;
    }

    else
    {
        if (num_items != 0) // prevent active location from increasing when there is no items.
        {
            activeLocation += 1;
        }

    }
}
void checkInput(int& input)
{
    cout << "Please enter an integer: ";
    while(!(cin >> input))
    {
        cin.clear();
        while (cin.get() != ' ')
        {

        }
        cout << "Wrong Input. Try again: ";
    }
}
void writeFile (ofstream& outputfile,int num_list3[],int num_items, int activeLocation)
{
    outputfile.open("test.txt");
    outputfile << num_items << endl;
    outputfile << activeLocation << endl;
    for(int i = 0; i < num_items; i++)
    {
        outputfile << num_list3[i] << " ";
    }
}