This is a beginning C++ assignment that uses codeblocks to write the program. Yo
ID: 3844235 • Letter: T
Question
This is a beginning C++ assignment that uses codeblocks to write the program.
Your goal is 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 third line lists all the integers (maximum of 10). 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 in 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. “Insert” will insert an integer before the selected integer and makes the newly inserted 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. The menu should look like this...
Menu:
1.Insert "Insert" key
2.Delete "Delete" key
3.Sort "F2" key
4.Select "Down Arrow" key
5.Move Right "Right Arrow" key
6.Move Left "Left Arrow" key
7.Exit "F1" key
Thanks for the help and I will rate.
Explanation / Answer
#include<iostream>
#include<cstdlib>
#include<fstream>
#include<conio.h>
#include<dos.h>
#include <iomanip>
#include <string>
#include <windows.h>
using namespace std;
void menu();
int main()
{
int num = 0;
int next = 0;
fstream f_stream;
f_stream.open("base.txt");
if (f_stream.fail())
{
cout << "base.txt is missing. program cannot run :(" << endl;
exit(1);
}
cout << "input stream successfully opened" << endl;
int ch =0;
int values[12];
do
{
menu();
while (!f_stream.eof())
{
f_stream >> next;
values[num] = next;
num++;
}
cout << endl;
cout << "Here is the list of numbers on the third line: " << endl << endl;
for (int t = 2; t < num; t++)
{
cout << values[t] << endl;
}
cout << endl;
int num_of_integers = num - 2;
int active = values[1];
int activenumber = values[active];
cout << "Number of integers: " << values[0] << endl;
cout << "Current active integer: " << activenumber << endl;
ch=getch();
if (ch == 0||ch == 224)//
ch=getch();
int newactivenumber;
int lastorder = num - 1;
if (ch == 82)
{
if (num == 12)
{
cout << "Cannot insert an integer before " << activenumber << endl << "Please delete an integer before inserting" << endl;
}
else if (values[active] == values[3])
{
cout << "Please enter an integer to insert at the end of the number line" << endl;
cin >> values[num-1];
}
else
{
cout << "Please enter an integer to insert before " << activenumber << endl;
int previous = active - 1;
cin >> values[previous];
}
}
else if (ch == 83)
{
cout << values[active] << " was deleted." << endl;
int temp, temp1;
for (int p = active; p<(lastorder);p++)
{
temp1 = p + 1;
values[p] = values[temp1];
}
int new_integers = num_of_integers - 1;
values[0] = new_integers;
num--;
}
else if (ch == 80)
{
if (values[1] == lastorder)
{
newactivenumber = 2;
values[1] = newactivenumber;
}
else
{
newactivenumber = values[1] + 1;
values[1] = newactivenumber;
}
cout << values[newactivenumber] << " is now selected" << endl;
}
else if (ch == 77)
{
if (values[active] == values[num-1])
{
cout << "I'm sorry, Dave. I'm afraid I can't do that." << endl;
}
else
{
int temp;
temp = values[active];
int rightno = active + 1;
values[active] = values[rightno];
values[rightno] = temp;
cout << "The active number has been moved 1 position right in the list" << endl;
}
}
else if (ch == 60)
{
int i;
int temporary = values[active];
for(i=2;i<num;i++)
{
for(int j=2;j<num-i-1;j++)
{
if(values[j]>values[j+1])
{
int temp = values[j];
values[j] = values[j+1];
values[j+1] = temp;
}
}
}
cout<<"Sorted list"<<endl;
for(i=2;i<num;i++)
{
cout<<values[i] << endl;
}
for(i=2;i<num;i++)
{
if (values[i] == activenumber)
{
values[1] = i;
}
}
}
else if (ch == 75)
{
if (values[active] == values[2])
{
cout << "I'm sorry, Dave. I,'m afraid I can't do that." << endl;
}
else
{
int temp;
temp = values[active];
int leftno = active - 1;
values[active] = values[leftno];
values[leftno] = temp;
cout << "The active number has been moved 1 position left in the list" << endl;
}
}
else if (ch == 59)
{
f_stream.close();
cout << "streams successfully closed" << endl;
break;
}
else
{
cout << "Try again" << endl;
}
for (int out = 0; out < num; out++)
{
if (out == 0 || out == 1)
{
f_stream << values[out] << ' ';
}
else if (values[out]==0)
{
f_stream << "";
}
else if (out != num-1)
{
f_stream << values[out] << " ";
}
else
{
f_stream << values[out];
}
}
system("PAUSE");
system("CLS");
}
while (ch != 59);
f_stream.close();
cout << "streams successfully closed" << endl;
return 0;
}
void menu()
{
cout<<"Menu:" << endl;
cout << setiosflags(ios::left) << setw(14)<< "1.Insert" << "Press the "Insert" key" << endl;
cout << setiosflags(ios::left) << setw(14)<< "2.Delete" << "Press the "Delete" key" << endl;
cout << setiosflags(ios::left) << setw(14)<< "3.Sort" << "Press the "F2" key:" << endl;
cout << setiosflags(ios::left) << setw(14)<< "4.Select" << "Press the "Down Arrow" key" << endl;
cout << setiosflags(ios::left) << setw(14)<< "5.Move Right" << "Press the "Right Arrow" key" << endl;
cout << setiosflags(ios::left) << setw(14)<< "6.Move Left" << "Press the "Left Arrow" key" << endl;
cout << setiosflags(ios::left) << setw(14)<< "7.Exit"<< "Press the "F1" key" << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.