I am supposed to write a program with two classes, Employee and Department. When the main() function runs, it asks the user to choose one of the six numbered options that are displayed and creates arrays for Employee and Department objects. I'm disregarding every other option except option 1, the Create Department Option, so the focus of this issue will be the Department class and the Department department[3] array.
There is a while loop in the main() function that continuously runs until the user decides to exit. If the user enters option 1, a Department array object is created, and then the user also enters the departmentID, departmentName, and departmentHeadName for that object. The while loop notifies the user if the array has three Employee objects. However, I am having difficulties because each departmentID needs to be unique. For example, I cannot enter 1 for the first array object's departmentID, and then enter 1 again for the second array object's departmentID. How do I check if the user's departmentID input already exists in a previous object?
#include <iostream>
#include <string>
using namespace std;
// Employee Class
class Employee
{
private:
string employeeID;
string employeeName;
string employeeDepartmentID;
double employeeSalary;
int employeeAge;
public:
void createEmployee()
{
cout << "Please Enter Employee Details:" << endl;
cout << "Employee ID : ";
cin >> employeeID;
cout << "Employee Name :";
cin >> employeeName;
cout << "Salary: $";
cin >> employeeSalary;
cout << "Age : ";
cin >> employeeAge;
cout << "Department ID : ";
cin >> employeeDepartmentID;
}
};
// Department Class
class Department
{
private:
string departmentID;
string departmentName;
string departmentHeadName;
public:
void createDepartment()
{
cout << "Please Enter Department Details: \n";
cout << "Department ID : ";
cin >> departmentID;
cout << "Department Name : ";
cin >> departmentName;
cout << "Head of Department : ";
cin >> departmentHeadName;
}
};
// Function prototype
void displayMenu();
// Client main function
int main()
{
Employee employee[5];
Department department[3];
int choice;
int departmentCount = 0;
int employeeCount = 0;
while (true)
{
displayMenu();
cin >> choice;
if (choice == 1 && departmentCount < 3)
{
department[departmentCount].createDepartment();
departmentCount = departmentCount + 1;
}
else if (choice == 1 && departmentCount >= 3)
{
cout << "\nThe array is full, you can not add any more Departments." << endl;
}
else if (choice == 2 && employeeCount < 5)
{
employee[employeeCount].createEmployee();
employeeCount = employeeCount + 1;
}
else if (choice == 2 && employeeCount >= 5)
{
cout << "The array is full, you can not add any more Employees." << endl;
}
else if (choice == 6)
{
cout << "Thank you, goodbye." << endl;
break;
}
}
return 0;
}
// Display menu function
void displayMenu()
{
cout << "1. Create Department" << endl;
cout << "2. Create Employee" << endl;
cout << "3. Write Out Data File" << endl;
cout << "4. Read In Data File" << endl;
cout << "5. Display Salary Report" << endl;
cout << "6. -- Quit -- " << endl;
cout << "Please make a selection : ";
}
try this one
#include <iostream>
#include <string>
using namespace std;
// Employee Class
class Employee
{
private:
string employeeID;
string employeeName;
string employeeDepartmentID;
double employeeSalary;
int employeeAge;
public:
void createEmployee()
{
cout << "Please Enter Employee Details:" << endl;
cout << "Employee ID : ";
cin >> employeeID;
cout << "Employee Name :";
cin >> employeeName;
cout << "Salary: $";
cin >> employeeSalary;
cout << "Age : ";
cin >> employeeAge;
cout << "Department ID : ";
cin >> employeeDepartmentID;
}
};
// Department Class
class Department
{
private:
string departmentID;
string departmentName;
string departmentHeadName;
public:
void createDepartment()
{
cout << "Please Enter Department Details: \n";
cout << "Department ID : ";
cin >> departmentID;
cout << "Department Name : ";
cin >> departmentName;
cout << "Head of Department : ";
cin >> departmentHeadName;
}
public:
string getDepartmentID(){
return departmentID;
}
};
// Function prototype
void displayMenu();
// Client main function
int main()
{
Employee employee[5];
Department department[3];
int choice;
int departmentCount = 0;
int employeeCount = 0;
while (true)
{
displayMenu();
cin >> choice;
if (choice == 1 && departmentCount < 3)
{
department[departmentCount].createDepartment();
for(int i=0;i<departmentCount;i++)
{
if(department[i].getDepartmentID()==department[departmentCount].getDepartmentID())
{
cout<<"already exists......................... \n";
}
}
departmentCount = departmentCount + 1;
}
else if (choice == 1 && departmentCount >= 3)
{
cout << "\nThe array is full, you can not add any more Departments." << endl;
}
else if (choice == 2 && employeeCount < 5)
{
employee[employeeCount].createEmployee();
employeeCount = employeeCount + 1;
}
else if (choice == 2 && employeeCount >= 5)
{
cout << "The array is full, you can not add any more Employees." << endl;
}
else if (choice == 6)
{
cout << "Thank you, goodbye." << endl;
break;
}
}
return 0;
}
// Display menu function
void displayMenu()
{
cout << "1. Create Department" << endl;
cout << "2. Create Employee" << endl;
cout << "3. Write Out Data File" << endl;
cout << "4. Read In Data File" << endl;
cout << "5. Display Salary Report" << endl;
cout << "6. -- Quit -- " << endl;
cout << "Please make a selection : ";
}
I used getDepartmentID function in Department Class to get department id from each department object
public:
string getDepartmentID(){
return departmentID;
}
there should be return type is string.because you have created departmentID as a string.
and I used For Loop in main function to compare relevant department id already exists or not
for(int i=0;i<departmentCount;i++)
{
if(department[i].getDepartmentID()==department[departmentCount].getDepartmentID())
{
cout<<"already exists......................... \n";
}
}
Related
I am currently working on a personal project. For now, I am creating the part where it gets the users information. I am running into an issue where if the user chooses to correct their name, it steps over it and does not let the user re input the correct name. I have tried clearing before getting to the switch statement and in different locations.
Here is the switch statement:
switch (correction)
{
case 1 : cout << "Name: ";
cin.clear();
getline(cin,name);
checkInfo(age,number,name);
break;
case 2 : cout << "Age: ";
cin >> age;
while (age < 21)
{
cout << "Age is too low, try again." << endl;
cin >> age;
}
checkInfo(age,number,name);
break;
case 3 : cout << "Number: ";
cin >> number;
while(!isNumeric(number) || number.size() < 8)
{
cout << "Phone number either is too short/long or contains characters that are not digits. Try again" << endl;
cout << "Number: ";
cin >> number;
}
checkInfo(age,number,name);
break;
default : cout << "Please select options 1 through 3, nothing else is accepted: ";
cin >> correction;
break;
}
And here is where the user first in puts their information in the beginning:
cout << "Enter your name: ";
getline(cin,name);
cin.clear();
cout << "Enter your age: ";
cin >> age;
cin.clear();
cout << "Enter your phone number: ";
cin >> number;
cout << endl;
while (age >= 21)
{
while (!isNumeric(number) || number.size() < 8)
{
cout << "Phone number either is too short/long or contains characters that are not digits. Try again" << endl;
cout << "Number: ";
cin >> number;
}
checkInfo(age,number,name);
}
}
I have a feeling i'm not clearing it somewhere correctly. Any help or recommendations to make this better is also appreciated. Thank you.
info.h file:
//info.h file
#ifndef INFO
#define INFO
#include <iostream>
#include <fstream>
#include <cctype>
#include <algorithm>
using namespace std;
namespace info
{
class user
{
public:
char yesno;
char confirm;
int correction;
void getInfo (int age, string number, string name);
void checkInfo(int age, string number, string name);
void changeInfo(int age, string number, string name);
private:
bool isNumeric(string str);
};
}
void info::user::changeInfo(int age, string number, string name)
{
cout << "Age: " << age << endl;
cout << endl;
cout << "Please select the number according to the information that needs to be changed." << endl;
cout << endl;
cout << "1. Name" << endl;
cout << "2. Age" << endl;
cout << "3. Number" << endl;
cout << "Selection: ";
cin >> correction;
switch (correction)
{
case 1 : cout << "Name: ";
cin.clear();
getline(cin,name);
checkInfo(age,number,name);
break;
case 2 : cout << "Age: ";
cin >> age;
while (age < 21)
{
cout << "Age is too low, try again." << endl;
cin >> age;
}
checkInfo(age,number,name);
break;
case 3 : cout << "Number: ";
cin >> number;
while(!isNumeric(number) || number.size() < 8)
{
cout << "Phone number either is too short/long or contains characters that are not digits. Try again" << endl;
cout << "Number: ";
cin >> number;
}
checkInfo(age,number,name);
break;
default : cout << "Please select options 1 through 3, nothing else is accepted: ";
cin >> correction;
break;
}
}
void info::user::getInfo (int age, string number, string name)
{
cout << "Enter your name: ";
getline(cin,name);
cin.clear();
cout << "Enter your age: ";
cin >> age;
cin.clear();
cout << "Enter your phone number: ";
cin >> number;
cout << endl;
while (age >= 21)
{
while (!isNumeric(number) || number.size() < 8)
{
cout << "Phone number either is too short/long or contains characters that are not digits. Try again" << endl;
cout << "Number: ";
cin >> number;
}
checkInfo(age,number,name);
}
}
void info::user::checkInfo(int age, string number, string name)
{
cout << "Is the given information correct?" << endl;
cout << "-------------------" << endl;
cout << endl;
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Number: " << number << endl;
cout << endl;
cout << "If yes, please press y for yes, n for no: ";
cin >> confirm;
while (age >= 21)
{
if (confirm == 'y' || confirm == 'Y')
{
cout << "In my actual project, this is where i would save the information" << endl;
break;
}
else
{
changeInfo(age,number,name);
checkInfo(age,number,name);
break;
}
}
}
bool info::user::isNumeric(string str)
{
for (int i = 0; i < str.length(); i++)
{
if(isdigit(str[i]) == false)
{
return false;
}
}
return true;
}
#endif
Main.cpp file:
#include <iostream>
#include "info.h"
using namespace std;
int main ()
{
int age;
string number;
string name;
info::user userInfo;
userInfo.final(age, number,name);
return 0;
}
I have to write a program for bank.The classes if you will say I will provide but the problem is here,
My Problem is that in my main program I first call a function called BankProgram(); But there are other statements in the function after the function is being called The below code isnt executed at all
cout << "Enter y for opening menu again and n for exiting\t";
cin >> option;
while (option != 'y' || option != 'n')
{
cout << "Enter right value Please! only y or n: ";
char option1;
cin >> option1;
if (option1 == 'y' || option1 == 'n')
{
break;
}
}
if (option == 'y')
{
goto label;
}
else if (option == 'n')
{
cout << "The program is ending now! ";
}
Although I want to display the menu again for the user to do anything in the bank but the program terminates and exits I dont know why.I think there is a problem with object lifetime but I think that must not be the problem
Any help is appreciated.
This is the AddAccount Class
#pragma once
#include<iostream>
#include<string>
#include <vector>
using namespace std;
class Accounts
{
vector<int> Account_ID;
vector<string> AccountType;
vector<int> Customer_ID;
vector<int> Account_Balance;
public:
Accounts();
void WithDraw(int);
void Deposit(int,string,int,int);//Also can be named as add Account
void Balance();
void DeleteAccount(int);
int getAccountsNumber();
};
//Definning classes methods
Accounts::Accounts()
{
//no need to initialize vectors.They work perfect without initializing.C++ has done work for it
}
void Accounts::Deposit(int AID,string AT,int CID,int AB)
{
this->Account_ID.push_back(AID);
this->AccountType.push_back(AT);
this->Customer_ID.push_back(CID);
this->Account_Balance.push_back(AB);
}
void Accounts::WithDraw(int index)
{
cout << "\nThe Account ID of " << (index + 1) << " person is equal to: "
<< this->Account_ID[index] << endl;
cout << "\nThe Account Type of " << (index + 1) << " person is equal to: "
<< this->AccountType[index] << endl;
cout << "\nThe Customer ID of " << (index + 1) << " person is equal to: "
<< this->Customer_ID[index] << endl;
cout << "\nThe Account Balance of " << (index + 1) << " person is equal to: "
<< this->Account_Balance[index] << endl;
}
void Accounts::DeleteAccount(int index)
{
Account_ID.erase(Account_ID.begin()+index);
AccountType.erase(AccountType.begin()+index);
Customer_ID.erase(Customer_ID.begin()+index);
Account_Balance.erase(Account_Balance.begin()+index);
//Displaying that the account is successfully removed from the bank
cout << "\nThe Account ID of " << (index + 1) << " was successfully removed from the bank";
cout << "\nThe Account Type of " << (index + 1) <<" was successfully removed from the bank";
cout << "\nThe Customer ID of " << (index + 1) << " was successfully removed from the bank";
cout << "\nThe Account Balance of " << (index + 1) << " was successfully removed from the bank";
}
//It will display all the balance in the bank
void Accounts::Balance()
{
//The static int is not changed on the iteration where ever used in the loop or where ever
static int sum = 0;//To store the sum of all balance
for (unsigned int i = 0; i < Account_Balance.size(); i++)
{
sum = sum + Account_Balance[i];
}
cout << "The total balance in the bank is equal to : " << sum;
}
int Accounts::getAccountsNumber()
{
return Account_ID.size();
}
This is the Customer Class
#pragma once
#include<string>
using namespace std;
#include"Accounts.h"
#include"Manager.h"
class Customer
{
vector<string> Name;
vector<int> ID;
public:
Customer();
void AddCustomer(string,int);
void PrintAllCustomersData();
void DeleteCustomer(int);
void Print_CustomerDetails(int);
string getCustomerName(int);
};
void Customer::AddCustomer(string n, int id)
{
this->Name.push_back(n);
this->ID.push_back(id);
cout << "\nThe customer " << n << "with Id: " << id << " was successfully added in the Bank.";
}
void Customer::PrintAllCustomersData()
{
for (unsigned int i = 0; i < ID.size(); i++)
{
cout << "\nThe ID of " << (i + 1) << "Customer is : " << ID[i] << " and NAME is : " << Name[i];
}
}
void Customer::DeleteCustomer(int index)
{
Name.erase(Name.begin() + index);
ID.erase(ID.begin() + index);
cout << "\nThe customer with Name : " << Name[index] << " and ID: " << ID[index] << " was successfully deleted\n";
}
void Customer::Print_CustomerDetails(int index)
{
cout << "The Customer Name is : " << Name[index] << endl;
cout << "The Id of Customer is : " << ID[index] << endl;
}
string Customer::getCustomerName(int index)
{
return (Name[index]);
}
This is the Manager Class
#pragma once
#include<iostream>
#include<string>
using namespace std;
class Manager
{
string Name;
string Branch;
int ID;
public:
void Print_ManagerDetails();
friend ostream& operator<<(ostream&, const Manager& M);
friend istream& operator>>(istream&, Manager& M);
};
void Manager::Print_ManagerDetails()
{
cout << "\nThe ID of Manager is : " << ID << endl;
cout << "\nThe Name of Manager is : " << Name << endl;
cout << "\nThe Branch of Manager is : " << Branch << endl;
}
istream& operator>>(istream& input, Manager& M) {
input >> M.ID >> M.Name>>M.Branch;
return input;
}
ostream& operator<<(ostream& output, const Manager& M) {
output << "\nThe ID of Manager is : " << M.ID<<endl;
output << "\nThe Name of Manager is : " << M.Name << "\nThe Branch of Manager is : " << M.Branch<<endl;
return output;
}
This is the Bank Class
#pragma once
#include"Customer.h"
#include"Accounts.h"
class Bank
{
Customer* customers_ptr;
Accounts* Accounts_ptr;
Manager manager;
public:
Bank();
void AddAccount(int,string,int,int);
void DeleteAccount(int);
void AddCustomer(string,int);
void DeleteCustomer(int);
int GetNoOfAccounts();
string GetCustomer_Name(int);
};
Bank::Bank()
{
cout << "\nThe program is in the bank class\n";
}
void Bank::AddAccount(int AID, string AT, int CID, int AB)
{
Accounts_ptr->Deposit(AID, AT, CID, AB);
}
void Bank::DeleteAccount(int index)
{
Accounts_ptr->DeleteAccount(index);
}
void Bank::AddCustomer(string name, int ID)
{
customers_ptr->AddCustomer(name, ID);
}
void Bank::DeleteCustomer(int index)
{
customers_ptr->DeleteCustomer(index);
}
int Bank::GetNoOfAccounts()
{
int num=Accounts_ptr->getAccountsNumber();
return num;
}
string Bank::GetCustomer_Name(int index)
{
string name = customers_ptr->getCustomerName(index);
return name;
}
This is the main program
#include<iostream>
#include<string>
#include<conio.h>
#include<algorithm>
#include"Bank.h"
#include"Customer.h"
#include"Manager.h"
#include"Accounts.h"
using namespace std;
void BankProgram();
void BankProgram()
{
Bank b1;
Accounts A1;
Manager m1;
char options;
cout << "\n\nEnter what you want to do \n1 for entering the managers data,\n2 for showing the managers data "
<< "\n3 for adding a customer in the bank\n4 for adding an account in the bank \n5 for deleting the customer\n"
<< "\n5 for deleting the account\n6 for getting customer name\n7 for getting No. of accounts"
<< "\n8 for seeing all the balance in the bank\nPress 'e' for exit";
cin >> options;
switch (options)
{
case '1':
{
//The manager class data
cout << "\nEnter the name,ID,Branch Of Manager: ";
cin >> m1;
break;
}
case '2':
{
cout << "\nThe data of Manager is :" << m1;
break;
}
case '3':
{
string Cname;
int CID;
cout << "\nEnter the name of customer: ";
cin >> Cname;
cout << "\nEnter the Customer ID: ";
cin >> CID;
b1.AddCustomer(Cname, CID);
break;
}
case '4':
{
int AID;
int CID;
int AB;
string AT;
cout << "\nEnter the name of Account ID: ";
cin >> AID;
cout << "\nEnter the name of Customer ID: ";
cin >> CID;
cout << "\nEnter the name of Account BALANCE: ";
cin >> AB;
cout << "\nEnter the name of Account Type: ";
cin >> AT;
b1.AddAccount(AID, AT, CID, AB);
break;
}
case '5':
{
int index;
cout << "\nEnter the customer which you want to delete: ";
cin >> index;
b1.DeleteCustomer(index);
break;
}
case '6':
{
int index;
cout << "\nEnter the account which you want to delete: ";
cin >> index;
b1.DeleteAccount(index);
break;
}
case '7':
{
int cn;
cout << "\nEnter the customer ID which you want to get name: ";
cin >> cn;
b1.GetCustomer_Name(cn);
break;
}
case '8':
{
b1.GetNoOfAccounts();
break;
}
case '9':
{
A1.Balance();
break;
}
case 'e':
{
cout << "The program is ending now: ";
break;
}
default:
{
cout << "\n\nEnter right value please: \n";
}
}
}
int main()
{
// BankProgram();
bool flag = true;
char option;
label:
BankProgram();
cout << "Enter y for opening menu again and n for exiting\t";
cin >> option;
while (option != 'y' || option != 'n')
{
cout << "Enter right value Please! only y or n: ";
char option1;
cin >> option1;
if (option1 == 'y' || option1 == 'n')
{
break;
}
}
if (option == 'y')
{
goto label;
}
else if (option == 'n')
{
cout << "The program is ending now! ";
}
}
Well it turns out to be very simple
Bank::Bank()
{
cout << "\nThe program is in the bank class\n";
}
void Bank::AddAccount(int AID, string AT, int CID, int AB)
{
Accounts_ptr->Deposit(AID, AT, CID, AB);
}
Accounts_ptr is an uninitialised pointer, using it results in a program crash. You must give Accounts_ptr a value in the Bank constructor.
You have the same issue with customers_ptr.
//Amanda Genise
//CSC123 - Part 3
//08/06/2015
#include <iostream>
#include <string>
using namespace std;
class gamelist
{
private:
int gamecount;
//gameobject gameobjs[10];
public:
void add_game();
void print_list();
float total_value();
};
class gameobject
{
public:
void set_id_num(int num);
int get_id_num();
void set_name(string name1);
string get_name();
void set_type(int type_of_game);
string get_type();
void set_buy_value(float buy_game);
float get_buy_value();
void set_market_value(float market_price);
float get_market_value();
void set_year(int year1);
int get_year();
private:
int id_num;//identifier number for the game
string name;//the name of the game
int type;//whether the game is cartridge, CD, DVD, BR, download
string type_name;//type of game
float buy_value;//price of game
float market_value;//value of game
int year;//year the game was made
};
int main()
{
int option;//menu choice
do
{
//menu
cout << endl;
cout << "Please choose an option from the below menu. " << endl;
cout << "1. Add Game" << endl;
cout << "2. Print List" << endl;
cout << "3. Total value of collection" << endl;
cout << "4. Delete Game" << endl;
cout << "5. Exit" << endl;
cout << "Which would you like to execute? ";
cin >> option;
cin.ignore();
//to add a game
if (option == 1)
{
gamelist run;
run.add_game();
}
else if (option == 2)
{
gamelist run;
run.print_list();
}
} while (option != 5);
if (option == 5)
return 0;
}
void gamelist::add_game()
{
gameobject test;
int id;
string name_game;
int type_game;
int buy;
int market;
int year_game;
cout << "Please enter an id number for the game: ";
cin >> id;
test.set_id_num(id);//passes value
cout << "Please enter a name for the game: ";
cin.ignore();
getline(cin, name_game);
test.set_name(name_game);//passes value
cin.ignore();
cout << "There are four types of games." << endl;
cout << " 0. Cartridge " << endl;
cout << " 1. CD " << endl;
cout << " 2. DVD " << endl;
cout << " 3. BR " << endl;
cout << " 4. Download " << endl;
cout << "Which type do you want to set for the game (enter number)? ";
cin >> type_game;
test.set_type(type_game);//passes value
cout << "Please set a buying value for the game: ";
cin >> buy;
test.set_buy_value(buy);//passes value
cout << "Please set the market value of the game: ";
cin >> market;
test.set_market_value(market);//passes value
cout << "What is the model year of the game? ";
cin >> year_game;
test.set_year(year_game);//passes value
}
//sets id num for the game
void gameobject::set_id_num(int num)
{
id_num = num;
}
//displays the id num for the game
int gameobject::get_id_num()
{
return(id_num);
}
//sets desired name for game
void gameobject::set_name(string name1)
{
name = name1;
}
//displays the name of the game
string gameobject::get_name()
{
return(name);
}
//presents a menu to choose type of game
void gameobject::set_type(int type_of_game)
{
type = type_of_game;
}
//prints the type of game chosen
string gameobject::get_type()
{
if (type == 0)
{
type_name = "cartridge";
return(type_name);
}
else if (type == 1)
{
type_name = "CD";
return(type_name);
}
else if (type == 2)
{
type_name = "DVD";
return(type_name);
}
else if (type == 3)
{
type_name = "BR";
return(type_name);
}
else if (type == 4)
{
type_name = "download";
return(type_name);
}
}
//sets the buying value of game
void gameobject::set_buy_value(float buy_game)
{
buy_value = buy_game;
}
//displays the buying value for game
float gameobject::get_buy_value()
{
return(buy_value);
}
//sets market value
void gameobject::set_market_value(float market_price)
{
market_value = market_price;
}
//displays market value
float gameobject::get_market_value()
{
return(market_value);
}
//sets model year of the game
void gameobject::set_year(int year1)
{
year = year1;
}
//displays model year
int gameobject::get_year()
{
return(year);
}
I have not written the code for print or total value. But I am mostly worried about getting the gameobject gameobjs[10] array to work. I have no idea how to fill the array and it is supposed to be filled with the game info after I add a game. help?
You can do this in void gamelist::add_game()
test.set_id_num(id);
this->gameobjs[0].set_id_num(id);
Then what you can do is to execute the statements present in add_game() in a loop, and add 10 gameobjects.
Try this, it will help you to add 10 games for 1 gamelist object
int id;
string name_game;
int type_game;
int buy;
int market;
int year_game;
for(int i = 0; i <10; i++)
{
cout << "Please enter an id number for the game: ";
cin >> id;
this->gameobjs[i].set_id_num(id);
cout << "Please enter a name for the game: ";
cin.ignore();
getline(cin, name_game);
cin.ignore();
this->gameobjs[i].set_name(name_game);
cout << "There are four types of games." << endl;
cout << " 0. Cartridge " << endl;
cout << " 1. CD " << endl;
cout << " 2. DVD " << endl;
cout << " 3. BR " << endl;
cout << " 4. Download " << endl;
cout << "Which type do you want to set for the game (enter number)? ";
cin >> type_game;
this->gameobjs[i].set_type(type_game);
cout << "Please set a buying value for the game: ";
cin >> buy;
this->gameobjs[i].set_buy_value(buy);
cout << "Please set the market value of the game: ";
cin >> market;
this->gameobjs[i].set_market_value(market);
cout << "What is the model year of the game? ";
cin >> year_game;
this->gameobjs[i].set_year(year_game);
}
I have to write a program for an array based database that will allow the user to enter new data, update existing data, delete entries and view a list of the entries. The requirements are a structure called DATE, a structure called CustData that holds the user data, an array of type CustData with a size of 10 and requires an individual function for entering, updating, deleting and displaying the customer data. It also needs to use a while loop to initialize each index in the array with everything initialized to 0. I have a rough program written but the more I work on it the more I feel like I am doing it completely wrong. So far I have it working to the point that it lets me add multiple entries without overwriting previous ones, but I can't seem to be able to limit the number of entries I can input. I also have the displaying of entries correct. Any help that could be offered is greatly appreciated, below is my program so far, with some of the data input sections commented out to make testing it easier. My apologies for leaving this out, this is in C++, written with visual studio 2013.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
struct Date
{
int month,
day,
year;
};
struct cust
{
int ID;
string name;
string address;
string city;
string state;
string zip;
string phone;
double balance;
Date lastpayment;
};
const int SIZE = 10;
int menuchoice;
int num = 0;
int i;
void showmenu();
void funcentercustdata(cust[], int);
void funcupdatecustdata();
void funcdeletecustdata();
void funcdisplaycustdata(cust[], int);
cust custdb[SIZE];
int main()
{
cout << "Welcome to Michael's Marvelous Database Contrabulator!\n";
cout << setw(10) << "Customer Database\n\n\n";
showmenu();
int index;
for (index = 0; index < SIZE; index++)
{
custdb[index].ID = 0;
custdb[index].name = "";
custdb[index].address = "";
custdb[index].city = "";
custdb[index].state = "";
custdb[index].zip = "";
custdb[index].phone = "";
custdb[index].balance = 0.00;
custdb[index].lastpayment.month = 0;
custdb[index].lastpayment.day = 0;
custdb[index].lastpayment.year = 0;
}
return 0;
}
void showmenu()
{
cout << "\n\n1) Enter new customer data.\n";
cout << "2) Update customer data.\n";
cout << "3) Delete customer data.\n";
cout << "4) Display Customer data.\n";
cout << "5) Quit the program.\n\n";
cout << "Please enter your choice: ";
cin >> menuchoice;
do
{
switch (menuchoice)
{
case 1:
funcentercustdata(custdb, SIZE);
showmenu();
break;
case 2:
funcupdatecustdata();
showmenu();
break;
case 3:
funcdeletecustdata();
showmenu();
break;
case 4:
funcdisplaycustdata(custdb, SIZE);
showmenu();
break;
case 5:
cout << "Thank you and have a nice day!\n";
break;
default:
cout << "Please enter a correct choice\n";
cin >> menuchoice;
break;
}
} while (menuchoice != 5);
}
void funcentercustdata(cust custinfo[], int size)
{
if (custinfo[i].ID != 0)
{
i++;
cout << "\n\nEnter ID: ";
cin >> custinfo[i].ID;
cout << "Enter name: ";
cin.ignore(0);
cin >> custinfo[i].name;
/* cout << "Enter address: ";
cin.ignore(0);
cin>>custinfo[i].address;
cout << "Enter city: ";
cin.ignore(0);
cin>>custinfo[i].city;
cout << "Enter state: ";
cin.ignore(0);
cin>>custinfo[i].state;
cout << "Enter zip: ";
cin.ignore(0);
cin>>custinfo[i].zip;
cout << "Enter phone number (###-###-####): ";
cin.ignore(0);
cin>>custinfo[i].phone;
cout << "Enter balance: ";
cin >> custinfo[i].balance;
cout << "Enter last payment (mo day year, e.g. 11 17 2014): ";
cin >> custinfo[i].lastpayment.month >> custinfo[i].lastpayment.day
>> custinfo[i].lastpayment.year;
cin.ignore(1);
// }*/
cout << "Customers successfully added.\n";
}
else if (custinfo[i].ID != 0 && custinfo[i].ID >= 4)
{
cout << "No further inputs allowed\n";
}
else
{
cout << "\n\nEnter ID: ";
cin >> custinfo[i].ID;
cout << "Enter name: ";
cin.ignore(0);
cin >> custinfo[i].name;
/* cout << "Enter address: ";
cin.ignore(0);
cin>>custinfo[i].address;
cout << "Enter city: ";
cin.ignore(0);
cin>>custinfo[i].city;
cout << "Enter state: ";
cin.ignore(0);
cin>>custinfo[i].state;
cout << "Enter zip: ";
cin.ignore(0);
cin>>custinfo[i].zip;
cout << "Enter phone number (###-###-####): ";
cin.ignore(0);
cin>>custinfo[i].phone;
cout << "Enter balance: ";
cin >> custinfo[i].balance;
cout << "Enter last payment (mo day year, e.g. 11 17 2014): ";
cin >> custinfo[i].lastpayment.month >> custinfo[i].lastpayment.day
>> custinfo[i].lastpayment.year;
cin.ignore(1);
// }*/
cout << "Customers successfully added.\n";
}
}
void funcupdatecustdata()
{
cout << "insert function 2\n\n";
}
void funcdeletecustdata()
{
cout << "insert function 3\n\n";
}
void funcdisplaycustdata(cust custinfo[], int size)
{
for (int i = 0; i < size; i++)
{
if (custinfo[i].ID == 0)
cout << " ";
else if (custinfo[i].ID != 0)
{
cout << "\n\nClient ID: " << custinfo[i].ID << endl;
cout << "Client name: " << custinfo[i].name << endl;
/* cout << "Client address: " << custinfo[i].name << endl;
cout << "Client city: " << custinfo[i].name << endl;
cout << "Client state: " << custinfo[i].name << endl;
cout << "Client zip: " << custinfo[i].name << endl;
cout << "Client phone: " << custinfo[i].name << endl;*/
cout << "Client balance: " << custinfo[i].balance << endl;
cout << "Client last deposit: " << custinfo[i].lastpayment.month << "/" <<
custinfo[i].lastpayment.day << "/" << custinfo[i].lastpayment.year << endl;
}
}
}
You've asked multiple questions concerning the issues in your program. So I will look at the first question:
I can't seem to be able to limit the number of entries I can input
First, your code has some fundamental flaws. One flaw is the repeated calling of showmenu() while you're in the showmenu() function. This is a recursive call, and is totally unnecessary. Imagine if your program or similar program that was structured this way had to be running 24 hours a day, and there were thousands of entries added. You will evenutally blow out the stack with all the recursive calls. So this has to be fixed.
Second, showmenu(), at least to me, should do what it says, and that is "show the menu". It should not be processing input. Do the processing of input in a separate function.
Here is a more modularized version of the program:
#include <iostream>
void processChoice(int theChoice);
void showmenu();
void addCustomer();
void deleteCustomer();
int getMenuChoice();
int customerCount = 0;
int main()
{
cout << "Welcome to Michael's Marvelous Database Contrabulator!\n";
cout << setw(10) << "Customer Database\n\n\n";
int choice = 0;
do
{
showmenu();
choice = getMenuChoice();
if (choice != 5)
processChoice(choice);
} while (choice != 5);
}
void showmenu()
{
cout << "\n\n1) Enter new customer data.\n";
cout << "2) Update customer data.\n";
cout << "3) Delete customer data.\n";
cout << "4) Display Customer data.\n";
cout << "5) Quit the program.\n\n";
}
int getMenuChoice()
{
int theChoice;
cout << "Please enter your choice: ";
cin >> theChoice;
return theChoice;
}
void processChoice(int theChoice)
{
switch (theChoice)
{
case 1:
addCustomer();
break;
//...
case 3:
deleteCustomer();
break;
}
}
void addCustomer()
{
if ( customerCount < 10 )
{
// add customer
// put your code here to add the customer
//...
// increment the count
++customerCount;
}
}
void deleteCustomer()
{
if ( customerCount > 0 )
{
// delete customer
--customerCount;
}
}
This is the basic outline of keeping track of the number of customers. Code organization and modularity is the key. The count of the current number of entries is either incremented or decremented wihin the addCustomer and deleteCustomer functions. Also note the test that is done in add/deleteCustomer().
In the main() function, note the do-while loop and the way it is set up. The showMenu function shows itself, the getMenuChoice function gets the choice and returns the number that was chosen.
If the choice is not 5, process the request. If it is 5, then the while part of the do-while kicks you out of the processing, otherwise you start back at the top (showMenu, getMenuChoice, etc). This avoids the recursive calls that your original code was doing.
In C++, I'm trying to input movie's names and years of releasing and store them in a database/structure. Before I ask for the titles and years to be inputted. I have the user log on with credentials. In this case, the username is "rusty" and the password is "rusty".
The issue I'm having is the after the credentials are verified, the first movie title to input into the database/structure is skipped. I believe this has something to do with me using the _getch function, but I'm not totally sure.
My code is below. My output looks like this:
Please enter your username
rusty
Please enter your password
Access granted! Welcome rusty
Enter title: Enter year: (input movie year)
Enter title: (input movie title)
Enter year: (input movie year)
Enter title: (input movie title)
....
#include <iostream>
#include <string>
#include <sstream>
#include <conio.h>
using namespace std;
//function prototype
int username_and_pass();
#define NUM_MOVIES 6
struct movies_list{
string title;
int year;
}films[NUM_MOVIES];
// prototype with function declaration
void sort_on_title(movies_list films[], int n)
{
movies_list temp;
for (int i = 0; i < n - 1; i++)
{
if (films[i].title>films[i + 1].title)
{
temp = films[i];
films[i] = films[i + 1];
films[i + 1] = temp;
}
}
}// end of sort_on_title function
void printmovie(movies_list movie)
{
cout << movie.title;
cout << " (" << movie.year << ") \n";
}
void search_on_title(movies_list films[], int n, string title)
{
bool flag = false;
for (n = 0; n < NUM_MOVIES; n++)
{
if (films[n].title == title)
{
cout << "Title: " << films[n].title << endl;
cout << "Year of Release: " << films[n].year << endl;
cout << "\n";
flag = true;
}
}
if (flag == false)
cout << "Search on title not found!" << endl;
}// end of search_on_title function
void search_on_year(movies_list films[], int n, int year)
{
bool flag = false; // check on existence of record
for (n = 0; n < NUM_MOVIES; n++)
{
if (films[n].year == year) // display if true
{
cout << "Title: " << films[n].title << endl;
cout << "Year of Release: " << films[n].year << endl;
cout << "\n";
flag = true;
}
}
if (flag = false)
cout << "Search on title not found!" << endl;
}// end of search_on_title function
int menu()
{
int choice;
cout << " " << endl;
cout << "Enter 1 to search on titles " << endl;
cout << "Enter 2 to search on years " << endl;
cin >> choice;
cout << "\n";
return choice;
}// end of menu function
int username_and_pass()
{
string uName;
string password;
int value;
char ch;
cout << "Please enter your username\n";//"rusty"
cin >> uName;
cout << "Please enter your password\n";//"rusty"
ch = _getch();
while (ch != 13)//As long as the user doesn't press Enter
{//(enter is ASCII code 13) continue reading keystrokes from the screen.
password.push_back(ch);
cout << '*';
ch = _getch();
}
if (uName == "rusty" && password == "rusty")
{
cout << "\n\nAccess granted! Welcome " << uName << "\n\n" << endl;
value = 1
}
else
{
cout << "Invalid credentials" << endl;
value = 0;
}
return value;
}// end of username_and_pass function
int main()
{
string mystr;
int n;
string response;
int value = 0;
do
{
value = username_and_pass();
} while (value==0);
if(value==1)
{
for (n = 0; n < NUM_MOVIES; n++)
{
cout << "Enter title: ";
getline(cin, films[n].title);
cout << "Enter year: ";
getline(cin, mystr);
stringstream(mystr) >> films[n].year;
}
//sorts records
sort_on_title(films, NUM_MOVIES);
cout << "\nYou have entered these movies:\n";
for (n = 0; n < NUM_MOVIES; n++)
printmovie(films[n]);
//menu
int choice = 0;
choice = menu();
if (choice == 1)
{
string searchTerm;
cout << "What is the movie you want to search for? " << endl;
cin >> searchTerm;
cout << " " << endl;
search_on_title(films, NUM_MOVIES, searchTerm);
}
else
{
int searchTermYear;
cout << "What is the year you want to search for? " << endl;
cin >> searchTermYear;
cout << " " << endl;
search_on_year(films, NUM_MOVIES, searchTermYear);
}
cout << "Would you like to query the database again? (Y/N)" << endl;
cin >> response;
if (response == "Y" || "y")
{
choice = menu();
if (choice == 1)
{
string searchTerm;
cout << "What is the movie you want to search for? " << endl;
cin >> searchTerm;
cout << " " << endl;
search_on_title(films, NUM_MOVIES, searchTerm);
}
else
{
int searchTermYear;
cout << "What is the year you want to search for? " << endl;
cin >> searchTermYear;
cout << " " << endl;
search_on_year(films, NUM_MOVIES, searchTermYear);
}
}
}
return 0;
}
Flush all the content's of input buffer.
Please go to following link to flush contents of input buffer.
https://stackoverflow.com/a/7898516/4112271
I am not sure what causing you this problem.But can you try using cin.clear(). Place it before you ask for input of title.
cin.clear();
cout << "Enter title: ";
getline(cin, films[n].title);
cout << "Enter year: ";
getline(cin, mystr);
stringstream(mystr) >> films[n].year;