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

You are going to create a switch loop with different options. Option 1: Ask user

ID: 3710302 • Letter: Y

Question

You are going to create a switch loop with different options. Option 1: Ask users to input Person Name, Ticket Type and Fine. You will keep asking users until they press q key. Once user types Q, you will store all the details into the.txt file or any other file Option 2: Use the file that is generated from Option 1 and perform sorting by person name. You also need to prompt users if option 1 is not complete. You will then create another file with sorted person name. Option 3: Display file You will read entire file from option 1 and display it on the screen. I would like to see everything as comma separated. You also need to use exception handling here. Option 4: Ask users to enter a search keyword. You will then search the keyword in the file (option 1) and display success or failure message Option 5: Use the array concept and read the person name from the file. Just view the entire array on the output screen. Option 6: Replace function. Ask users to replace ticket type and generate a new file with new ticket type. Please keep rest of the information same as it is Option 7: Delete all the files from hard drive

Explanation / Answer

#include<iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#define MAX 100
using namespace std;

// Function to display menu and return user choice
int menu()
{
int choice;
// Displays menu
cout<<" Option 1 Input data.";
cout<<" Option 2 Sort data.";
cout<<" Option 3 Display data.";
cout<<" Option 4 Search name.";
cout<<" Option 5 Person names.";
cout<<" Option 6 Update ticket type.";
cout<<" Option 7 Delete all the files.";
cout<<" Option 8 Exit.";
cout<<" Enter your option number: ";
// Accepts user choice
cin>>choice;
// Returns user choice
return choice;
}// End of function

// Function to accept data
int acceptData(string name[], string ticketType[], double fine[])
{
char ch;
int counter = 0;
// Loops till user not entered 'q' or 'Q'
do
{
// Accept data
cout<<" Enter Name: ";
cin>>name[counter];
cout<<" Enter Ticket Type: ";
cin>>ticketType[counter];
cout<<" Enter Fine: ";
cin>>fine[counter];
// Accept choice
cout<<" Enter q to stop any other key to continue: ";
cin>>ch;
// Increase the record counter by one
counter++;
// Checks if the choice is 'q' or 'Q' then stop
if(ch == 'Q' || ch == 'q')
break;
}while(1); // End of do - while loop
// Return number of records
return counter;
}// End of function

// Function to display information
void displayData(string name[], string ticketType[], double fine[], int len)
{
// Loops till number of records
for(int c = 0; c < len; c++)
cout<<name[c]<<", "<<ticketType[c]<<", "<<fine[c]<<endl;
}// End of function

// Function to sort by name
void sortNames(string name[], string ticketType[], double fine[], int len)
{
string temp;
double temp1;

// Loops till number of records
for(int i = 0; i < len; i++)
{
// Loops from outer loop plus one to number of records
for(int j = i + 1; j < len; j++)
{
// Checks if the name at i index position is greater than the name at j index position
if(name[i].compare(name[j]) > 0)
{
// Swapping process for name
temp = name[i];
name[i] = name[j];
name[j] = temp;

// Swapping process for ticket type
temp = ticketType[i];
ticketType[i] = ticketType[j];
ticketType[j] = temp;

// Swapping process for fine
temp1 = fine[i];
fine[i] = fine[j];
fine[j] = temp1;
}// End of if condition
}// End of inner for loop
}// End of outer for loop
}// End of function

// Function to write data to file
void writeData(string name[], string ticketType[], double fine[], int len, string fileNames)
{
// Ofstream object declared
ofstream outFile;

// Open file for writing
outFile.open(fileNames.c_str());

// Checks whether file can be opened or not
if(outFile.fail())
{
cout <<"Unable to open the file for writing." << endl;
exit(0);
}// End of if condition

// Loops till number of records
for(int x = 0; x < len; x++)
{
// Writes record information to file
outFile<<name[x]<<" "<<ticketType[x]<<" "<<fine[x];
// Checks if loop value is less than the last record
if(x < (len - 1))
// Write new line character
outFile<<" ";
}// End of for loop
}// End of function

// Function to search name
void searchName(string name[], string ticketType[], double fine[], int len)
{
string tempName;
// Initial found status is zero for not found
int found = 0;
// Accept the name to search
cout<<" Enter a name to search: ";
cin>>tempName;

// Loops till number of records
for(int x = 0; x < len; x++)
{
// Compares the name at x index position is equals to the name entered by the user
if(name[x].compare(tempName) == 0)
{
// Set the found status to one
found = 1;
cout<<" Success.";
break;
}// End of if condition
}// End of for loop
// Checks if the found value is zero name not found
if(found == 0)
cout<<" Failure.";
}// End of function

// Function read data from file
void readFile(string fileName)
{
// ifstream object declared
ifstream inFile;
// to store names read from the file
string name[MAX];
string temp;
double temp1;
int c = 0;

// Open file for reading
inFile.open(fileName.c_str());

// Checks whether file can be opened or not
if(inFile.fail())
{
cout <<"No Such File Found." << endl;
exit(0);
}// End of if condition

// Loops till not end of file
while (!inFile.eof())
{
// Reads the name from the file and stores it in string array name's c index position
// Increase the counter by one
inFile>>name[c++];
// For dummy reading
inFile>>temp;
inFile>>temp1;
}// End of while loop

cout<<" Names in file: ";

// Loops till number of names read
for(int x = 0; x < c; x++)
// Displays the name
cout<<name[x]<<endl;
}// End of function

void updateTicketType(string name[], string ticketType[], double fine[], int len)
{
string type, tempName;
// Accepts the name
cout<<" Enter the name to change ticket type: ";
cin>>tempName;
// Initial found status is zero for not found
int found = 0;

// Loops till number of records
for(int x = 0; x < len; x++)
{
// Compares the name at x index position is equals to the name entered by the user
if(name[x].compare(tempName) == 0)
{
// Set the found status to one
found = 1;
// Displays the current ticket type
cout<<" Current ticket type is: "<<ticketType[x];
// Accepts the new ticket type
cout<<" Enter the ticket type to change: ";
cin>>type;
// Replaces the ticket type
ticketType[x] = type;
break;
}// End of if condition
}// End of for loop
// Checks if the found value is zero name not found
if(found == 0)
cout<<" Failure. No such name found";
}// End of function

// Function to delete all the file created
void deleteFiles(string fileNames[], int fileCounter)
{
// Loops till number of files
for(int x = 0; x < fileCounter; x++)
{
// Deletes the file and checks the delete status
// If not zero then display error message
if( remove(fileNames[x].c_str()) != 0 )
cout<<" Error deleting file: "<<fileNames[x];
// otherwise display success message
else
cout<<" File "<<fileNames[x]<<" successfully deleted.";
}// End of for loop
}// End of function

// main function definition
int main()
{
string name[MAX];
string ticketType[MAX];
string fileNames[MAX];
double fine[MAX];
int counter;
int selectedOption = 0;
int fileCounter = 0;
// Loops till user choice is not 8
do
{
// Calls the function to display menu and returns the user choice
// Based on the user choice call the function
switch(menu())
{
case 1:
// Set the selected option to 1, for option one selected
selectedOption = 1;
counter = acceptData(name, ticketType, fine);
writeData(name, ticketType, fine, counter, "personData.txt");
fileNames[fileCounter++] = "personData.txt";
break;
case 2:
// Checks if the selected option value is zero
if(selectedOption == 0)
{
// Then display message to select option one
cout<<" Please select the option 1 first.";
break;
}// End of if condition
sortNames(name, ticketType, fine, counter);
writeData(name, ticketType, fine, counter, "sortData.txt");
fileNames[fileCounter++] = "sortData.txt";
break;
case 3:
displayData(name, ticketType, fine, counter);
break;
case 4:
searchName(name, ticketType, fine, counter);
break;
case 5:
readFile("sortData.txt");
break;
case 6:
updateTicketType(name, ticketType, fine, counter);
writeData(name, ticketType, fine, counter, "updateData.txt");
fileNames[fileCounter++] = "updateData.txt";
break;
case 7:
deleteFiles(fileNames, fileCounter);
break;
case 8:
exit(0);
default:
cout<<" Invalid Choice.";
}// End of switch - case
}while(1); // End of do - while loop
}// End of function

personData.txt file contents

Pyari
AC
20
Mohan
Sleeper
55
Sahu
General
10
Ravi
Sleeper
56

sortData.txt file contents

Mohan
Sleeper
55
Pyari
AC
20
Ravi
Sleeper
56
Sahu
General
10

updateData.txt file contents

Mohan
Sleeper
55
Pyari
AC
20
Ravi
AC
56
Sahu
General
10

Sample Output:


Option 1 Input data.
Option 2 Sort data.
Option 3 Display data.
Option 4 Search name.
Option 5 Person names.
Option 6 Update ticket type.
Option 7 Delete all the files.
Option 8 Exit.
Enter your option number: 2

Please select the option 1 first.
Option 1 Input data.
Option 2 Sort data.
Option 3 Display data.
Option 4 Search name.
Option 5 Person names.
Option 6 Update ticket type.
Option 7 Delete all the files.
Option 8 Exit.
Enter your option number: 1

Enter Name: Pyari

Enter Ticket Type: AC

Enter Fine: 20

Enter q to stop any other key to continue: t

Enter Name: Mohan

Enter Ticket Type: Sleeper

Enter Fine: 55

Enter q to stop any other key to continue: y

Enter Name: Sahu

Enter Ticket Type: General

Enter Fine: 10

Enter q to stop any other key to continue: y

Enter Name: Ravi

Enter Ticket Type: Sleeper

Enter Fine: 56

Enter q to stop any other key to continue: q

Option 1 Input data.
Option 2 Sort data.
Option 3 Display data.
Option 4 Search name.
Option 5 Person names.
Option 6 Update ticket type.
Option 7 Delete all the files.
Option 8 Exit.
Enter your option number: 2

Option 1 Input data.
Option 2 Sort data.
Option 3 Display data.
Option 4 Search name.
Option 5 Person names.
Option 6 Update ticket type.
Option 7 Delete all the files.
Option 8 Exit.
Enter your option number: 3
Mohan, Sleeper, 55
Pyari, AC, 20
Ravi, Sleeper, 56
Sahu, General, 10

Option 1 Input data.
Option 2 Sort data.
Option 3 Display data.
Option 4 Search name.
Option 5 Person names.
Option 6 Update ticket type.
Option 7 Delete all the files.
Option 8 Exit.
Enter your option number: 4

Enter a name to search: Rakesh

Failure.
Option 1 Input data.
Option 2 Sort data.
Option 3 Display data.
Option 4 Search name.
Option 5 Person names.
Option 6 Update ticket type.
Option 7 Delete all the files.
Option 8 Exit.
Enter your option number: 4

Enter a name to search: Mohan

Success.
Option 1 Input data.
Option 2 Sort data.
Option 3 Display data.
Option 4 Search name.
Option 5 Person names.
Option 6 Update ticket type.
Option 7 Delete all the files.
Option 8 Exit.
Enter your option number: 5

Names in file:
Mohan
Pyari
Ravi
Sahu

Option 1 Input data.
Option 2 Sort data.
Option 3 Display data.
Option 4 Search name.
Option 5 Person names.
Option 6 Update ticket type.
Option 7 Delete all the files.
Option 8 Exit.
Enter your option number: 6

Enter the name to change ticket type: Ram

Failure. No such name found
Option 1 Input data.
Option 2 Sort data.
Option 3 Display data.
Option 4 Search name.
Option 5 Person names.
Option 6 Update ticket type.
Option 7 Delete all the files.
Option 8 Exit.
Enter your option number: 6

Enter the name to change ticket type: Ravi

Current ticket type is: Sleeper
Enter the ticket type to change: AC

Option 1 Input data.
Option 2 Sort data.
Option 3 Display data.
Option 4 Search name.
Option 5 Person names.
Option 6 Update ticket type.
Option 7 Delete all the files.
Option 8 Exit.
Enter your option number: 12

Invalid Choice.
Option 1 Input data.
Option 2 Sort data.
Option 3 Display data.
Option 4 Search name.
Option 5 Person names.
Option 6 Update ticket type.
Option 7 Delete all the files.
Option 8 Exit.
Enter your option number: 7

File personData.txt successfully deleted.
File sortData.txt successfully deleted.
File updateData.txt successfully deleted.

Option 1 Input data.
Option 2 Sort data.
Option 3 Display data.
Option 4 Search name.
Option 5 Person names.
Option 6 Update ticket type.
Option 7 Delete all the files.
Option 8 Exit.
Enter your option number: 8

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote