SlideShare a Scribd company logo
1 of 12
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <conio.h>
using namespace std;
//Function reads characters from the user and controls that user
enters integer number
bool read_integer(istream& fin,int &number);
//Function loads array from txt file of special format
void load_from_file(int a[],const char filename[]);
//Function save array to txt file of special format
void save_to_file(int a[],const char filename[]);
//Prints array and the active element
void print_array(int a[]);
//Function prints menu of the program
void print_menu();
//Function will insert an integer before the selected integer and
makes the newly inserted integer active.
void insert(int a[],int num_array);
//Function deletes the active integer.
void delete_active(int a[]);
//Function sorts the list in ascending order
void sort(int a[]);
//Function selects the next integer in the list.
void select(int a[]);
//Function moves the selected integer one position to the right
void move_right(int a[]);
//Function moves the selected integer one position to the left
void move_left(int a[]);
int main()
{
const int MAX_LENGTH = 10; // defines the maximum number
of elements in the third string
int values[MAX_LENGTH+2];
char scanCode;
// load information from the file
load_from_file(values,"numbers.txt");
// cycle for user dialog
do
{
// print menu and array
print_menu();
print_array(values);
scanCode = getch(); // read pressed key
if (scanCode == ' ')
scanCode = getch(); // clean ' '
if ((scanCode == 0)||(scanCode == 224)) // for extended control
keys
scanCode = getch();
switch (scanCode)
{
case '1': // insert integer before active ('1' or 'insert')
case 82:
insert(values,MAX_LENGTH);
break;
case '2': // delete active ('2' or 'delete')
case 83:
delete_active(values);
break;
case '3': // sort values (3 or F2)
case 60 :
sort(values);
break;
case '4': // select element ('4' or 'down arrow')
case 80:
select(values);
break;
case '5': // move right ('5' or 'right arrow')
case 77:
move_right(values);
break;
case '6': // move left ('6' or 'left arrow')
case 75:
move_left(values);
break;
case '7': // exit the program ('7' or 'F1')
case 59:
// save array to file
save_to_file(values,"numbers.txt");
//exit
cout<<"End of program"<<endl;
return 0;
break;
}
} while ((scanCode!='7')&&(scanCode!=59)); // 59 is 'F1'
code
return 0;
}
void new_line(istream& fin)
{
char symbol;
do
{
fin.get(symbol);
} while (symbol != ' ');
}
bool read_integer(istream& fin,int &number)
{
char symbol;
number = 0; // initialize the number
bool good_int;
good_int = true;
fin.get(symbol);
if (symbol == ' ')
fin.get(symbol); // clean if we have ' '
while (symbol !=' ')
{
if (isdigit(symbol)) // correction of the number
number = number*10 + (symbol-'0');
else // user enters forbidden symbol
good_int = false;
fin.get(symbol);
}
return good_int;
}
void load_from_file(int a[],const char filename[])
{
ifstream fin;
fin.open(filename);
if (fin.fail( ))
{
cout << "Input file opening failed. ";
exit(1);
}
fin >> a[0]; // read number of elements in the list
fin >> a[1]; // read index of active element
// read elements of the list
for (int i=0; i<a[0]; i++) // first 2 elements are length and active
element
{ // so elements of 3rd line in
array[2],array[3]...array[MAX_LENGTH+1]
fin >> a[i+2];
}
fin.close(); // close stream
return;
}
void save_to_file(int a[],const char filename[])
{
ofstream fout;
fout.open(filename);
if (fout.fail())
{
cout << "Output file opening failed. ";
exit(1);
}
fout << a[0] << endl; // write length of list to file
fout<< a[1] << endl; // write index if active element to file
// write elements of list to file, separated by space character
for (int i = 0; i < a[0]; i++)
fout<<a[i+2]<<' ';
fout<<endl;
fout.close(); //close out file
return;
}
void print_array(int a[])
{
cout<<endl;
// check length of the array
if (a[0] == 0)
cout<<"empty";
for (int i = 1; i <= a[0]; i++)
if (a[1]== i) // number of active element
cout<<"["<<a[i+1]<<"] ";
else
cout<< a[i+1]<< " ";
// array[1] indicates the number of active element in line,
starting from 1
cout<<endl;
}
void print_menu()
{
// clear screen system("cls");
system("cls"); // for(int i=0;i<25;i++) cout<<endl;
cout.setf(ios::left);
cout <<setw(15)<<"1.Insert"<<""Insert" key"<<endl;
cout <<setw(15)<<"2.Delete"<<""Delete" key "<<endl;
cout <<setw(15)<<"3.Sort"<<""F2" key "<<endl;
cout <<setw(15)<<"4.Select"<<""Down Arrow" key
"<<endl;
cout <<setw(15)<<"5.Move Right"<<""Right Arrow" key
"<<endl;
cout <<setw(15)<<"6.Move Left"<<""Left Arrow" key
"<<endl;
cout <<setw(15)<<"7.Exit"<<""F1" key"<<endl<<endl;
}
void insert(int a[],int num_array)
{
int value;
bool result;
// checking, that list is full
if (a[0]==num_array)
{
cout<<"List of integers is full. Insertion is not
possible."<<endl;
return;
}
do
{
cout<<"Enter inserted integer : ";
result = read_integer(cin,value); // read integer with control
if (!result)
cout<<"You entered a wrong integer value"<<endl;
} while (!result);
// shift all elements after active one position right
for (int i = a[0]; i >= a[1]; i--)
a[i+2] = a[i+1]; // +2 correction (first two elements are amount
and active element)
// check if array was empty (active was not selected)
if (a[1] == 0)
a[1] = 1;
// insert new element at active position
a[a[1]+1] = value;
// increase the total amount of elements in array
a[0] ++;
return;
}
void delete_active(int a[])
{
if (a[0]==0)
{
cout<<"List of integers is empty. Deletion is not
possible."<<endl;
return;
}
// shift active element and all elements after active one position
left
for (int i= a[1]; i < a[0]; i++)
a[i+1] = a[i+2];
// decrease the total amount of elements in array
a[0] --;
// correct the index of active element
if (a[1]>a[0])
a[1] = a[0];
return;
}
void sort(int a[])
{
if (a[0]<=1) // sorting illegal (one, or none elements in the list)
return;
int active = a[ a[1] + 1 ]; // save active integer element
// bubble sort of elements array[2]...array[ array[0] ]
for (int i = a[0] + 1 ; i > 2; i--)
for (int j = 2; j < i; j++)
if (a[j] > a[j+1])
{
int temp = a[j+1];
a[j+1] = a[j];
a[j] = temp;
}
// search new index of saved active integer
for (int i = 2; i <= a[0] + 1; i++)
if (a[i] == active)
{
a[1] = i - 1; // write index of active integer
break;
}
return;
}
void select(int a[])
{
if (a[0]<=1) // selection illegal (one, or none elements in the
list)
return;
// if the last integer is selected
if (a[1] == a[0])
a[1] = 1; // select the first item in the array
else
a[1]++; // select next element in the list
return;
}
void move_right(int a[])
{
if (a[1]==a[0]) // move right is illegal (if the last integer is
selected)
return;
int index = a[1]; // index of active element
// swap active element (index in array[1]) with the next element
int tmp = a[index+1];
a[index+1] = a[index+2];
a[index+2] = tmp;
// correct index of active element
a[1]++;
return;
}
void move_left(int a[])
{
// if the first integer is selected (array[1] = 1) or there is not
selected element (array[1] = 0)
if (a[1]<=1) // move left is illegal
return;
int index = a[1]; // index of active element
// swap active element (index in array[1]) with the previous)
int tmp = a[index];
a[index] = a[index+1];
a[index+1] = tmp;
// correct index of active element
a[1]--;
return;
}
bool read_integer(istream& fin,int &number)
{
char symbol;
number = 0; // initialize the number
bool good_int;
good_int = true;
fin.get(symbol);
if (symbol == ' ')
fin.get(symbol); // clean if we have ' '
while (symbol !=' ')
{
if (isdigit(symbol)) // correction of the number
number = number*10 + (symbol-'0');
else // user enters forbidden symbol
good_int = false;
fin.get(symbol);
}
return good_int;
}
I can input all positive intergal in this program, but I cannot
input negative intergal. this program require that i just allow to
input integer, and i need to input both negative and positive
integer. what should I do? CAUsers Roder Desktopl Project
2.exe Insert" key "Delete" key F2" key Down Arrow" key 1.
Insert 2. Delete 3. Sort 4. Select 5. Move Right "Right Arrow"
key 6. Move Left 7. Exit "Left Arrow" key "F1" key -8-3 0 1
5 [12] 22 55 66 Enter inserted integer 8 You entered a wrong
integer value Enter inserted integer:
Solution
The below function has an actual problem in reading negative
numbers. The function is designed to check the weather the
entered number is positive or not a number.
It reads each and every character and checks weather it is digit
or not if it's not a digit then function return false to the insert
function.
If you enter 345 as input then reads 3,4 and 5 individually and
checks digit or not. In your case if you enter -8 function reads
'-' and 8 individually. '-' is not a digit hence your function
return false.
Here just modify the function as given below
bool read_integer(istream& fin,int &number)
{
char symbol;
number = 0; // initialize the number
bool good_int;
good_int = true;
fin.get(symbol);
if (symbol == ' ')
fin.get(symbol); // clean if we have ' '
int count=0; // count variable to check first char '-' or not
int negNumber=0;
while (symbol !=' ')
{
if(count==0) // checking first character weather enterd '-' or
not
{
if(symbol=='-') // if it's negative number
{
fin.get(symbol); // next char
negNumber=1; // detecting negative number
count++; //incrementing count
}
else //if it's positive normal operation
{
if (isdigit(symbol)) // correction of the number
number = number*10 + (symbol-'0');
else // user enters forbidden symbol
good_int = false;
fin.get(symbol);
count++;
}
}
else // for the next chars as usual operation
{
if (isdigit(symbol)) // correction of the number
number = number*10 + (symbol-'0');
else // user enters forbidden symbol
good_int = false;
fin.get(symbol);
count++;
}
}
if(negNumber==1)
number=number-(2*number); //loading negative number using
basic formula ; i.e 10-2*10=-10;
return good_int;
}

More Related Content

Similar to #include fstream#include iostream#include cstdlib#includ.docx

Please answer the 4 questions using C- The expected output is shown be.docx
Please answer the 4 questions using C- The expected output is shown be.docxPlease answer the 4 questions using C- The expected output is shown be.docx
Please answer the 4 questions using C- The expected output is shown be.docxcgraciela1
 
Can you give an example of a binary heap programCan you give an .pdf
Can you give an example of a binary heap programCan you give an .pdfCan you give an example of a binary heap programCan you give an .pdf
Can you give an example of a binary heap programCan you give an .pdfarorasales234
 
-- Reminder that your file name is incredibly important- Please do not.docx
-- Reminder that your file name is incredibly important- Please do not.docx-- Reminder that your file name is incredibly important- Please do not.docx
-- Reminder that your file name is incredibly important- Please do not.docxAdamq0DJonese
 
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...GkhanGirgin3
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...ssuserd6b1fd
 
operating system ubuntu,linux,MacProgram will work only if you g.pdf
operating system ubuntu,linux,MacProgram will work only if you g.pdfoperating system ubuntu,linux,MacProgram will work only if you g.pdf
operating system ubuntu,linux,MacProgram will work only if you g.pdfaptcomputerzone
 
C++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdfC++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdfRahul04August
 
operating system Linux,ubuntu,Mac#include stdio.h #include .pdf
operating system Linux,ubuntu,Mac#include stdio.h #include .pdfoperating system Linux,ubuntu,Mac#include stdio.h #include .pdf
operating system Linux,ubuntu,Mac#include stdio.h #include .pdfaquazac
 
Swift 5.1 Language Guide Notes.pdf
Swift 5.1 Language Guide Notes.pdfSwift 5.1 Language Guide Notes.pdf
Swift 5.1 Language Guide Notes.pdfJkPoppy
 
C++ Course - Lesson 2
C++ Course - Lesson 2C++ Course - Lesson 2
C++ Course - Lesson 2Mohamed Ahmed
 
VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2YOGESH SINGH
 
#include stdafx.h using namespace std; #include stdlib.h.docx
#include stdafx.h using namespace std; #include stdlib.h.docx#include stdafx.h using namespace std; #include stdlib.h.docx
#include stdafx.h using namespace std; #include stdlib.h.docxajoy21
 
Write a program that obtains the execution time of selection sort, bu.pdf
Write a program that obtains the execution time of selection sort, bu.pdfWrite a program that obtains the execution time of selection sort, bu.pdf
Write a program that obtains the execution time of selection sort, bu.pdfarri2009av
 
CBSE Class XII Comp sc practical file
CBSE Class XII Comp sc practical fileCBSE Class XII Comp sc practical file
CBSE Class XII Comp sc practical filePranav Ghildiyal
 

Similar to #include fstream#include iostream#include cstdlib#includ.docx (20)

Please answer the 4 questions using C- The expected output is shown be.docx
Please answer the 4 questions using C- The expected output is shown be.docxPlease answer the 4 questions using C- The expected output is shown be.docx
Please answer the 4 questions using C- The expected output is shown be.docx
 
Can you give an example of a binary heap programCan you give an .pdf
Can you give an example of a binary heap programCan you give an .pdfCan you give an example of a binary heap programCan you give an .pdf
Can you give an example of a binary heap programCan you give an .pdf
 
c programming
c programmingc programming
c programming
 
week-15x
week-15xweek-15x
week-15x
 
-- Reminder that your file name is incredibly important- Please do not.docx
-- Reminder that your file name is incredibly important- Please do not.docx-- Reminder that your file name is incredibly important- Please do not.docx
-- Reminder that your file name is incredibly important- Please do not.docx
 
Arrays
ArraysArrays
Arrays
 
week-16x
week-16xweek-16x
week-16x
 
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
 
operating system ubuntu,linux,MacProgram will work only if you g.pdf
operating system ubuntu,linux,MacProgram will work only if you g.pdfoperating system ubuntu,linux,MacProgram will work only if you g.pdf
operating system ubuntu,linux,MacProgram will work only if you g.pdf
 
C++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdfC++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdf
 
operating system Linux,ubuntu,Mac#include stdio.h #include .pdf
operating system Linux,ubuntu,Mac#include stdio.h #include .pdfoperating system Linux,ubuntu,Mac#include stdio.h #include .pdf
operating system Linux,ubuntu,Mac#include stdio.h #include .pdf
 
Swift 5.1 Language Guide Notes.pdf
Swift 5.1 Language Guide Notes.pdfSwift 5.1 Language Guide Notes.pdf
Swift 5.1 Language Guide Notes.pdf
 
DAA Lab Work.docx
DAA Lab Work.docxDAA Lab Work.docx
DAA Lab Work.docx
 
C++ Course - Lesson 2
C++ Course - Lesson 2C++ Course - Lesson 2
C++ Course - Lesson 2
 
Arrays
ArraysArrays
Arrays
 
VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2
 
#include stdafx.h using namespace std; #include stdlib.h.docx
#include stdafx.h using namespace std; #include stdlib.h.docx#include stdafx.h using namespace std; #include stdlib.h.docx
#include stdafx.h using namespace std; #include stdlib.h.docx
 
Write a program that obtains the execution time of selection sort, bu.pdf
Write a program that obtains the execution time of selection sort, bu.pdfWrite a program that obtains the execution time of selection sort, bu.pdf
Write a program that obtains the execution time of selection sort, bu.pdf
 
CBSE Class XII Comp sc practical file
CBSE Class XII Comp sc practical fileCBSE Class XII Comp sc practical file
CBSE Class XII Comp sc practical file
 

More from ajoy21

Please complete the assignment listed below.Define and explain, us.docx
Please complete the assignment listed below.Define and explain, us.docxPlease complete the assignment listed below.Define and explain, us.docx
Please complete the assignment listed below.Define and explain, us.docxajoy21
 
Please cite sources for each question. Do not use the same sources f.docx
Please cite sources for each question. Do not use the same sources f.docxPlease cite sources for each question. Do not use the same sources f.docx
Please cite sources for each question. Do not use the same sources f.docxajoy21
 
Please choose one of the following questions to answer for this week.docx
Please choose one of the following questions to answer for this week.docxPlease choose one of the following questions to answer for this week.docx
Please choose one of the following questions to answer for this week.docxajoy21
 
Please check the attachment for my paper.Please add citations to a.docx
Please check the attachment for my paper.Please add citations to a.docxPlease check the attachment for my paper.Please add citations to a.docx
Please check the attachment for my paper.Please add citations to a.docxajoy21
 
Please answer to this discussion post. No less than 150 words. Refer.docx
Please answer to this discussion post. No less than 150 words. Refer.docxPlease answer to this discussion post. No less than 150 words. Refer.docx
Please answer to this discussion post. No less than 150 words. Refer.docxajoy21
 
Please attach Non-nursing theorist summaries.JigsawExecutive .docx
Please attach Non-nursing theorist summaries.JigsawExecutive .docxPlease attach Non-nursing theorist summaries.JigsawExecutive .docx
Please attach Non-nursing theorist summaries.JigsawExecutive .docxajoy21
 
Please answer the question .There is no work count. PLEASE NUMBER .docx
Please answer the question .There is no work count. PLEASE NUMBER .docxPlease answer the question .There is no work count. PLEASE NUMBER .docx
Please answer the question .There is no work count. PLEASE NUMBER .docxajoy21
 
Please answer the following questions. Please cite your references..docx
Please answer the following questions. Please cite your references..docxPlease answer the following questions. Please cite your references..docx
Please answer the following questions. Please cite your references..docxajoy21
 
Please answer the following questions.1.      1.  Are you or.docx
Please answer the following questions.1.      1.  Are you or.docxPlease answer the following questions.1.      1.  Are you or.docx
Please answer the following questions.1.      1.  Are you or.docxajoy21
 
Please answer the following question with 200-300 words.Q. Discu.docx
Please answer the following question with 200-300 words.Q. Discu.docxPlease answer the following question with 200-300 words.Q. Discu.docx
Please answer the following question with 200-300 words.Q. Discu.docxajoy21
 
Please answer the following question Why do you think the US ha.docx
Please answer the following question Why do you think the US ha.docxPlease answer the following question Why do you think the US ha.docx
Please answer the following question Why do you think the US ha.docxajoy21
 
Please answer the following questions. Define tunneling in the V.docx
Please answer the following questions. Define tunneling in the V.docxPlease answer the following questions. Define tunneling in the V.docx
Please answer the following questions. Define tunneling in the V.docxajoy21
 
Please answer the following questions1. How can you stimulate the.docx
Please answer the following questions1. How can you stimulate the.docxPlease answer the following questions1. How can you stimulate the.docx
Please answer the following questions1. How can you stimulate the.docxajoy21
 
Please answer the following questions very deeply and presicely .docx
Please answer the following questions very deeply and presicely .docxPlease answer the following questions very deeply and presicely .docx
Please answer the following questions very deeply and presicely .docxajoy21
 
Please answer the following questions in an informal 1 ½ - 2-page es.docx
Please answer the following questions in an informal 1 ½ - 2-page es.docxPlease answer the following questions in an informal 1 ½ - 2-page es.docx
Please answer the following questions in an informal 1 ½ - 2-page es.docxajoy21
 
Please answer the following questions in a response of 150 to 200 wo.docx
Please answer the following questions in a response of 150 to 200 wo.docxPlease answer the following questions in a response of 150 to 200 wo.docx
Please answer the following questions in a response of 150 to 200 wo.docxajoy21
 
Please answer these questions regarding the (TILA) Truth in Lending .docx
Please answer these questions regarding the (TILA) Truth in Lending .docxPlease answer these questions regarding the (TILA) Truth in Lending .docx
Please answer these questions regarding the (TILA) Truth in Lending .docxajoy21
 
Please answer the following question pertaining to psychology. Inc.docx
Please answer the following question pertaining to psychology. Inc.docxPlease answer the following question pertaining to psychology. Inc.docx
Please answer the following question pertaining to psychology. Inc.docxajoy21
 
Please answer the following questions in a response of 250 to 300 .docx
Please answer the following questions in a response of 250 to 300 .docxPlease answer the following questions in a response of 250 to 300 .docx
Please answer the following questions in a response of 250 to 300 .docxajoy21
 
Please answer the three questions completly. I have attached the que.docx
Please answer the three questions completly. I have attached the que.docxPlease answer the three questions completly. I have attached the que.docx
Please answer the three questions completly. I have attached the que.docxajoy21
 

More from ajoy21 (20)

Please complete the assignment listed below.Define and explain, us.docx
Please complete the assignment listed below.Define and explain, us.docxPlease complete the assignment listed below.Define and explain, us.docx
Please complete the assignment listed below.Define and explain, us.docx
 
Please cite sources for each question. Do not use the same sources f.docx
Please cite sources for each question. Do not use the same sources f.docxPlease cite sources for each question. Do not use the same sources f.docx
Please cite sources for each question. Do not use the same sources f.docx
 
Please choose one of the following questions to answer for this week.docx
Please choose one of the following questions to answer for this week.docxPlease choose one of the following questions to answer for this week.docx
Please choose one of the following questions to answer for this week.docx
 
Please check the attachment for my paper.Please add citations to a.docx
Please check the attachment for my paper.Please add citations to a.docxPlease check the attachment for my paper.Please add citations to a.docx
Please check the attachment for my paper.Please add citations to a.docx
 
Please answer to this discussion post. No less than 150 words. Refer.docx
Please answer to this discussion post. No less than 150 words. Refer.docxPlease answer to this discussion post. No less than 150 words. Refer.docx
Please answer to this discussion post. No less than 150 words. Refer.docx
 
Please attach Non-nursing theorist summaries.JigsawExecutive .docx
Please attach Non-nursing theorist summaries.JigsawExecutive .docxPlease attach Non-nursing theorist summaries.JigsawExecutive .docx
Please attach Non-nursing theorist summaries.JigsawExecutive .docx
 
Please answer the question .There is no work count. PLEASE NUMBER .docx
Please answer the question .There is no work count. PLEASE NUMBER .docxPlease answer the question .There is no work count. PLEASE NUMBER .docx
Please answer the question .There is no work count. PLEASE NUMBER .docx
 
Please answer the following questions. Please cite your references..docx
Please answer the following questions. Please cite your references..docxPlease answer the following questions. Please cite your references..docx
Please answer the following questions. Please cite your references..docx
 
Please answer the following questions.1.      1.  Are you or.docx
Please answer the following questions.1.      1.  Are you or.docxPlease answer the following questions.1.      1.  Are you or.docx
Please answer the following questions.1.      1.  Are you or.docx
 
Please answer the following question with 200-300 words.Q. Discu.docx
Please answer the following question with 200-300 words.Q. Discu.docxPlease answer the following question with 200-300 words.Q. Discu.docx
Please answer the following question with 200-300 words.Q. Discu.docx
 
Please answer the following question Why do you think the US ha.docx
Please answer the following question Why do you think the US ha.docxPlease answer the following question Why do you think the US ha.docx
Please answer the following question Why do you think the US ha.docx
 
Please answer the following questions. Define tunneling in the V.docx
Please answer the following questions. Define tunneling in the V.docxPlease answer the following questions. Define tunneling in the V.docx
Please answer the following questions. Define tunneling in the V.docx
 
Please answer the following questions1. How can you stimulate the.docx
Please answer the following questions1. How can you stimulate the.docxPlease answer the following questions1. How can you stimulate the.docx
Please answer the following questions1. How can you stimulate the.docx
 
Please answer the following questions very deeply and presicely .docx
Please answer the following questions very deeply and presicely .docxPlease answer the following questions very deeply and presicely .docx
Please answer the following questions very deeply and presicely .docx
 
Please answer the following questions in an informal 1 ½ - 2-page es.docx
Please answer the following questions in an informal 1 ½ - 2-page es.docxPlease answer the following questions in an informal 1 ½ - 2-page es.docx
Please answer the following questions in an informal 1 ½ - 2-page es.docx
 
Please answer the following questions in a response of 150 to 200 wo.docx
Please answer the following questions in a response of 150 to 200 wo.docxPlease answer the following questions in a response of 150 to 200 wo.docx
Please answer the following questions in a response of 150 to 200 wo.docx
 
Please answer these questions regarding the (TILA) Truth in Lending .docx
Please answer these questions regarding the (TILA) Truth in Lending .docxPlease answer these questions regarding the (TILA) Truth in Lending .docx
Please answer these questions regarding the (TILA) Truth in Lending .docx
 
Please answer the following question pertaining to psychology. Inc.docx
Please answer the following question pertaining to psychology. Inc.docxPlease answer the following question pertaining to psychology. Inc.docx
Please answer the following question pertaining to psychology. Inc.docx
 
Please answer the following questions in a response of 250 to 300 .docx
Please answer the following questions in a response of 250 to 300 .docxPlease answer the following questions in a response of 250 to 300 .docx
Please answer the following questions in a response of 250 to 300 .docx
 
Please answer the three questions completly. I have attached the que.docx
Please answer the three questions completly. I have attached the que.docxPlease answer the three questions completly. I have attached the que.docx
Please answer the three questions completly. I have attached the que.docx
 

Recently uploaded

Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptxPoojaSen20
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 

Recently uploaded (20)

Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptx
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 

#include fstream#include iostream#include cstdlib#includ.docx

  • 1. #include <fstream> #include <iostream> #include <cstdlib> #include <iomanip> #include <conio.h> using namespace std; //Function reads characters from the user and controls that user enters integer number bool read_integer(istream& fin,int &number); //Function loads array from txt file of special format void load_from_file(int a[],const char filename[]); //Function save array to txt file of special format void save_to_file(int a[],const char filename[]); //Prints array and the active element void print_array(int a[]); //Function prints menu of the program void print_menu(); //Function will insert an integer before the selected integer and makes the newly inserted integer active. void insert(int a[],int num_array); //Function deletes the active integer. void delete_active(int a[]); //Function sorts the list in ascending order void sort(int a[]); //Function selects the next integer in the list. void select(int a[]); //Function moves the selected integer one position to the right void move_right(int a[]); //Function moves the selected integer one position to the left void move_left(int a[]); int main() { const int MAX_LENGTH = 10; // defines the maximum number of elements in the third string int values[MAX_LENGTH+2];
  • 2. char scanCode; // load information from the file load_from_file(values,"numbers.txt"); // cycle for user dialog do { // print menu and array print_menu(); print_array(values); scanCode = getch(); // read pressed key if (scanCode == ' ') scanCode = getch(); // clean ' ' if ((scanCode == 0)||(scanCode == 224)) // for extended control keys scanCode = getch(); switch (scanCode) { case '1': // insert integer before active ('1' or 'insert') case 82: insert(values,MAX_LENGTH); break; case '2': // delete active ('2' or 'delete') case 83: delete_active(values); break; case '3': // sort values (3 or F2) case 60 : sort(values); break; case '4': // select element ('4' or 'down arrow') case 80: select(values); break; case '5': // move right ('5' or 'right arrow') case 77: move_right(values);
  • 3. break; case '6': // move left ('6' or 'left arrow') case 75: move_left(values); break; case '7': // exit the program ('7' or 'F1') case 59: // save array to file save_to_file(values,"numbers.txt"); //exit cout<<"End of program"<<endl; return 0; break; } } while ((scanCode!='7')&&(scanCode!=59)); // 59 is 'F1' code return 0; } void new_line(istream& fin) { char symbol; do { fin.get(symbol); } while (symbol != ' '); } bool read_integer(istream& fin,int &number) { char symbol; number = 0; // initialize the number bool good_int; good_int = true; fin.get(symbol); if (symbol == ' ') fin.get(symbol); // clean if we have ' ' while (symbol !=' ')
  • 4. { if (isdigit(symbol)) // correction of the number number = number*10 + (symbol-'0'); else // user enters forbidden symbol good_int = false; fin.get(symbol); } return good_int; } void load_from_file(int a[],const char filename[]) { ifstream fin; fin.open(filename); if (fin.fail( )) { cout << "Input file opening failed. "; exit(1); } fin >> a[0]; // read number of elements in the list fin >> a[1]; // read index of active element // read elements of the list for (int i=0; i<a[0]; i++) // first 2 elements are length and active element { // so elements of 3rd line in array[2],array[3]...array[MAX_LENGTH+1] fin >> a[i+2]; } fin.close(); // close stream return; } void save_to_file(int a[],const char filename[]) { ofstream fout; fout.open(filename); if (fout.fail()) {
  • 5. cout << "Output file opening failed. "; exit(1); } fout << a[0] << endl; // write length of list to file fout<< a[1] << endl; // write index if active element to file // write elements of list to file, separated by space character for (int i = 0; i < a[0]; i++) fout<<a[i+2]<<' '; fout<<endl; fout.close(); //close out file return; } void print_array(int a[]) { cout<<endl; // check length of the array if (a[0] == 0) cout<<"empty"; for (int i = 1; i <= a[0]; i++) if (a[1]== i) // number of active element cout<<"["<<a[i+1]<<"] "; else cout<< a[i+1]<< " "; // array[1] indicates the number of active element in line, starting from 1 cout<<endl; } void print_menu() { // clear screen system("cls"); system("cls"); // for(int i=0;i<25;i++) cout<<endl; cout.setf(ios::left); cout <<setw(15)<<"1.Insert"<<""Insert" key"<<endl; cout <<setw(15)<<"2.Delete"<<""Delete" key "<<endl; cout <<setw(15)<<"3.Sort"<<""F2" key "<<endl; cout <<setw(15)<<"4.Select"<<""Down Arrow" key
  • 6. "<<endl; cout <<setw(15)<<"5.Move Right"<<""Right Arrow" key "<<endl; cout <<setw(15)<<"6.Move Left"<<""Left Arrow" key "<<endl; cout <<setw(15)<<"7.Exit"<<""F1" key"<<endl<<endl; } void insert(int a[],int num_array) { int value; bool result; // checking, that list is full if (a[0]==num_array) { cout<<"List of integers is full. Insertion is not possible."<<endl; return; } do { cout<<"Enter inserted integer : "; result = read_integer(cin,value); // read integer with control if (!result) cout<<"You entered a wrong integer value"<<endl; } while (!result); // shift all elements after active one position right for (int i = a[0]; i >= a[1]; i--) a[i+2] = a[i+1]; // +2 correction (first two elements are amount and active element) // check if array was empty (active was not selected) if (a[1] == 0) a[1] = 1; // insert new element at active position a[a[1]+1] = value; // increase the total amount of elements in array a[0] ++;
  • 7. return; } void delete_active(int a[]) { if (a[0]==0) { cout<<"List of integers is empty. Deletion is not possible."<<endl; return; } // shift active element and all elements after active one position left for (int i= a[1]; i < a[0]; i++) a[i+1] = a[i+2]; // decrease the total amount of elements in array a[0] --; // correct the index of active element if (a[1]>a[0]) a[1] = a[0]; return; } void sort(int a[]) { if (a[0]<=1) // sorting illegal (one, or none elements in the list) return; int active = a[ a[1] + 1 ]; // save active integer element // bubble sort of elements array[2]...array[ array[0] ] for (int i = a[0] + 1 ; i > 2; i--) for (int j = 2; j < i; j++) if (a[j] > a[j+1]) { int temp = a[j+1]; a[j+1] = a[j]; a[j] = temp; } // search new index of saved active integer
  • 8. for (int i = 2; i <= a[0] + 1; i++) if (a[i] == active) { a[1] = i - 1; // write index of active integer break; } return; } void select(int a[]) { if (a[0]<=1) // selection illegal (one, or none elements in the list) return; // if the last integer is selected if (a[1] == a[0]) a[1] = 1; // select the first item in the array else a[1]++; // select next element in the list return; } void move_right(int a[]) { if (a[1]==a[0]) // move right is illegal (if the last integer is selected) return; int index = a[1]; // index of active element // swap active element (index in array[1]) with the next element int tmp = a[index+1]; a[index+1] = a[index+2]; a[index+2] = tmp; // correct index of active element a[1]++; return; } void move_left(int a[]) {
  • 9. // if the first integer is selected (array[1] = 1) or there is not selected element (array[1] = 0) if (a[1]<=1) // move left is illegal return; int index = a[1]; // index of active element // swap active element (index in array[1]) with the previous) int tmp = a[index]; a[index] = a[index+1]; a[index+1] = tmp; // correct index of active element a[1]--; return; } bool read_integer(istream& fin,int &number) { char symbol; number = 0; // initialize the number bool good_int; good_int = true; fin.get(symbol); if (symbol == ' ') fin.get(symbol); // clean if we have ' ' while (symbol !=' ') { if (isdigit(symbol)) // correction of the number number = number*10 + (symbol-'0'); else // user enters forbidden symbol good_int = false; fin.get(symbol); } return good_int; } I can input all positive intergal in this program, but I cannot input negative intergal. this program require that i just allow to input integer, and i need to input both negative and positive integer. what should I do? CAUsers Roder Desktopl Project
  • 10. 2.exe Insert" key "Delete" key F2" key Down Arrow" key 1. Insert 2. Delete 3. Sort 4. Select 5. Move Right "Right Arrow" key 6. Move Left 7. Exit "Left Arrow" key "F1" key -8-3 0 1 5 [12] 22 55 66 Enter inserted integer 8 You entered a wrong integer value Enter inserted integer: Solution The below function has an actual problem in reading negative numbers. The function is designed to check the weather the entered number is positive or not a number. It reads each and every character and checks weather it is digit or not if it's not a digit then function return false to the insert function. If you enter 345 as input then reads 3,4 and 5 individually and checks digit or not. In your case if you enter -8 function reads '-' and 8 individually. '-' is not a digit hence your function return false. Here just modify the function as given below bool read_integer(istream& fin,int &number) { char symbol; number = 0; // initialize the number bool good_int;
  • 11. good_int = true; fin.get(symbol); if (symbol == ' ') fin.get(symbol); // clean if we have ' ' int count=0; // count variable to check first char '-' or not int negNumber=0; while (symbol !=' ') { if(count==0) // checking first character weather enterd '-' or not { if(symbol=='-') // if it's negative number { fin.get(symbol); // next char negNumber=1; // detecting negative number count++; //incrementing count } else //if it's positive normal operation { if (isdigit(symbol)) // correction of the number number = number*10 + (symbol-'0'); else // user enters forbidden symbol good_int = false; fin.get(symbol); count++;
  • 12. } } else // for the next chars as usual operation { if (isdigit(symbol)) // correction of the number number = number*10 + (symbol-'0'); else // user enters forbidden symbol good_int = false; fin.get(symbol); count++; } } if(negNumber==1) number=number-(2*number); //loading negative number using basic formula ; i.e 10-2*10=-10; return good_int; }