All commands must be in the command line.
The expense data file is separate.
Programming Project #2: Manage Spending Using Commands Objectives: • Understand how to create and manipulate list of objects using array or vector class • Handle input errors and invalid values • Design and create a well-structure program using C++ basic programming constructs and classes. Description: Each “expense” contains 2 values: the spending amount (double) and its description (string)
Here is an example of the “expense” data file which is simply the list of “expense” entries (one expense per line): 45.25 Monthly telephone and Internet services 200.20 Monthly electric, water and gas 1200 Rent 12.90 Netflix membership 99 Amazon membership 150.50 Weekly movie, music, concert and entertainment 400 Friday restaurant and dining out 50 Monthly gym membership 175.75 Home repair and improvement 333 Yearly Tax and insurance 160 Gift and charitable contributions Implement the command-driven “ManageExpenses” program to support for the following commands:
- show show all spending entries - spend add a new spending entry by asking the amount and the description - amount>= amount-value display only spending entries that have the amount greater or equal to the given amount value - save [filename] save the current list of expenses into a given data file if no file name is given, it will save over the original file - help display the list of supported commands - exit end the program Requirements: 1. The program must produce the same output as provided. 2. The program must check and accept the command line argument of the data file name 3. The program must accept any data file name (e.g. should not hard code the file name in the program) 4. The program must read the data file only once, build the array of data only (and not the data file again). 5. There should be no global variable used in the program 6. Commands are case-insensitive. “SHOW” and “show” should produce the same command output. 7. Give an error message when command line argument for data file name is not given 8. The program should have error checks for incorrect given amount (negative as well as a string instead of a number) 9. The “amount>=” search commands expect one argument. If they don’t give an argument, please display an “invalid” command. Please do not prompt the user to enter the search string or amount separately
#include <iostream> // header file for basic input output
#include<fstream> // header file for File input output
#include <vector> // header file for vectors (dynamic array)
using namespace std;
// Class for various expenses
class Expense{
public:
double spending_amount;
char description[80];
};
int main(int argc, char const *argv[])
{
if(argc<2){
cout<<"Error! Name of the data file not provided. Program will terminate\n";
return -1;
}
string filename = argv[1]; // store filename given as command line argument
fstream input_file_stream; // filestream to read file
input_file_stream.open(filename, ios::in| ios::out| ios::binary); // opening file to read using ios::out flag to create file if it doesn't exist
//Checking if file was successfully opened
if(!input_file_stream.is_open()){
cout<<"Error opening file: "<<filename<<"\n";
return -1;
}
Expense exp;
vector<Expense> exp_arr;
while(input_file_stream.read((char*)&exp, sizeof(exp))){
exp_arr.push_back(exp);
}
input_file_stream.close();
string command="";
ofstream output_file_stream; // to save file
while(true){
cin.clear();
getline(cin,command);
if(command=="exit" || command=="EXIT") // exit if the exit command is given
break;
if(command=="show" || command=="SHOW"){
if(exp_arr.size()==0){
cout<<"File empty. Nothing to display. Please add entries to file using command spend\n";
continue;
}
for(int i=0;i<exp_arr.size();i++){
cout<<exp_arr[i].spending_amount<<" "<<exp_arr[i].description<<"\n";
}
}
else if(command=="spend" || command=="SPEND"){
cout<<"Enter the amount: ";
string num;
cin>>num;
int i;
for(i=0;i<num.length();i++)
if(isalpha(num[i])){
break;
}
if(i==num.length()){
exp.spending_amount = stod(num);
}
else{
cout<<"Invalid amount entered!\n";
continue;
}
// cin>>exp.spending_amount;
if(exp.spending_amount<0){
cout<<"Invalid amount entered!\n";
continue;
}
cout<<"Enter the description: ";
cin.ignore();
cin.getline(exp.description,80);
// cin.clear();
exp_arr.push_back(exp);
}
else if(command.substr(0,8)=="amount>=" || command.substr(0,8)=="AMOUNT>="){
string num = command.substr(8,command.length());
if(num.length()==0){
cout<<"Invalid command!\n";
continue;
}
double amount = stod(num);
for(int i=0;i<exp_arr.size();i++){
if(exp_arr[i].spending_amount>=amount){
cout<<exp_arr[i].spending_amount<<" "<<exp_arr[i].description<<"\n";
}
}
}
else if(command.substr(0,4)=="save" || command.substr(0,4)=="SAVE"){
string fname = command.substr(4,command.length());
if(fname==""){
output_file_stream.open(filename, ios::out | ios::binary);
if(!output_file_stream.is_open()){
cout<<"Error writing to file "<<filename<<"\n";
continue;
}
else{
for(int i=0;i<exp_arr.size();i++){
exp = exp_arr[i];
output_file_stream.write((char*)&exp,sizeof(exp));
}
cout<<"Written successfully to file "<<filename<<"\n";
}
}
else{
fname = command.substr(5,command.length());
output_file_stream.open(fname, ios::out | ios::binary);
if(!output_file_stream.is_open()){
cout<<"Error writing to file "<<fname<<"\n";
continue;
}
else{
for(int i=0;i<exp_arr.size();i++){
exp = exp_arr[i];
output_file_stream.write((char*)&exp,sizeof(exp));
}
cout<<"Written successfully to file "<<fname<<"\n";
}
}
}
else if(command=="help" || command=="HELP"){
cout<<"Help:\n";
cout<<"The supported commands are:\n";
cout<<"1. show: Displays all entries of the expense file\n";
cout<<"2. spend: Add spending entries\n";
cout<<"3. amount>=xxx: Displays all entries whose spending is >= xxx where xxx is a double\n";
cout<<"4. save filename: Save the current records into the file. If filename is not specified, saves to default\n";
cout<<"5. help: Displays this help\n";
cout<<"6. exit: Exits the program\n";
cout<<"These commands are case insensitive\n";
}
else{
cout<<"Invalid command!\n";
}
}
output_file_stream.close();
return 0;
}
All commands must be in the command line. The expense data file is separate. Programming Project...
Please I need help a sample code is provided below of the
output
Description This project is an enhancement of your previous project using OOP concepts. You'lI define class and instantiate objects instead of using struct to implement your design Write a menu-driven program that provides the following options 1. Show All 2. Spend 3. Search expenses containing this string 4. Search expenses with greater than or equal to this amount 5. Exit It allows the user to select a...
Command line input In C++ it is possible to accept command line arguments. Command-line arguments are given after the name of a program in command-line operating systems like Linux and are passed in to the program from the operating system. To use command line arguments in the program, it must first understand the full declaration of the main function, which until now has accepted no arguments. In fact, main can accept two arguments: one argument is number of command line...
Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text file by removing all blank lines (including lines that only contain white spaces), all spaces/tabs before the beginning of the line, and all spaces/tabs at the end of the line. The file must be saved under a different name with all the lines numbered and a single blank line added at the end of the file. For example, if the input file is given...
I need help writing this code in C++ and I’m using xcode for
mac
the
objective is in the first picture and the rest is a sample output
of the code thaf needs to be similar
bjectives: . Perform C++ string object manipulation Understand how to manipulate data using arrays of structs Handle input errors and invalid values Design and create a wel-structure program using C++ basic programming constru Description: Write a menu-driven program that provides the following options 1....
Explain what the following UNIX command does: Is 'echo I.txt' Please type shell commands (for Windows. UNIX, or any other system that you like) for: creating a directory called hello world list the contents of this directory create a file called iwashere.txt display the contents of the file iwashere.txt from the command-line Please write a simple program in C for accepting a string from the command-line, reversing and printing the reversed string to standard output.
Java program
Program: Grade Stats In this program you will create a utility to calculate and display various statistics about the grades of a class. In particular, you will read a CSV file (comma separated value) that stores the grades for a class, and then print out various statistics, either for the whole class, individual assignments, or individual students Things you will learn Robustly parsing simple text files Defining your own objects and using them in a program Handling multiple...
I need help programming this question using C language. This program uses input data entered in command line at the same time when the command or .exe file is entered. They are called command-line arguments. Because everything entered in command line is taken as a string, these arguments including the command itself or the .exe file name are stored in an array of char pointers, each pointer points to the first character of a string (i.e., argument). The inputs can...
In C Programming Adding to your program in part A, go through the command line arguments and find the largest and smallest arguments by alphabetical order. Note that you should not need to sort your arguments, but instead compare them and save the smallest and largest strings as you go through. For example, if called with ./reverse one two three: It would display the output for part A: Three two one And then it would display The smallest string was:...
JAVA project PLEASE complete/ create project with comments in
the programming explaining everything
Text file
Year of storm/ Name of storm/ mmdd storm started/ mmdd storm
ended/ magnitude of storm/ //made up data
2004/Ali/1212/1219/1
2003/Bob/1123/1222/3
1980/Sarah/0123/0312/0
1956/Michael/1211/1223/4
1988/Ryan/0926/1019/
1976/Tim/0318/1010/0
2006/Ronald/0919/1012/2
1996/Mona/0707/0723/1
2000/Kim/0101/0201/1
2001/Jim/1101/1201/3
Text file
Class storm{
private String nameStorm;
private int yearStorm;
private int startStorm;
private int endStorm;
private int magStorm;
public storm(String name, int year, int start, int end, int
mag){
nameStorm = name;
yearStorm = year;
startStorm...