I get an error of "no operator matches these operands" when using the part of the code outputFile << customerName << "your Monthly payments are " << monthlyPay << endl;. Overall, I need the code to Add the ability to save data to disk in one or more files and a menu should give the user the option to save or retrieve data. I have not gotten past the error to properly run the program. Can you please help fix error.
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
#include <vector>
#include<fstream>
using namespace std;
void menu(void);
void writeData(void);
void readData(void);
const char FileName[] = "CourseProjectAvilaF.txt";
//Variables
vector <double> Loanlgth, Loanamt, interestRate, totalInterest;
vector <double> monthlyPay(100), loanTotal(100), creditScore(100);
vector <string> customerName;
int main()
{
menu();
return 0;
}
void menu(void)
{
const int INPUT_CUSTOMER = 1, DISPLAY_LOAN = 2, EXIT_PROGRAM = 3;
int option;
//Program
cout << "Thank you for choosing The Bank of UA for your loan requirements!\n\n";
do
{
cout << "UA Bank menu:\n\n"
<< "1. Enter your information\n"
<< "2. See your loan requirements\n"
<< "3. Exit program\n\n"
<< "Choose an option: ";
cin >> option;
while (option < INPUT_CUSTOMER || option > EXIT_PROGRAM)
{
cout << "Please enter a valid menu option: ";
cin >> option;
}
if (option == 1)
{
writeData();
}
if (option == 2)
{
readData();
}
} while (option != EXIT_PROGRAM);
}
//function to read customer information
void writeData(void)
{
fstream outputFile;
outputFile.open(FileName, fstream::app);
int index;
int numCustomers = 0;
cout << "Please enter the number of customers you would like\n"
<< " to enter loan information for: ";
cin >> numCustomers;
for (index = 0; index < numCustomers; index++)
{
string tempName;
double tempLoanamt, tempLoanlgth, tempcreditScore, tempinterestRate,
tempinterest;
cout << "Please enter your name: ";
cin >> tempName;
customerName.push_back(tempName);
cout << "Please enter the loan amount: $";
cin >> tempLoanamt;
Loanamt.push_back(tempLoanamt);
cout << "Please enter the length of the loan in months: ";
cin >> tempLoanlgth;
Loanlgth.push_back(tempLoanlgth);
cout << "What is your current credit score? ";
cin >> tempcreditScore;
creditScore.push_back(tempcreditScore);
//This will determine interest rate and overall loan amount when calculated
if (tempcreditScore <= 650)
tempinterestRate = .12;
else
tempinterestRate = .05;
interestRate.push_back(tempinterestRate);
//Calculations
tempinterest = Loanamt[index] * interestRate[index];
totalInterest.push_back(tempinterest);
loanTotal[index] = (Loanamt[index] + totalInterest[index]);
monthlyPay[index] = loanTotal[index] / Loanlgth[index];
// Out put files to write data to be saved
outputFile << customerName << "your Monthly payments are " << monthlyPay << endl;
outputFile << "Your total interest is " << totalInterest << endl;
outputFile << "You owe " << loanTotal << endl;
outputFile << "You have " << Loanlgth << " months to pay off your balance" << endl;
}
outputFile.close(); //Close file
}
//function loan information
void readData(void)
{
int index;
int numCustomers = 0;
ifstream inputFile;
inputFile.open(FileName, fstream::in);//Open the file with read mode
//Display monthly payment
cout << fixed << setprecision(2);
for (index = 0; index < numCustomers; index++)
{
cout << customerName[index] << " your total loan is " << loanTotal[index]
<< "\n"
<< "with a monthly payment of $" << monthlyPay[index] << "\n"
<< "for " << Loanlgth[index] << " months with an interest\n"
<< "rate of " << interestRate[index] << endl;
}
}
It's simple enough, you got it right everywhere else in your program.
When you want to access a particular element of a vector you use an index. Like this
outputFile << customerName[index] << "your Monthly payments are " << monthlyPay[index] << endl;
outputFile << "Your total interest is " << totalInterest[index] << endl;
outputFile << "You owe " << loanTotal[index] << endl;
outputFile << "You have " << Loanlgth[index] << " months to pay off your balance" << endl;
customerName and monthlyPay are vectors. You can't stream them directly. Instead you can do something like
for (auto const &name : customerName)
outputFile << name;
Related
I am working on the "checkout" process of my vending machine code. I want to write it so that the program will keep asking for the amount needed from the user until all the money is entered. However, this code segment does not completely work.
"Checkout" Segment of Code:
while (money < total) {
float amountOwed = total - money;
cout << "Please insert another $" << amountOwed << endl;
cout << "Enter amount: $" << flush;
float payment;
cin >> payment;
}
if (money > total) {
float change = money - total;
cout << "Thank you! You have $" << change << " change." << endl;
}
if (money == total) {
cout << "Thank you! Have a nice day!." << endl;
}
Full code below:
#include <iostream>
#include <iomanip>
using namespace std;
string menuItems[5] = { "Popcorn", "Coconut Clusters" , "Granola Bar" , "Trail Mix" , "Chocolate" };
float cost[5] = { 2, 3, 2.50, 1.50, 1 };
void vendingMachine() {
for (int i = 0; i < 5; i++)
cout << i + 1 << ". " << menuItems[i] << ": $" << cost[i] << endl;
}
int main() {
cout.precision(2);
cout << std::fixed;
cout << "Vending Machine" << endl;
cout << "----Items------" << endl;
vendingMachine();
cout << "Enter 0 to checkout" << endl;
float total;
total = 0;
int item;
do {
cout << "Enter your selection: " << flush;
cin >> item;
item = item - 1;
//here will be printed : $0 has been added to cart even if you pressed 0 and what to escape
//is it possible to fix this??
cout << menuItems[item] << ": $" << cost[item] << " has been added to cart." << endl;
total = total + cost[item];
} while (item != -1);
cout << " " << endl;
cout << "Proceding to checkout..." << endl;
cout << "========================" << endl;
cout << "Amount due: $" << total << endl;
cout << "Insert money here: $" << flush;
float money;
cin >> money;
while (money < total) {
float amountOwed = total - money;
cout << "Please insert another $" << amountOwed << endl;
cout << "Enter amount: $" << flush;
float payment;
cin >> payment;
}
if (money > total) {
float change = money - total;
cout << "Thank you! You have $" << change << " change." << endl;
}
if (money == total) {
cout << "Thank you! Have a nice day!." << endl;
}
return 0;
}
In this loop:
while (money < total) {
you are not modifying money or total so the loop will never exit.
You probably want to update money like this:
while (money < total) {
// ...
cin >> payment;
money += payment;
}
I am to create an application that takes at least 5 employees i.d, names, pay rate, and hours. And then I am to add the pay rate and the hours together to show the gross pay for each employee at the end of the initial inquiries. I am stuck on how to add it in the vector please help!
** Here is the assignment that our instructor gave us **
http://itweb.fvtc.edu/ag/?u=3&f=cpp-assignment4
I've added a vector and added all the essential information for the employee
#include <iostream>
#include <conio.h>
#include <string>
#include <vector>
using namespace std;
struct Employee
{
int id;
string firstName;
string lastName;
float payRate;
int hours;
};
int main()
{
/*========= other way of adding employee information ==========*/
/*const int NUM_EMPLOYEE = 5;
Employee employee[NUM_EMPLOYEE];
for (int i = 0; i < 5; i++)
{
cout << "ID of employee " << (i + 1) << ": ";
cin >> employee[i].id;
cout << "First Name of employee " << (i + 1) << ": ";
cin >> employee[i].firstName;
cout << "Last Name of employee " << (i + 1) << ": ";
cin >> employee[i].lastName;
cout << "Pay rate for employee " << (i + 1) << ": ";
cin >> employee[i].payRate;
cout << "Hours worked " << (i + 1) << ": ";
cin >> employee[i].hours;
}*/
/*========= End of other way of adding employee information ==========*/
vector<Employee> employees;
char Another = 'y';
while (Another == 'y' || Another == 'Y')
{
Employee e;
cout << "Enter employee ID: \n";
cin >> e.id;
cout << "Enter employee first name: \n";
cin >> e.firstName;
cout << "Enter employee last name: \n";
cin >> e.lastName;
cout << "Enter employee pay rate: \n";
cin >> e.payRate;
cout << "Enter employee hours worked: \n";
cin >> e.hours;
employees.push_back(e);
cout << "Another? (y/n): \n";
cin >> Another;
}
float sum = 0;
vector<Employee>::iterator it = employees.begin();
for (; it != employees.end(); it++)
{
cout << "ID of employee: \n" << it->id << ": \n"
<< "Employees name: \n" << it->firstName << " " << it->lastName << ": \n"
<< "Employee pay rate: \n" << it->payRate << ": \n"
<< "Employee hours worked: \n" << it->hours << "\n";
}
float avg = sum / employees.size();
Employee e;
/*cout << " ID of employees: \n" << e.id;
cout << " Name of employees: \n" << e.firstName << " " <<
e.lastName;*/
cout << "Gross pay of employees: \n" << avg;
_getch();
return 0;
}
Show Id, names, and gross pay of all employees to user
vector<Employee> employees;
char Another = 'y';
while (Another == 'y' || Another == 'Y')
{
Employee e;
cout << "Enter employee ID: \n";
cin >> e.id;
cout << "Enter employee first name: \n";
cin >> e.firstName;
cout << "Enter employee last name: \n";
cin >> e.lastName;
cout << "Enter employee pay rate: \n";
cin >> e.payRate;
cout << "Enter employee hours worked: \n";
cin >> e.hours;
employees.push_back(e);
cout << "Another? (y/n): \n";
cin >> Another;
}
float sum = 0;
vector<Employee>::iterator it = employees.begin();
cout << "ID" << "\t" << "First Name" << "\t" << "Last Name" << "\t" << "Pay rate" << "\t" << "Hours" << "\t" << "Gross Pay" << "\n" ;
for (; it != employees.end(); it++)
{
float grossPay = it->payRate * it->hours;
cout << it->id << "\t" << it->firstName << "\t\t" << it->lastName << "\t\t" << it->payRate << "\t\t"
<< it->hours << "$" << "\t" << grossPay << "\n";
sum += grossPay;
}
cout << "The gross pay for all employee is: " << "$" << sum;
_getch();
return 0;
}
//This is what i got so far. But if I want to make the application ask the user to input how many employee they want to enter into the database how do i do that? would it be like this?
int EMPLOYEE_NUM[][] // ??
i'm trying to load the file into an array when the program starts so i can
modify or search in it i don't know if my code works or not ( it's not reading the file )i have the file
and there's two books in
i have tried to debug it but couldn't find the problem the code works
but it think there's a problem with the load() function i don't know what
my code :
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
struct books{
//identfying books with all needed things
int id, status;
string title, p_name, p_address;
string date;
string aut_name, aut_nationality;
}newbook[10000], aut[10000];
int i = 0;
void load(){
ifstream myfile("books.txt", ios::in);
while (myfile >> newbook[i].id >> newbook[i].title >> newbook[i].p_name >> newbook[i].p_address >> aut[i].aut_name >> aut[i].aut_nationality >> newbook[i].date >> newbook[i].status)
i++;
}
void search_for_book(){
int temp = 0;
int idx;
cout << "enter the ID of the book you're looking for : ";
cin >> idx;
for (int srh = 0; srh < i; srh++){
if (newbook[srh].id == idx){
cout << setw(10) << "book found :" << endl;
cout << "title :" << newbook[srh].title << endl;
cout << "publisher name : " << newbook[srh].p_name << endl;
cout << "publisher address" << newbook[srh].p_address << endl;
cout << "author name :" << aut[srh].aut_name << endl;
cout << "author Nationality :" << aut[srh].aut_nationality << endl;
cout << "publish Date :" << newbook[srh].date << endl;
cout << "status :" << newbook[srh].status << endl;
temp++;
break;
}
else
srh++;
}
if (temp == 0){
cout << "couldn't find book" << endl << endl;
}
}
int main(){
load();
char choice;
cout << "enter your choice (3)";
cin >> choice;
if (choice == '3'){
search_for_book();
}
}
note :*( there are other functions like adding new book but not necessary to write )
*(i'm new to c++ don't really know how to read file into memory but i'm trying)
this is the code to save data into file :
void add_new_book_5(){
int booksnumber;
books newbook[1000], aut[100];
cout << "how many books you want to add ? ";
cin >> booksnumber;
cout << "what books you want to add :" << endl;
d_base.open(path, ios::out | ios::app);
for (int i = 0; i < booksnumber; i++){
cout << "id please : "; cin >> newbook[i].id;
cout << "title : "; cin.ignore(); getline(cin, newbook[i].title);
cout << "publisher name :"; getline(cin, newbook[i].p_name);
cout << "publisher address : "; getline(cin, newbook[i].p_address);
cout << "author" << " name : "; cin.ignore(); getline(cin, newbook[i].aut_name);
cout << "Nationality : "; getline(cin, newbook[i].aut_nationality);
cout << "Publish date :"; getline(cin, newbook[i].date);
cout << "How many copies of " << newbook[i].title << " "; cin >> newbook[i].status;
system("cls");
d_base << newbook[i].id << "\ " << newbook[i].title << "\ ";
d_base << newbook[i].p_name << "\ " << newbook[i].p_address << "\ ";
d_base << newbook[i].aut_name << "\ " << newbook[i].aut_nationality << "\ ";
d_base << newbook[i].date << "\ " << newbook[i].status << endl;
}
d_base.close();
cout << setw(76) << "Books Have Been Saved Sucessfully" << endl;
}
#include <iostream>
using namespace std;
int main()
{
int food, product, total;
double price;
cout << " Welcome To Maggie’s Shopping Cart Calculator!" << endl;
char option; // user's entered option will be saved in this variable
do{
//Displaying Options for the menu
cout << " 1) Please add an item to your cart " << endl;
cout << " 2) Final Total Amount" << endl;
cout << " 3) Quit the program" << endl; //Prompting user to enter an option according to menu
cout << " Please select an option : ";
cin >> option; // users option
total = price;
if (option == 1) // Checking if user selected option 1
{
cout << " Please enter item and the price : " << endl;
cin >> food;
cin >> price;
cout << " Total amount so far: " << price << endl;
}
else if (option == 2)
cout << "Total Amount so Far: " << total << endl;
else if (option == 3) // Checking if user selected option 2
{
cout << " End Shopping Cart! " << endl;
}
}
while (option != 3);
system("pause");
return 0;
}
}
This is my code that I have, I just continues to repeat the menu, even when I select an option help! The assignment is to have three options for the user to choose, and the menu should be repeated after choice 1 was selected. My code isn't letting me enter in any information.
modified ur code , check it out
#include <iostream>
#include <string>
using namespace std;
int main()
{
int product, total=0;int flag =1;string food;
double price;
cout << " Welcome To Maggie’s Shopping Cart Calculator!" << endl;
int option; // user's entered option will be saved in this variable
do{
//Displaying Options for the menu
cout << " 1) Please add an item to your cart " << endl;
cout << " 2) Final Total Amount" << endl;
cout << " 3) Quit the program" << endl; //Prompting user to enter an option according to menu
cout << " Please select an option : ";
cin >> option; // users option
if (option == 1) // Checking if user selected option 1
{
cout << " Please enter item and the price : " << endl;
cin >> food;
cin >> price;total = total + price ;
cout << " Total amount so far: " << total << endl;
}
else if (option == 2)
cout << "Total Amount so Far: " << total << endl;
else if (option == 3) // Checking if user selected option 2
{
cout << " End Shopping Cart! " << endl;
flag=0 ;
}
}
while ( flag== 1);
return 0;
}
hope this is helpful
You defined the option variable as char, while you compare it as int later on.. Try to change its definition to int
I am creating a program that acts as a payroll system in which I create a file name and enter in: ID (int), pay (double), hours (int) and gross wage (double). How would I take the data I enter to the file and put them in arrays and display them. Later on, I will need to sort through them as I have cases for in my switch statement.
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
//Function prototypes
void getEmployeeInfo(int&, double&, int&); //EmployeeInfo prototype
double calcWage(double, int, double&); //calcWage prototype
void printWages(ofstream &, int, double, int , double); //print wages prototype
//Main function
int main(){
int ID, hours, caseInput;
double pay, gross;
char input;
ofstream outputFile;
string filename;
do {
cout << " Menu " << endl;
cout << "1. Calculate gross wages for employees" << endl;
cout << "2. Display employees information to screen" << endl;
cout << "3. Display information in order of ID" << endl;
cout << "4. Display information in order of hourly rate" << endl;
cout << "5. Display information in order of hours worked" << endl;
cout << "6. Display information in order of wage" << endl;
cout << "7. Quit the system" << endl;
cout << "Enter your option --> ";
cin >> caseInput;
switch (caseInput) {
case 1: //Create file
cout << "Enter the filename: ";
cin >> filename;
//open file
outputFile.open(filename.c_str());
outputFile << setw(10) << "ID" << setw (15) << "Pay" << setw(17) << "Hours"
<< setw(11) << "Gross"<<endl;
outputFile << "---------------------------------------------------------\n";
do{
getEmployeeInfo(ID, pay, hours);
calcWage(pay, hours, gross);
printWages(outputFile, ID, pay, hours, gross);
cout << "Do you want to enter another employee's information? ";
cin >> input;
}
while(input == 'y' || input == 'Y');
//If user does not enter y, close file
outputFile.close();
cout << "The result is reported to the file " <<
filename << "." << endl;
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5: cout << "Thank you for using Math Tutor." << endl;
break;
case 6: cout << "Thank you for using Math Tutor." << endl;
break;
case 7: cout << "Thank you for using the Payroll System." << endl;
break;
default: cout << "Error. Enter a number 1-7." << endl;
}
}
while(caseInput != 7);
return 0;
}
//Function to input employee info
void getEmployeeInfo(int &ID, double &pay, int &hours){
cout << "Enter an employee's information by the order of ID number, rate, hours: ";
cin >> ID >> pay >> hours;
while(ID < 0){
cout << "You must enter a non negative value. Try again!" << endl;
cin >> ID;
}
while(pay < 0){
cout << "You must enter a non negative value. Try again!" << endl;
cin >> pay;
}
while(hours < 0){
cout << "You must enter a non negative value. Try again!" << endl;
cin >> hours;
}
}
//Function calculates gross pay
double calcWage(double pay, int hours, double &gross){
gross = pay * hours;
return gross;
}
//Print function
void printWages(ofstream &outputFile, int ID, double pay, int hours, double gross){
outputFile << setw(10)<< ID << setw(12) << "$" << setprecision(2)
<< fixed << pay << setw(13) << hours
<< setw(10) << "$" << fixed << gross << endl;
}
ifstream input("filename.txt");
copy(istreambuf_iterator<char>(input), istreambuf_iterator<char>(), ostreambuf_iterator<char>(cout));
This code will read data from a txt file and print it to screen. You may refer to cppreference.com to see how all the components work.
Make sure you include the appropriate headers. The reference will tell you that as well.
I think you can store your data in a New class, and save this data in a vecotr at first. Like this:
// declare a class with all your employee info in.
class Info
{
public:
Info(int id, int hours, double pay)
{
this.ID = id;
this.hours = hours;
this.pay = pay;
this.gross = pay*hours;
}
int ID;
int hours;
double pay;
double gross;
}
vector<Info> vInfo; // save your data from your input or file
// if you want to sort your data
std::sort(vInfo.begin(), vInfo.end(), [ ](Info &a, Info &b){
// you can change this depend on your order
// return a.xxx < b.xxxx
return a.ID < b.ID;
});
// then use the printWagesVec to print the data in your case 2,3,4,5,6
// change print function
int printWagesVec(vector<Info>& vInfo)
{
cout << setw(10) << "ID" << setw (15) << "Pay" << setw(17) << "Hours"
<< setw(11) << "Gross"<<endl;
cout << "---------------------------------------------------------\n";
vector<Info>::iterator it = vInfo.begin();
for(; it != vInfo.end(); it++)
{
cout << setw(10)<< it->ID << setw(12) << "$" << setprecision(2)
<< fixed << it->pay << setw(13) << it->hours
<< setw(10) << "$" << fixed << it->gross << endl;
}
return 0;
}