Can Xcode mix buffered and non-buffered input functions? - c++

I created this simple program to practice working with classes. I'm certain there are some errors with the way I used the class, but I'm just beginning to learn about them, so I haven't learned all of the conventions and etiquette. My main question is, can Xcode have functions that are buffered and other functions that are non-buffered? I'd like my function void InputValuesAndDisplayTotals(); to be buffered and my function void ManuallyAddCoinsAndDisplayTotals(); to be non-buffered (basically, hit a key and the character is instantly processed without using the enter key). Is this possible? Thanks in advanced.
#include <iostream>
#include <iomanip>
using namespace std;
class Coin
{
private:
const float PENNYVALUE = 0.01;
const float NICKELVALUE = 0.05;
const float DIMEVALUE = 0.10;
const float QUARTERVALUE = 0.25;
int PennyInput=0;
int NickelInput=0;
int DimeInput=0;
int QuarterInput=0;
public:
void InputValuesAndDisplayTotals();
void ManuallyAddCoinsAndDisplayTotals();
void Total();
};
int main()
{
int Choice;
Coin Count;
cout << "1 to enter total coin counts, 2 to manually count coins: ";
cin >> Choice;
if (Choice==1)
{
Count.InputValuesAndDisplayTotals();
Count.Total();
}
else
{
Count.ManuallyAddCoinsAndDisplayTotals();
}
cout << endl;
}
void Coin::InputValuesAndDisplayTotals()
{
cout << fixed << setprecision(2) << endl;
cout << "Input penny count: ";
cin >> PennyInput;
cout << "Total penny value: $" << PENNYVALUE*PennyInput;
Total();
cout << endl;
cout << "Input nickel count: ";
cin >> NickelInput;
cout << "Total nickel value: $" << NICKELVALUE*NickelInput;
Total();
cout << endl;
cout << "Input dime count: ";
cin >> DimeInput;
cout << "Total dime value: $" << DIMEVALUE*DimeInput;
Total();
cout << endl;
cout << "Input quarter count: ";
cin >> QuarterInput;
cout << "Total quarter value: $" << QUARTERVALUE*QuarterInput;
Total();
}
void Coin::ManuallyAddCoinsAndDisplayTotals()
{
char Choice2;
cout << "\n'1' for penny,\n'2' for nickel,\n'3' for dime,\n'4' for quarter,\n'q' to quit\n";
do
{
cout << "\nInput: ";
cin >> Choice2;
if (Choice2=='1')
++PennyInput;
if (Choice2=='2')
++NickelInput;
if (Choice2=='3')
++DimeInput;
if (Choice2=='4')
++QuarterInput;
cout << endl;
cout << "Pennies: " << PennyInput << endl;
cout << "Nickels: " << NickelInput << endl;
cout << "Dimes: " << DimeInput << endl;
cout << "Quarters: " << QuarterInput << endl;
Total();
}
while (Choice2!='q' && Choice2!='Q');
}
void Coin::Total()
{
cout << "\nTotal amount: $";
cout << fixed << setprecision(2) << (PENNYVALUE*PennyInput)+(NICKELVALUE*NickelInput)+(DIMEVALUE*DimeInput)+(QUARTERVALUE*QuarterInput);
cout << "\n";
}

Related

How do I run by attached a function

now i have create a code to calculate a formula but i have a problem with number 4,5,6,7 it can select but it not running. working principle of code is select a number in the box click enter and it will enter into that formula.i think my problem is a case function but i tried to fix and it can't.
....
#include<iostream>
#include<conio.h>
#include<cmath>
#include<math.h>
#include<iomanip>
#include<windows.h>
#define PI 3.14159
using namespace std;
HANDLE h=GetStdHandle(STD_OUTPUT_HANDLE);
//set Color
void colorText(int i){
SetConsoleTextAttribute(h,i);
}
//Declaring Variable
float density(float mass, float volume){
return mass / (double)volume;
}
int exponent(int base,int index,int result){
return result = pow(base, index);
}
float hypotenuse(float A_side, float B_side,float c_side_result){
return c_side_result = sqrt((A_side*A_side)+(B_side*B_side));
}
float velocity(float velo, float initial, float acceleration, float time){
return velo = initial + (acceleration * time);
}
float mass_en(float mass_value, float speedlight_value, float energy){
return energy = mass_value * (speedlight_value * speedlight_value);
}
float percentage(float percent, float number, float base_per){
return percent = (number/base_per)*100;
}
// Main Menu
void main_Menu(){
colorText(11);
system("CLS");
cout << "----------------------\n";
cout << "* 6 Useful formula *\n";
cout << "----------------------\n";
cout << setw(3)<<"1.finding density \n";
cout << setw(3)<<"2.finding exponent\n";
cout << setw(3)<<"3.finding hypotenuse\n";
cout << setw(3)<<"4.finding velocity\n";
cout << setw(3)<<"5.finding mass\n";
cout << setw(3)<<"6.finding percentage\n";
cout << setw(3)<<"7.member\n";
cout << "----------------------\n";
}
//single Menu
void density_menu(){
system("CLS");
cout << "---------------------\n";
cout << "* finding Density *\n";
cout << "---------------------\n";
}
void exponent_menu(){
system("CLS");
cout << "---------------------\n";
cout << "* finding Exponent *\n";
cout << "---------------------\n";
}
void hypotenuse_menu(){
system("CLS");
cout << "---------------------\n";
cout << "* finding Hypotenuse *\n";
cout << "---------------------\n";
}
void velocity_menu(){
system("CLS");
cout << "---------------------\n";
cout << "* finding Velocity *\n";
cout << "---------------------\n";
}
void mass_menu(){
system("CLS");
cout << "---------------------\n";
cout << "* finding Mass *\n";
cout << "---------------------\n";
}
void percentage_menu(){
system("CLS");
cout << "---------------------\n";
cout << "* finding percentage *\n";
cout << "---------------------\n";
}
int main(){
char choosechoice;
do{
main_Menu();
cout << "Press X or x to exit: ";
cin >> choosechoice;
switch(choosechoice){
case '1':
system("CLS");
float mass, volume;
cout << fixed;
cout << setprecision(2);
density_menu();
cout << "Enter Mass: ";
cin >> mass;
cout << "Enter Volume: ";
cin >> volume;
system("CLS");
density_menu();
cout << setw(10)<<"Mass: " << setw(6)<<mass << " kg"<< endl;
cout << setw(10)<<"Volume: " << setw(6)<<volume << " m^3"<< endl;
cout << setw(10)<<"Density: " << setw(6)<<density(mass,volume)<<" kg/m^3";
cout << "\n----------------------";
cout << "\nPress any key to return to main menu";
getch();
break;
case '2':
system("CLS");
int base, index, result;
cout << fixed;
cout << setprecision(2);
exponent_menu();
cout << "Enter Base number: ";
cin >> base;
cout << "Enter Index number: ";
cin >> index;
system("CLS");
exponent_menu();
cout << setw(15)<<"Base number: " << setw(6)<<base << endl;
cout << setw(15)<<"Index number: " << setw(6)<<index << endl;
cout << setw(15)<<"Result: " << setw(6)<<exponent(base,index,result);
cout << "\n----------------------";
cout << "\nPress any key to return to main menu";
getch();
break;
case '3':
system("CLS");
float A_side,B_side,c_side_result;
cout << fixed;
cout << setprecision(2);
hypotenuse_menu();
cout << "Enter A side: ";
cin >> A_side;
cout << "Enter B side: ";
cin >> B_side;
system("CLS");
hypotenuse_menu();
cout << setw(10)<<"Result: " << setw(6)<<hypotenuse(A_side,B_side,c_side_result);
cout << "\n----------------------";
cout << "\nPress any key to return to main menu";
getch();
break;
case '4':
system("CLS");
float velo, initial, acceleration, time;
cout << fixed;
cout << setprecision(2);
velocity_menu();
cout << "Enter initial velocity: ";
cin >> initial;
cout << "Enter acceleration : ";
cin >> acceleration;
cout << "Enter time : ";
cin >> time;
system("CLS");
velocity_menu();
cout << setw(10)<<"Result: " << setw(6)<<velocity(velo,initial,acceleration,time);
cout << "\n----------------------";
cout << "\nPress any key to return to main menu";
getch();
break;
case '5':
system("CLS");
float mass_value, speedlight_value, energy;
cout << fixed;
cout << setprecision(2);
mass_menu();
cout << "Enter Mass Value: ";
cin >> mass_value;
cout << "Enter Speed of light value : ";
cin >> speedlight_value;
system("CLS");
mass_menu();
cout << setw(10)<<"Result: " << setw(6)<<mass_en(mass_value, speedlight_value, energy);
cout << "\n----------------------";
cout << "\nPress any key to return to main menu";
getch();
break;
case '6':
system("CLS");
float percent, number, base_per;
cout << fixed;
cout << setprecision(2);
mass_menu();
cout << "Type the number: ";
cin >> number;
cout << "Type the base: ";
cin >> base_per;
system("CLS");
percentage_menu();
cout << setw(10)<<"Result: " << setw(6)<<percentage(percent, number, base_per);
cout << "\n----------------------";
cout << "\nPress any key to return to main menu";
getch();
break;
case '7':
system("CLS");
cout << fixed;
cout << setprecision(2);
cout << "---------------------------\n";
cout << " MEMBER \n";
cout << "Nattapon Sripradub M.4/1 No.14\n";
cout << "Natnicha Nuangsupthawee M.4/1 No.22\n";
cout << "Panicha Pornpattarapon M.4/1 No.24\n";
cout << "\n----------------------";
cout << "\nPress any key to return to main menu";
getch();
break;
}
}while(choosechoice!= 'X' && choosechoice != 'x');
return 0;
}

How to update a while loop with multiple if statements?

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;
}

Calling a class array inside of a class array C++

so I have a school project where I'm trying to create a bank account. So I have an array of class customer containing the customer name etc, this customer can have multiple accounts, so I have created an array of class accounts inside of customer. My problem is, I can't call a getter function inside of the account array, I have tried using customerArray[curr_id]->accountArray[curr_acc_num]->get_balance() however I am getting an error telling my that it is unaccessable.
Here is the code, any input would be apprechiated as it is still a work in progress.
#include <iostream>.
#include <windows.h>
#include "account.h"
#include "customer.h"
using namespace std;
bool quit;
int amount;
double bal;
bool valid_pin;
customer* customerArray[200];
account* accountArray[5];
int curr_id;
int curr_acc_num;
//1st name, 2nd name, ID
customer cust1("Paul", "Smith", 2233);
//ID, Deposit, Pin
account Paul(112, 222, 6716);
//Welcome screen function
void welcome_screen() {
cout << endl << endl << endl;
cout << "\tAbertay Banking" << endl;
cout << "\t System " << endl << endl << endl;
Sleep(1800);
}
//Display account balance function
void account_balance() {
system("cls");
cout << endl << endl;
cout << "\tBank of Abertay" << endl << endl;
cout << " " << customerArray[curr_id]->get_name() << endl;
cout << " ID: " << customerArray[curr_id]->get_custid() << endl << endl;
cout << " Balance: " << char(156) << customerArray[curr_id]->accountArray[curr_acc_num]->get_balance() << endl;
//Display any outstanding loans
if (Paul.get_loan() > 0) {
cout << endl << endl;
cout << " Outstanding loan: -" << char(156) << Paul.get_loan();
Sleep(3000);
}
//User has no loan on this account
else {
Sleep(3000);
}
}
//Withdrawl function
void withdrawl() {
bal = Paul.get_balance();
system("cls");
cout << endl << endl;
cout << "\tBank of Abertay" << endl << endl;
cout << "Enter the amount you wish to withdraw: ";
cin >> amount;
//customer has sufficent funds, withdrawl accepted
if (amount <= bal + Paul.get_overdraft()) {
bal -= amount;
Paul.set_balance(bal);
system("cls");
cout << endl << endl;
cout << "\tBank of Abertay" << endl << endl; cout << "You have withdrawn: " << char(156) << amount << endl;
cout << "You now have a balance off: " << char(156) << bal << endl;
cout << "Do you wish to carry out another transaction?"; //do you wish to carry out another transaction.
Sleep(2000);
}
//customer doesn't have enough funds to carry out withdrawl
else {
char choice;
system("cls");
cout << endl << endl;
cout << "\tBank of Abertay" << endl << endl;
cout << "You have insufficient funds in your account" << endl;
}
}
//Deposit function
void deposit() {
//balance from user account
bal = Paul.get_balance();
char check;
double ln;
system("cls");
cout << endl << endl;
cout << "\tBank of Abertay" << endl << endl;
cout << "Enter the amount you wish to deposit: ";
cin >> amount;
//Option to pay off loan, if applicable
if (Paul.get_loan() > 0) {
system("cls");
cout << endl << endl;
cout << "\tBank of Abertay" << endl << endl;
cout << "Do you want to pay off your loan? (Y/N)" << endl;
cin >> check;
system("cls");
cout << "\tBank of Abertay" << endl << endl;
//User selected to pay off loan
if (check == 'y') {
//deposit paid into loan
if (amount < Paul.get_loan()) {
ln = Paul.get_loan() - amount;
Paul.set_loan(ln);
cout << "Your loan is now at: -" << char(156) << Paul.get_loan() << endl;
}
//loan is payed of, any extra money is deposited into balance
else {
ln = amount - Paul.get_loan() + Paul.get_balance();
Paul.set_loan(0);
Paul.set_balance(ln);
cout << "You have payed your loan off!" << endl;
}
}
}
//user has no loan or doesn't wish to pay off loan
else {
bal += amount;
Paul.set_balance(bal);
}
cout << "You have deposited: " << char(156) << amount << endl;
cout << "You now have a balance off: " << char(156) << bal << endl;
cout << "Would you like to select another option with this account?"; //do you wish to carry out another transaction.
Sleep(1500);
}
//Apply for overdraft
void overdraft() {
system("cls");
char choice;
cout << endl << endl;
cout << "\tTake out an overdraft of " << char(156) << "300? " << endl;
cout << "\t (Y/N)" << endl;
cin >> choice;
if (choice == 'y') {
Paul.set_overdraft(300);
}
}
//Calculating interest rates
double interest_rate(int loan) {
//Up to £3,000 rate of 11%
if (loan < 3000) {
return 1.11;
}
//From £3,000 to £7,000 rate of 9%
else if (loan > 3000 && loan < 7000) {
return 1.09;
}
//From £7,000 to £21,000 rate of 7%
else if (loan > 7000 && loan < 21000) {
return 1.07;
}
//From £21,000 over rate of 5%
else if (loan < 21) {
return 1.05;
}
}
//Loan eligibility funcition
void loan_elgibility() {
double max_loan;
double loan;
//Calculating loan eligibility
system("cls");
cout << endl << endl;
cout << "\tBank of Abertay" << endl << endl;
cout << "Caculating loan eligibility";
max_loan = (2 * Paul.get_balance());
Sleep(800);
//Loan and rates
system("cls");
cout << endl << endl;
cout << "\tBank of Abertay" << endl << endl;
cout << " You are eligible to borrow up to: " << char(156) << max_loan << endl << endl;
cout << " Interest rates are as follows" << endl << endl;
cout << " - " << char(156) << "3,000 at 11%" << endl;
cout << " " << char(156) << "3,000 - " << char(156) << "7,000 at 9%" << endl;
cout << " " << char(156) << "7,000 - " << char(156) << "21,000 at 7%" << endl;
cout << " " << char(156) << "21,000 - at 5%" << endl;
Sleep(6200);
//User input loan request
system("cls");
cout << endl << endl;
cout << " Bank of Abertay" << endl << endl;
cout << " Enter loan request: " << char(156);
cin >> loan;
//Processing loan request
system("cls");
cout << endl << endl;
cout << " Bank of Abertay" << endl << endl;
cout << " We are processing your request..." << endl;
Sleep(800);
system("cls");
//Loan has been approved, loan deposited into account
if (loan <= Paul.get_balance()) {
cout << endl << endl;
cout << " Bank of Abertay" << endl << endl;
cout << "Your loan has been approved" << endl;
bal = Paul.get_balance() + loan;
loan = interest_rate(loan) * loan;
Paul.set_balance(bal);
Paul.set_loan(loan);
Sleep(800);
}
//Loan has not been approved
else {
cout << endl << endl;
cout << " Bank of Abertay" << endl << endl;
cout << "You do not qualify for a loan of this amount";
Sleep(800);
}
}
//Quit function
void quit_screen() {
system("cls");
char choice;
cout << endl << endl;
cout << " Bank of Abertay" << endl << endl;
cout << " Do you want to quit? (Y/N) " << endl;
cout << " ";
cin >> choice;
// customer wants to quit, end while loop and program
if (choice == 'y') {
system("cls"); //clear screen
cout << endl << endl << endl;
cout << " Thanks for using," << endl;
cout << " Bank of Abertay!"<< endl;
Sleep(2000);
quit = true;
}
else {
quit = false;
}
}
//Request user pin number
bool pin_request() {
double pin;
int counter = 0;
do {
system("cls");
cout << endl << endl;
cout << "\tBank of Abertay" << endl << endl;
cout << "\tEnter pin: ";
cin >> pin;
if (pin == Paul.get_pin()) {
return true;
}
else if (counter == 3) {
cout << "You have entered an invalid pin too many times!";
Sleep(800);
return false;
}
else {
system("cls");
cout << endl << endl;
cout << " Bank of Abertay" << endl << endl;
cout << "You have entered an invalid pin" << endl;
Sleep(800);
valid_pin = false;
counter++;
}
} while (valid_pin == false && counter < 3);
}
//Adding a new customer
int counter = 0;
void add_customer(string first_nom, string second_nom, int cust_ID) {
customer* new_customer = new customer(first_nom, second_nom, cust_ID);
customerArray[counter] = new_customer;
counter++;
}
//creating new bank account
void create_account()
{
int input_custid;
double input_balance;
int input_pin;
string first_nom;
string second_nom;
int cust_ID;
char choice;
//Request new customer information
system("cls");
cout << endl << endl;
cout << "\tBank of Abertay" << endl << endl;
cout << "Enter first name: ";
cin >> first_nom;
cout << endl;
cout << "Enter surname: ";
cin >> second_nom;
cout << endl;
cout << "Customer ID: ";
cin >> cust_ID;
//create new customer class array
add_customer(first_nom, second_nom, cust_ID);
//creating new account array
system("cls");
cout << endl << endl;
cout << "\tBank of Abertay" << endl << endl;
cout << "Enter The account No. :";
cin >> input_custid;
cout << "Initial deposit in new account: ";
cin >> input_balance;
cout << "Create pin: ";
cin >> input_pin;
system("cls");
cout << " We are processing your request..." << endl;
Sleep(800);
cust1.add_account(input_custid, input_balance, input_pin);
system("cls");
cout << "Account Created.." << endl;
cout << "Thank you for joining the Bank of Abertay";
Sleep(800);
}
//find user account
bool find_account() {
int custID;
int counter;
bool found_account;
system("cls");
cout << endl << endl;
cout << " Bank of Abertay" << endl << endl;
cout << " Enter your customer ID: ";
cin >> custID;
cout << " - ";
cin >> curr_acc_num;
//not happy about this!!
for (int i = 0; i < 200; i++) {
if (customerArray[i]->get_custid() == custID) {
curr_id = i;
return true;
}
else if (i == 199) {
return false;
}
}
}
//an option to send money to another account?
// need a function that takes user back to option menu if wrong pin enter!
int main() {
//welcome screen
welcome_screen();
//while loop until player decides to quit
do {
int option_choice;
system("cls"); //clear screen, updating board every time function called
cout << endl << endl;
cout << "\tBank of Abertay" << endl << endl;
cout << "\t Menu" << endl << endl;
cout << "\t1. View Balance" << endl;
cout << "\t2. Withdraw" << endl;
cout << "\t3. Deposit" << endl;
cout << "\t4. Loan eligibility" << endl;
cout << "\t5. New account" << endl;
cout << "\t6. Overdraft" << endl;
cout << "\t7. Quit" << endl << endl;
cout << "\t\tSelect: ";
cin >> option_choice;
//ensure option screen only runs through once unless an invalid option is chosen
int valid_number_check = 0;
// do {
switch (option_choice) {
//Display account balance
case 1:
if (find_account() == true && pin_request() == true) {
account_balance();
break;
}
else {
cout << "Your request has been denied";
break;
}
//Account withdrawl
case 2:
if ( find_account() == true && pin_request() == true) {
withdrawl();
break;
}
else {
cout << "Your request has been denied";
break;
}
//Account deposit
case 3:
if (find_account() == true && pin_request() == true) {
deposit();
break;
}
else {
cout << "Your request has been denied";
break;
}
//Loan eligibility
case 4:
if (find_account() == true && pin_request() == true) {
loan_elgibility();
break;
}
else {
cout << "Your request has been denied";
break;
}
//Create new account
case 5:
create_account();
break;
//Overdraft
case 6:
if (find_account() == true && pin_request() == true) {
overdraft();
break;
}
else {
cout << "Your request has been denied";
break;
}
//Quit
case 7:
quit_screen();
break;
//Invalid number entered
default:
cout << endl << endl;
cout << " Bank of Abertay" << endl << endl;
cout << "Please enter a valid number";
Sleep(800);
break;
}
} while (quit == false);
}
// main
Account .h
#pragma once
#include <string>
#include "account.h"
using namespace std;
class customer
{
private:
int custid;
string name;
account* accountArray[5];
int counter = 0;
public:
int get_custid();
customer(string, string, int);
string get_name();
void add_account(int, double, int);
~customer();
};
Account.cpp
#include "customer.h"
#include "account.h"
#include <string>
using namespace std;
customer::customer(string first_nom, string second_nom, int cust_ID) {
counter++;
string first_name = first_nom;
string sur_name = second_nom;
//counting the number of accounts
counter++;
//identification of customer array
//custid = counter + 1000;
custid = cust_ID;
}
customer::~customer()
{
}
int customer::get_custid() {
return custid;
}
void customer::add_account(int id, double dep, int pin) {
account* new_account = new account(id, dep, pin);
accountArray[counter] = new_account;
counter++;
}
string customer::get_name() {
return name;
}
customer.h
#pragma once
class account
{
private:
double balance;
int custid;
double loan_amount;
double overdraft;
int pin;
int account_number;
public:
account(int, double, int);
~account();
//getters
double get_balance();
double get_loan();
double get_pin();
double get_overdraft();
//setters
void set_overdraft(double over);
void set_balance(double bal);
void set_loan(double loan);
};
customer.cpp
#include "account.h"
#include <iostream>
#include <windows.h>
using namespace std;
account::account(int input_custid, double input_balance, int input_pin)
{
custid = input_custid;
balance = input_balance;
pin = input_pin;
loan_amount = 0;
overdraft = 0;
}
account::~account()
{
}
double account::get_balance()
{
return balance;
}
double account::get_loan()
{
return loan_amount;
}
double account::get_pin()
{
return pin;
}
double account::get_overdraft()
{
return overdraft;
}
void account::set_balance(double bal)
{
balance = bal;
}
void account::set_loan(double loan)
{
loan_amount = loan;
}
void account::set_overdraft(double over)
{
overdraft = over;
}

How would I store data I have inputted into a txt file to arrays?

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;
}

Calculator in C++ Help. Can't Return Running Total [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have to make a Calculator using a constructor and destructor, that adds, subtracts, multiplies and divides, and returns the total every time. For some reason, when I call the line "Calculator.add(num);" or any of the "Calculator." portions return me an error saying that it "expected an identifier". Am I missing something simple?
Thanks.
Here is my main.cpp file.
#include <iostream>
#include "Calculator.h"
#include <cstdlib>
using namespace std;
double total;
int main(){
while (true){
cout << "*** Calculator *** " << endl;
cout << "A: Add a value " << endl;
cout << "S: Subtract a value " << endl;
cout << "M: Multiply by a value " << endl;
cout << "D: Divide by a value " << endl;
cout << "T: Get the total " << endl;
cout << "Q: Quit " << endl;
cout << endl;
char input;
cin >> input;
if (input == 'A'){
cout << "Current Total: " << total << endl;
cout << "Selection: A";
cout << endl;
cout << "*** Add selected *** " << endl;
cout << "Value:";
double num;
cin >> num;
cout << endl;
double turnTotal = total;
Calculator.add(num);
cout << turnTotal << "+" << num << " = " << total;
}
if (input == 'S'){
cout << "Current Total: " << total << endl;
cout << "Selection: S";
cout << endl;
cout << "*** Subtract selected *** " << endl;
cout << "Value: ";
double num2;
cin >> num2;
cout << endl;
double turnTotal2 = total;
Calculator.subtract(num2);
cout << turnTotal2 << "-" << num2 << "=" << total;
}
if (input == 'M'){
cout << "Current Total: " << total << endl;
cout << "Selection: M";
cout << endl;
cout << "*** Multiply selected *** " << endl;
cout << "Value: ";
double num3;
cin >> num3;
cout << endl;
double turnTotal3 = total;
Calculator.multiply(num3);
cout << turnTotal3 << "*" << num3 << "=" << total;
}
if (input == 'D'){
cout << "Current Total: " << total << endl;
cout << "Selection: D";
cout << endl;
cout << "*** Divide selected *** " << endl;
cout << "Value: ";
double num4;
cin >> num4;
cout << endl;
double turnTotal4 = total;
Calculator.divide(num4);
cout << turnTotal4 << "/" << num4 << "=" << total;
}
if (input == 'T'){
cout << "Current Total: " << total << endl;
cout << "Selection: T";
cout << endl;
cout << "*** Total selected *** " << endl;
cout << "Value: ";
double num5;
cin >> num5;
cout << endl;
double turnTotal5 = total;
Calculator.getTotal(num5);
cout << turnTotal5 << "-" << num5 << "=" << total;
}
if (input == 'Q'){
cout << "Thank you for using the calculator! Bye bye! Have a great day!" << endl;
}
}
}
And here is the .cpp file
#include <cstdlib>
#include <iostream>
#include "Calculator.h"
using namespace std;
Calculator::Calculator(double x){
double total = x;
return;
}
double Calculator::getTotal(){
return total;
}
void Calculator::add(double x){
total += x;
}
void Calculator::subtract(double x){
total -= x;
}
void Calculator::multiply(double x){
total *= x;
}
void Calculator::divide(double x){
total /= x;
}
And here's the class .h file.
#include <cstdlib>
#include <iostream>
using namespace std;
//class specification
class Calculator {
public:
//constructor
Calculator(){double total = 0;}
Calculator(double total);
//member functions
void add(double x);
void subtract(double x);
void multiply(double x);
void divide(double x);
double getTotal();
//destructor
~Calculator();
private:
//data
double total = 0;
};
You doesn't seem to initialize your Calculator first. You could add the initialization for example on the start of main function as follows
double total;
Calculator calc;
int main(){
while (true){
And then use it like
calc.add(num);