How can i save arrays and print them on C++ - c++

How can i save arrays and print them in c++,here's what i mean, i am creating a student database, and in one class i am giving the students name, surname, id, age, etc. So how can i save them and print those information that i have given. Here is what i have tried so far, and it hasn't been working any body have any ideas?
#ifndef Student_h
#define Student_h
class Student
{
public:
virtual void Print() = 0;
};
#endif
#include "Student.h"
#ifndef University_h
#define University_h
class University :public Student
{
private:
string University_Name;
public:
University(string Uni);
void Set_University_Name(string University);
string Get_University_Name();
void Print();
};
#include<iostream>
#include<string>
#include"University.h"
#include"Student.h"
using namespace std;
University::University(string Uni)
{
Set_University_Name(Uni);
}
void University::Set_University_Name(string U_Name)
{
University_Name = U_Name;
}
string University::Get_University_Name()
{
return University_Name;
}
void University::Print()
{
cout << "\n South East European University" << University_Name;
}
class Data_Base
{
private:
string Student_Name;
string Student_Surname;
string Course;
int Student_Age;
int Student_ID;
public:
void Set_Name(string Name);
string Get_Name();
void Set_Surname(string Surname);
string Get_Surname();
void Set_Course(string _Course);
string Get_Course();
void Set_Age(int Age);
int Get_Age();
void Set_ID(int ID);
int Get_ID();
void Print();
void Print_Data();
double Average();
};
void Data_Base::Print(){
int n = 0;
cout << "\n Enter how many students you wish to add: ";
cin >> n;
for (int i = 0; i < n; i++) {
cout << "\n Student:" << i + 1 << endl;
cout << "\n Enter Student Name:";
cin >> Student_Name;
cout << "\n Enter Student Surname:";
cin >> Student_Surname;
cout << "\n What Course is the Student Studying:";
cin >> Course;
cout << "\n Enter Student Age:";
cin >> Student_Age;
cout << "\n Enter Student ID:";
cin >> Student_ID;
}
}
void Data_Base::Print_Data(){
cout << "Student Data Base: " << endl;{
cout << "\n Student Name :" << Student_Name;
cout << "\n Course :" << Course;
cout << "\n ID : " << Student_ID;
}
}
#include<iostream>
#include<string>
#include"Student.h"
#include"University.h"
#include"DataBase.h"
using namespace std;
int main()
{
int Menu;
Data_Base add, view, average;
Data_Base* AddStudent = &add;
Data_Base* ViewStudents = &view;
Data_Base* AverageGrade = &average;
menu:
cout << "\n===========================" << endl;
cout << "\n Press - 1 - To Add New Students";
cout << "\n Press - 2 - To View Added Students";
cout << "\n Press - 3 - To To Find the Average Grade";
cout << "\n Press - 0 - To Quit Program";
cout << "\n ----Please Select An Option----";
cin >> Menu;
switch (Menu)
{
case 1:
AddStudent->Print();
break;
case 2:
ViewStudents->Print_Data();
break;
case 3:
AverageGrade->Average();
break;
case 0:
return 0;
cout << "\n Thank You for Using our Program!";
break;
default:
cout << "\n Wrong Button!Try Again";
goto menu;
break;
}
return 0;
}

int main()
{
int Menu;
Data_Base db;
menu:
cout << "\n===========================" << endl;
cout << "\n Press - 1 - To Add New Students";
cout << "\n Press - 2 - To View Added Students";
cout << "\n Press - 3 - To To Find the Average Grade";
cout << "\n Press - 0 - To Quit Program";
cout << "\n ----Please Select An Option----";
cin >> Menu;
switch (Menu)
{
case 1:
db.Print();
break;
case 2:
db.Print_Data();
break;
case 3:
db.Average();
break;
case 0:
return 0;
cout << "\n Thank You for Using our Program!";
break;
default:
cout << "\n Wrong Button!Try Again";
goto menu;
break;
}
return 0;
}
This should let you see one student after you added it. As mentioned in the comments, you will need a list in order to store multiple students in the same database. Also it was wrong to use three diferent Data_base objects hoping for them to have the same private members becouse they have different memory unless they point to the same object.

Related

Trying to incorporate void functions into switch statements

I'm a novice coding student and trying to create a menu using structs, functions, and switch statements to make a mini database for a class assignment. I'm trying to implant the functions into the switch statements.
I'm getting errors on lines 87 and 137 and I'm not sure where I'm going wrong. Any help, explanation, or correction is much appreciated.
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream>
using namespace std;
// Jaret Clark
// Week 3 Interactive Assignment
// INT 499
// Prof. Joseph Issa
// 03/31/2022
struct EZTechMovie {
string name;
string *cast[10];
string rating;
};
void displaymovie(EZTechMovie movie, int cast_num) {
int i;
cout << endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
cout << endl;
cout << "Your entry:\n";
//Movies
cout << endl;
cout << "Movie TITLE: " << movie.name;
cout << endl;
//Movie rating
cout << "Movie Rating: " << movie.rating;
cout << endl;
//Cast name
cout << "Main Cast Members: \n";
//loop for cast members ~ stores in array
for (int i = 0; i < cast_num; ++i) {
cout << movie.cast[i];
cout << endl;
}
}
void mainmenu() {
string movie_input;
int m;
cout << endl;
cout << "Would you like to store movies into database? (yes or no) ";
getline(cin, movie_input);
cout << endl;
if (movie_input == "yes") {
string cont;
string cast_name;
int x, m, n, i, cast_num;
EZTechMovie moviedb[100];
cout << endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
cout << endl;
for (n = 0; n < 100; n++) {
cout << "Movie Title: ";
getline(cin, moviedb[n].name);
cout << endl;
cout << "Movie rating: ";
getline(cin, moviedb[n].rating);
cout << endl;
cout << "How many cast do you want to enter? ";
cin >> cast_num;
cout << endl;
cin.ignore();
for (i = 0; i < cast_num; i++) {
cout << "Cast name: First and Last name: ";
getline(cin, moviedb[n].cast[i]);
cout << endl;
}
cout << endl;
displaymovie(moviedb[n], cast_num);
cout << endl;
cout << "Add more movies? (yes or no) ";
getline(cin, cont);
if (cont == "no") {
break;
}
cout << endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
cout << endl;
}
}
else if (movie_input == "no") {
return;
}
else {
cout << "INVALID Input";
mainmenu();
}
}
// menu
void movieMenu() {
int choice;
EZTechMovie movie;
do {
cout << "***********************Welcome to EZTechMovie Movie Entry Menu***********************" << endl;
cout << "Press 1 to Enter Movie Info - Name, Cast Members, and Rating.\n";
cout << "Press 2 to Retrieve movie info recently entered.\n";
cout << "Press 3 To Quit program.\n";
// evaluate menu options in switch case
switch (choice) {
case 1:
mainmenu();
break;
case 2:
displaymovie(EZTechMovie movie, int cast_num);
break;
case 3:
cout << "Thank you and Goodbye!";
break;
default:
cout: "Invalid Selection. Try again!\n";
}
//get menu selection
cin >> choice;
} while (choice != 3);
}
int main() {
movieMenu();
}
Regarding the error on line 87 (getline(cin, moviedb[n].cast[i]);) :
moviedb[n].cast[i] is a std::string*, not std::string like you might have meant.
A quick compilation fix would be to use:
getline(cin, *(moviedb[n].cast[i]));
i.e. dereference the pointer.
However - this code raises other design/programming issues:
Why do you use std::string* and not std::string in the first place.
Why do you use C style array instead of std::vector (or std::array if you can commit to the size). This is relevant for both: string *cast[10]; and EZTechMovie moviedb[100];

Program Ends unexpectedly after the function is called

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.

C++ Virtual Void

Okay, so I have a parent class called employee and 3 subclass called manager,researcher and engineer. I made a vector and want to list them. this is the how I process the making.
vector <Employee*,Manager*> EmployeeDB;
Employee *temp;
temp = new Manager(first, last, salary, meetings, vacations);
EmployeeDB.push_back(temp);
I have no problem in making the vector, my concern is listing the info. all 3 subclasses have firstname, lastname and salary but they're difference is that they have different data members which is unique, example the Manager has the int value vacation and the Engineer has the int value experience so on and so forth.
Employee.h:
#include <iostream>
#include <string>
using namespace std;
#ifndef EMPLOYEE_h
#define EMPLOYEE_h
class Employee
{
public:
Employee();
Employee(string firstname, string lastname, int salary);
string getFname();
string getLname();
int getSalary();
virtual void getInfo();
private:
string mFirstName;
string mLastName;
int mSalary;
};
#endif
Employee.cpp:
#include "Employee.h"
#include <iostream>
#include <string>
using namespace std;
Employee::Employee()
{
mFirstName = "";
mLastName = "";
mSalary = 0;
}
Employee::Employee(string firstname, string lastname, int salary)
{
mFirstName = firstname;
mLastName = lastname;
mSalary = salary;
}
string Employee::getFname()
{
return mFirstName;
}
string Employee::getLname()
{
return mLastName;
}
int Employee::getSalary()
{
return mSalary;
}
void Employee::getInfo()
{
cout << "Employee First Name: " << mFirstName << endl;
cout << "Employee Last Name: " << mLastName << endl;
cout << "Employee Salary: " << mSalary << endl;
}
Main:
#include <vector>
#include <iostream>
#include <string>
#include "Employee.h"
#include "Engineer.h"
#include "Manager.h"
#include "Researcher.h"
using namespace std;
vector <Employee*> EmployeeDB;
Employee *temp;
void add()
{
int emp, salary, vacations, meetings, exp, c;
string first, last, type, school, topic;
bool skills;
do
{
system("cls");
cout << "===========================================" << endl;
cout << " Add Employee " << endl;
cout << "===========================================" << endl;
cout << "[1] Manager." << endl;
cout << "[2] Engineer." << endl;
cout << "[3] Researcher." << endl;
cout << "Input choice: ";
cin >> emp;
system("cls");
} while (emp <= 0 || emp > 3);
cout << "===========================================" << endl;
cout << " Employee Info " << endl;
cout << "===========================================" << endl;
cout << "Employee First name: ";
cin >> first;
cout << "Employee Last name: ";
cin >> last;
cout << "Employee Salary: ";
cin >> salary;
switch (emp)
{
case 1:
cout << "Employee numbers of meetings: ";
cin >> meetings;
cout << "Employee numbers of vacations: ";
cin >> vacations;
temp = new Manager(first, last, salary, meetings,vacations);
EmployeeDB.push_back(temp);
delete temp;
break;
case 2:
cout << endl;
cout << "[1]YES [2]NO" << endl;
cout << "Employee C++ Skills: ";
cin >> c;
if (c == 1)
{
skills = true;
}
else
{
skills = false;
}
cout << "Employee Years of exp: ";
cin >> exp;
cout << "(e.g., Mechanical, Electric, Software.)" << endl;
cout << "Employee Engineer type: ";
cin >> type;
temp = new Engineer(first, last, salary, skills, exp, type);
EmployeeDB.push_back(temp);
delete temp;
break;
case 3:
cout << "Employee School where he/she got his/her PhD: ";
cin >> school;
cout << "Employee Thesis Topic: ";
cin >> topic;
temp = new Researcher(first, last, salary, school, topic);
EmployeeDB.push_back(temp);
delete temp;
break;
}
}
void del()
{
}
void view()
{
for (int x = 0; x < (EmployeeDB.size()); x++)
{
cout << EmployeeDB[x]->getInfo();
}
}
void startup()
{
cout << "===========================================" << endl;
cout << " Employee Database " << endl;
cout << "===========================================" << endl;
cout << "[1] Add Employee." << endl;
cout << "[2] Delete Employee." << endl;
cout << "[3] List Employees." << endl;
cout << "[4] Exit." << endl;
cout << "Please Enter Your Choice: ";
}
int main(int argc, char** argv)
{
bool flag = true;
int choice;
do {
do
{
system("cls");
system("pause>nul");
startup();
cin >> choice;
} while (choice < 0 || choice >4);
switch (choice)
{
case 1:
add();
break;
case 2:
del();
break;
case 3:
view();
break;
case 4:
flag = false;
system("EXIT");
break;
}
} while (flag == true);
return 0;
system("pause>nul");
}
I am getting error on the view() function.
It says no operator<< matches these operands
binary '<<': no operator found which takes a right hand operand of type void etc etc.
The problem is that the getInfo has return type void and you are trying to put that return value into cout.
It's important to understand that the code std::cout << val actually calls the function operator<<(ostream& out, const objectType& val) where objectType is the type of 'val'.
In your case the type is void, and there is simply no implementation of operator<< that takes void as a type. hence the error "no operator found which takes a right hand operand of type void...".
In order to fix your issue you have a few options:
Change view() to be
for (...)
{
EmployeeDB[x]->getInfo();
}
Change getInfo() to return a string the info as you'd like:
std::string getInfo()
{
std::string info;
info =...
return info;
}
Create an operator<< for Employee and change view to call it:
view()
{
for (...)
{
std::cout << EmployeeDB[x];
}
}

C++ map, switch, and main menu trouble

When I run my Main Menu, I am able to add data that is needed for the map, but the problem happens when I go back to the menu and find that when I try to print the data nothing appears. In case you are wondering, there are no errors, it will just not print the data stored within the map when you run print contacts from the menu. Below is the code.
Main
#include <iostream>
using namespace std;
#include "AddContact.h"
//PASSWORD IS "Delta" use upper case D and the rest is lower case
int main() {
AddContact con;
con.Menu();
return 0;
}
AddContact.h
#include <iostream>
#include<cstdlib>
#include<map>
using namespace std;
#ifndef ADDCONTACT_H_
#define ADDCONTACT_H_
class AddContact {
public:
std::map<int, AddContact> people;
private:
string ContactName;
long long ContactPhone;
string Address;
string Email;
string Skype;
public:
AddContact();
AddContact(string ContactName, long long ContactPhone, string Address, string Email, string Skype);
void ADD();
void print()const;
void view();
void Menu();
void Password();
};
#endif /* ADDCONTACT_H_ */
AddContact.cpp
#include "AddContact.h"
AddContact::AddContact(): ContactName(""), ContactPhone(0), Address(""), Email(""), Skype(""){
}
AddContact::AddContact(string ContactName, long long ContactPhone, string Address, string Email, string Skype) {
this->ContactName = ContactName;
this->ContactPhone = ContactPhone;
this->Address = Address;
this->Email = Email;
this->Skype = Skype;
}
void AddContact::Password(){
cout << "Please Enter Your Password. " << endl;
string pass;
cin >> pass;
if (pass != "Delta") {
Password();
}
}
void AddContact::Menu(){
cout << "!!MyCircle Contact!!" << endl;
cout << endl;
Password();
int selection;
do {
cout << "***************************" << endl;
cout << "1. Add New Contact " << endl;
cout << "2. Display Contact " << endl;
cout << "3. Quit " << endl;
cout << "***************************" << endl;
cin>> selection;
switch(selection){
case 1:
ADD();
break;
view();
break;
}
} while (selection != 0);
}
void AddContact::ADD() {
for (int i = 0; i < 1; i++) {
string temp1;
cout << "Please Enter Contact Name: " << endl;
cin >> temp1;
long long temp2;
cout << "Please Enter Contact Phone Number: " << endl;
cin >> temp2;
int choice;
cout<< "Press 1 if you would like to add additional contact info or press 0 to return to main menu "<< endl;
cin >> choice;
if (choice == 1) {
string temp3 = "";
cout << "Please enter contact Address: " << endl;
cin>> temp3;
getline(cin, temp3);
string temp4= "";
cout << "Please enter contact Email: " << endl;
cin >> temp4;
string temp5 = "";
cout << "Please enter contact Skype: " << endl;
cin >> temp5;
people[i] = AddContact(temp1, temp2, temp3, temp4, temp5);
} else {
}
}
}
void AddContact::print()const{
cout<<ContactName<<" "<<ContactPhone<<" "<<Address<<" "<<Email<<" "<<Skype<<endl;
}
void AddContact::view(){
for(map<int, AddContact>::iterator it = people.begin(); it != people.end();it++){
it->second.print();
}
}
It looks like you're mising a default in your switch statement:
switch(selection){
case 1:
ADD();
break;
view();
break;
}
is mean to be
switch(selection){
case 1:
ADD();
break;
default:
view();
break;
}

Address Book using Inheritance and Operator Overloading or Exception handling

I have to make an address book using C++ and should include classes and inherited classes. The main class contains name and address and the inherited Teacher class contains teacher Phone number and CNIC and Employee class contains Phone and CNIC and the Student class contains student ID and Phone. I made a program in C++ but that would always over write the previous data. I am stuck here. I would need a skull program or simple algorithms to guide me ahead.
This is my code.
// new.cpp : Defines the entry point for the console application.
// Address Book.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
#include<string>
#include <conio.h>
#include <fstream>
#include <sstream>
#include <iomanip>
using namespace std;
class Main
{
private:
string Name;
string Address;
char test;
public:
virtual void getdata()
{
ofstream outfile("TEMP.txt");
cout <<"Enter Name: " ;
cin>>test;
getline(cin, Name); //cin >> Name;
cout << endl;
cout << "Enter Address: ";
getline(cin, Address);// cin >> Address;
outfile << Name
<< endl
<< Address;
}
virtual void setdata()
{
cout << "\n\nName: " << test+Name;
cout << "\nAddress: " << Address;
}
virtual void remove()
{
}
};
//----------------------------------------------------------
class Teacher : public Main
{
private:
int T_Number;
string T_CNIC;
public:
void getdata()
{
ofstream outfile("Teacher.txt");
Main::getdata();
cout << "Enter CNIC: ";
getline(cin,T_CNIC);//cin >> T_CNIC;
cout << "Enter Contact Number: " << endl;
cin >> T_Number;
outfile << T_CNIC
<< endl
<< T_Number;
cout << "Data entered" << endl;
}
void setdata()
{
Main::setdata();
cout << "\nTeacher CNIC: " << T_CNIC;
cout << "\nTeacher Contact Number: " << T_Number;
}
};
//------------------------------------------------------------
class Student : public Main
{
private:
int S_Number;
int S_ID;
public:
void getdata()
{
ofstream outfile("Student.txt");
Main::getdata();
cout << "Enter ID: ";
cin >> S_ID;
cout << "Enter Contact Number: ";
cin >> S_Number;
outfile << S_ID
<< endl
<< S_Number;
cout << "Data entered" << endl;
}
void setdata()
{
Main::setdata();
cout << "\nStudent Unique ID: " << S_ID;
cout << "\nStudent Contact Number: " << S_Number;
}
};
class Employee : public Main
{
private:
int E_Number;
string E_CNIC;
public:
void getdata()
{
ofstream outfile("Employee.txt");
Main::getdata();
cout << "Enter Employee CNIC: ";
getline(cin,E_CNIC);//cin >> E_CNIC;
cout << "Enter Contact Number: ";
cin >> E_Number;
outfile << E_CNIC
<< endl
<< E_Number;
cout << "Data entered" << endl;
;}
void setdata()
{
Main::setdata();
cout << "\nEmployee Unique ID: " << E_CNIC;
cout << "\nEmployee Contact Number: " << E_Number;
}
};
//-----------------------------------------------------------------------------------------
int _tmain(int argc, _TCHAR* argv[])
{
Main* myarr[100];
int var = 0;
int choice;
char input;
start:
printf("===============MAIN MENU===============");
printf("\n[1]Insert\n[2]Search\n[3]Delete\n[4]Exit\n");
printf("=======================================");
printf("\n\nEnter Your Choice: ");
cin >> choice;
switch(choice)
{
case 1:
cout << "\nEnter data for Student or Teacher or Employee (s/t/e)? ";
cin >> input;
do
{
if( input=='t' )
myarr[var] = new Teacher;
else if (input == 'e')
myarr[var] = new Employee;
else
myarr[var] = new Student;
myarr[var++]->getdata();
cout << " Enter another (y/n)? ";
cin >> input;
if(input == 'n')
{
goto start;
}
}
while( input =='y');
for(int j=0; j<var; j++)
myarr[j]->setdata();
cout << endl;
break;
case 2:
return 0;
break;
case 3:
return 0;
break;
case 4:
cout << "Are you sure you want to quit? " << endl;
getch();
break;
default:
cout << "Choose from 1 - 4 ONLY!!" << endl;
goto start;
break;
}
return 0;
}
Then I would try declared it protected, and instead of calling main::getdata(), you directly write data into the file. see if that works.