Instructions: For this part of the project, you will add a menu to your program
ID: 3702105 • Letter: I
Question
Instructions:
For this part of the project, you will add a menu to your program in Part 2 and add functionality to create and display mailing labels by reading input from a text file. The menu should be the first thing that is displayed when your program is run. Allow the user to continue making choices until they choose the option to quit. Redisplay the menu before prompting the user for a menu option. See the Sample Output for how your menu should look.
Invalid menu choices should be validated. If the user enters an invalid menu option, your program should give them an error message that explains what they did wrong and redisplay the menu with a prompt to enter a different option number. This process should continue until the user enters a valid option number. In addition, your program should validate the user’s input for type of item to be mailed. If the user enters any number other than 1, 2 or 3, your program should give them an error message that explains what they did wrong and prompt them to enter a different number. This process should continue until the user enters valid data.
The multiple mailings option of your menu should read a text file named Mailings.txt and display to the console all the information that was displayed in Project 2 for each mailing in the file. When the user selects the multiple mailings option, the mailing labels should be created and displayed for every mailing in the file without the user needing to do anything else. The Mailings.txt file contains 5 mailings, but your program must be able to process an identically formatted file with any number of mailings.
If a user selects the multiple mailings option, your program should calculate and display the total cost of all the postage in the text file. After displaying all the mailing labels, display the total amount of postage owed. Format the total cost of the postage in dollars and cents. See Sample Output.
Summary:
Add a menu to offer the user a choice of mailing a single item, mailing multiple items or quitting the program. The menu should be the first thing displayed when your program begins executing. The menu format should match the one in the Sample Output.
Allow the user to continue making menu choices until they choose the option to quit. Always display the menu before prompting the user for a menu option.
Properly handle an invalid menu choice.
Validate the user’s input for type of mailing.
Add functionality to create and display mailing labels by reading input from the Mailings.txt text file. Make sure your program can process an identically formatted file containing any number of mailings without counting the records.
Calculate and display the total cost of all the postage in the text file. Make sure your program will correctly calculate the total cost no matter how many mailings are in the text file.
Notes:
Save your .cpp file with the required filename.
Click the assignment link to upload your file.
Sample Output
Welcome to the Mailing Label Printing System
1 – Single Mailing
2 – Multiple Mailings
3 -- Quit
Enter your choice: 1
Enter name : Larry Smith
Enter street address: 122 Main Street
Enter city: Charlotte
Enter state: NC
Enter zip code: 23499
Enter 1 for letter, 2 for envelope, 3 for parcel: 1
Enter weight in ounces: 2.1
**************************$0.93
Larry Smith
122 Main Street
Charlotte NC 23499
|::|:|::||::|::||:|::|:|::::||:|
1 – Single Mailing
2 – Multiple Mailings
3 -- Quit
Enter your choice: 2
**************$2.94
Ann Perkins
217 Lowell Drive
Pawnee IN 47408
|:|::||:::|:|::|||:::|::|:|:::||
**************$1.42
Ron Swanson
3657 White Bridge Road
Eagleton IN 47320
|:|::||:::|::||:::|:|||::::|::||
**************$0.71
Andy Dwyer
789 Cherry Road
Wamapoke IN 48034
|:|::||::|:||:::::||::|::|:::|||
**************$0.49
Ben Wyatt
1513 Plymouth Street
Pawnee IN 47407
|:|::||:::|:|::|||:::|:::||::|:|
**************$2.54
Chris Traeger
8900 Rapport Street
Eagleton IN 47322
|:|::||:::|::||:::|:|::|:|::|:||
Total cost of all the postage is $8.10
1 – Single Mailing
2 – Multiple Mailings
3 -- Quit
Enter your choice: 3
Thank you. Closing program.
Explanation / Answer
#include <iostream>
#include <unistd.h>
#include <fstream>
using namespace std;
int main()
{
int choice,zip,parcel;
float weight;
string name,street,city,state;
ifstream inFile;
inFile.open("tmp.txt");
string foobar;
string foo="**************";
do{
cout << "Welcome to the Mailing Label Printing System" << endl << "1 – Single Mailing" << endl;
cout << "2 – Multiple Mailings" << endl << "3-- Quit" << endl<< "Enter your choice:" << endl;
cin >> choice;
if(choice==1){
cout << "Enter name :" ;
getline(cin, name);
cout << "Enter street address:";
getline(cin,street);
cout << "Enter city :" ;
getline(cin, city);
cout << "Enter state:" ;
getline(cin, state);
cout <<"Enter zip code:";
cin >> zip;
cout <<"Enter 1 for letter, 2 for envelope, 3 for parcel:";
cin >> parcel;
cout << "Enter weight in ounces:";
cin >> weight;
cout << endl;
cout <<"**************************$0.93";
cout << name << endl;
cout << street << endl << city << " " << state << " " << zip << " " << endl;
continue;
}
else if(choice==2){
float sum=0.0;
while (getline(inFile, foobar))
{
auto res = mismatch(foo.begin(), foo.end(), foobar.begin());
if (res.first == foo.end())
{
foobar.erase(0,15);
sum = sum + strtof((foobar).c_str(),0);
}
cout << foobar<<endl;
}
cout << "Total cost of all the postage is $" << sum << endl;
continue;
}
else if(choice==3){
cout << "Thank you. Closing program.";
return 0;
}
else {
cout << "ERROR:<<Please Enter a Valid Choice(1,2 or 3)>>"<< endl;
usleep(2000000);
continue;
}
}while(1);
return 0;
}