CODE IN C++ IN VISUAL STUDIO
#include<iostream>
#include<fstream>
#include<string>
#include <iomanip>
using namespace std;
int getChoice();
void addRecord();
void displayTotal();
void displayRecords();
void displayAverage();
int main()
{
int choice=0;
do
{
choice=getChoice();
if(choice==1)
addRecord();
else if(choice==2)
displayRecords();
else if(choice==3)
displayTotal();
else if(choice==4)
displayAverage();
}while(choice!=5);
return 0;
}
int getChoice()
{
int menuChoice=0;
cout<<endl<<"Menu Option"<<endl;
cout<<"1 Add Records"<<endl;
cout<<"2 Display Records"<<endl;
cout<<"3 Display Total sales"<<endl;
cout<<"4 Display Average Sales"<<endl;
cout<<"5 Exit"<<endl;
cout<<"Choice(1,2,3,4 or 5)? ";
cin>>menuChoice;
cin.ignore(100,'\n');
cout<<endl;
return menuChoice;
}
void addRecord()
{
string name="";
int sales=0;
ofstream outFile;
outFile.open("sales.txt",ios::app);
if(outFile.is_open())
{
cout<<"Salesperson's name (X to stop): ";
getline(cin,name);
while(name !="X" && name!="x")
{
cout<<"Sales: ";
cin>>sales;
cin.ignore(100,'\n');
outFile<<name<<'#'<<sales<<endl;
cout<<"Salesperson's name"<<"(X to stop): ";
getline(cin, name);
}
outFile.close();
}
else
cout<<"Sales.txt file could not be opened."<<endl;
}
void displayTotal()
{
string name ="";
int sales=0;
int total=0;
ifstream inFile;
inFile.open("sales.txt");
if(inFile.is_open())
{
getline(inFile,name,'#');
inFile>>sales;
inFile.ignore();
while(!inFile.eof())
{
total+=sales;
getline(inFile,name,'#');
inFile>>sales;
inFile.ignore();
}
inFile.close();
cout<<"Total sales $"<<total<<endl<<endl;
}
else
cout<<"Sales.txt file could no be opened"<<endl;
}
void displayRecords()
{
ifstream inFile;
string str;
inFile.open("sales.txt");
if(inFile.is_open())
{
while (std::getline(inFile, str))
{
size_t pos = str.find("#");//find the position of the # in the line
string str3 = str.substr (0,pos);//then taking substring upto the #
//and then print the name and the price (for taking price take the substring from the pos+1)
cout << "Salesperson's name: "<<str3 <<", Sales =$"<<str.substr(pos+1)<<endl;
// now we loop back and get the next line in 'str'
}
}
else
cout<<"Sales.txt file could no be opened"<<endl;
}
void displayAverage()
{
string name ="";
int sales=0;
int total=0;
int count_Records=0;//this variable keep track on the no of records in the file
ifstream inFile;
inFile.open("sales.txt");
//this is same as the finding the total
//but here we have the find the no of records in the file
if(inFile.is_open())
{
getline(inFile,name,'#');
inFile>>sales;
inFile.ignore();
count_Records++;//increment by 1
while(!inFile.eof())
{
count_Records++;//increment by 1
total+=sales;
getline(inFile,name,'#');
inFile>>sales;
inFile.ignore();
}
double avg=double(total)/double(count_Records);//calculate the avarage bye this formula
std::cout << std::fixed;
std::cout << std::setprecision(2);//this is for setting precision upto 2 points
cout<<"Avarage sales $"<<avg<<endl<<endl;
}
else
cout<<"Sales.txt file could no be opened"<<endl;
}
OUTPUT
SALES.TXT FILE
CODE SNAPS
IF YOU HAVE ANY QUERY REGARDING THIS CODE PLEASE ASK ME IN THE COMMENT I AM HERE FOR HELP. THANKS. PLEASE APPRICIATE MY WORK WITH THUMBS UP.
Please add code for program. movies.txt file. LAB 14-2 Plan and Create In this lab, you...
C++ program Write a C++ program to manage a hospital system, the system mainly uses file handling to perform basic operations like how to add, edit, search, and delete record. Write the following functions: 1. hospital_menu: This function will display the following menu and prompt the user to enter her/his option. The system will keep showing the menu repeatedly until the user selects option 'e' from the menu. Arkansas Children Hospital a. Add new patient record b. Search record c....
unctions with No Parameters Summary In this lab, you complete a partially prewritten Python program that includes a function with no parameters. The program asks the user if they have preregistered for art show tickets. If the user has preregistered, the program should call a function named discount() that displays the message "You are preregistered and qualify for a 5% discount." If the user has not preregistered, the program should call a function named noDiscount() that displays the message "Sorry,...
Write a C# code for a Sales Calculator: Note: You cannot use the foreach loop in any part of this program! sales.txt file: 1189.55, 1207.00, 1348.27, 1456.88, 1198.34, 1128.55, 1172.37, 1498.55, 1163.29 The application should have controls described as follows: • A button that reads Get Sales. If the user clicks this button, the application should call a method named ProcesSalesFile. ProcessSalesFile Method: accepts no arguments and does not return anything The ProcessSalesFile Method will do the following: o Open...
Write a C# code for a Sales Calculator: Note: You cannot use the foreach loop in any part of this program! sales.txt file: 1189.55, 1207.00, 1348.27, 1456.88, 1198.34, 1128.55, 1172.37, 1498.55, 1163.29 The application should have controls described as follows: • A button that reads Get Sales. If the user clicks this button, the application should call a method named ProcesSalesFile. ProcessSalesFile Method: accepts no arguments and does not return anything The ProcessSalesFile Method will do the following: o Open...
In C++ please! Please include .cpp and .hpp files! Thank you! Recursive Functions Goals Create and use recursive functions In this lab, we will write a program that uses three recursive functions. Requirements: Important: You must use the array for this lab, no vectors allowed. First Recursive Function Write a function that recursively prints a string in reverse. The function has ONLY one parameter of type string. It prints the reversed character to the screen followed by a newline character....
CE – Return and Overload in C++ You are going to create a rudimentary calculator. The program should call a function to display a menu of three options: 1 – Integer Math 2 – Double Math 3 – Exit Program The program must test that the user enters in a valid menu option. If they do not, the program must display an error message and allow the user to reenter the selection. Once valid, the function must return the option...
C Program Assignment: 2-Add comments to your program to full document it by describing the most important processes. 3-Your variables names should be very friendly. This means that the names should have meaning related to what they are storing. Example: Instead of naming the variable that stores the hours worked for an employee in a variable called ‘x’ you should name it HoursWorked. 5-Submit your .c program (note that I only need your source code and not all files that...
INSTRUCTION AND PROBLEMS Write a Python program for each of the problems in this lab. Please use PyCharm to type and test your programs. Submit the Python files to Blackboard for credit. In this lab, you should submit 4 Python files, one for each problem PROBLEM I Energy consumption is measured in units of kilowatt hours (kWh). The more kWh a household use in a month, the higher the energy bll. A power company charges customers $0.12 per kWh for...
Please write this program in C++, thanks Task 9.3 Write a complete C++ program to create a music player Your program should read in several album names, each album has up to 5 tracks as well as a genre. First declare genre for the album as an enumeration with at least three entries. Then declare an album structure that has five elements to hold the album name, genre, number of tracks, name of those tracks and track location. You can...