I've searched through the already asked questions, but I haven't found what I'm looking for.
I'm working with structures, where I store a person's details (first name, last name, birth date and so on...). The goal is then to create an array of pointers to these structures and make a list containing a group of people's information. the main function is vary basic so far.
#include <iostream>
#include "methods.h"
using namespace std;
void printMenu() {
cout << "\n Welcome to the Person program!" << endl
<< " Choose what you would like to do: " << endl
<< " 1. Insert person" << endl
<< " 2. Show person" << endl
<< " 3. Exit" << endl;
}
int main() {
char choice;
person p;
do {
printMenu();
cout << "\n Your choice: ";
cin >> choice;
cin.ignore();
switch(choice) {
case '1': setPerson(p); break;
case '2': getPerson(p); break;
default: cout << "\n Exiting..." << endl;
}
} while(choice == '1' || choice == '2');
return 0;
}
here's the methods.cc file
// methods.cc
#include <iostream>
#include "methods.h"
using namespace std;
static char dummy; // catches the '\n' left in the stream after the usage of cin >>
void setFirstName(char s[]) {
cout << "\n First name: ";
cin.getline(s, 25);
}
void getFirstName(const char s[]) {
cout << "\n First name: " << s << endl;
}
void setLastName(char s[]) {
cout << "\n Last name: ";
cin.getline(s, 25);
}
void getLastName(const char s[]) {
cout << "\n Last name: " << s << endl;
}
void setAddress(address & a) {
cout << "\n Street: ";
cin.getline(a.street, 25);
cout << "\n Number: ";
cin.getline(a.number, 6);
cout << "\n Town: ";
cin.getline(a.town, 25);
cout << "\n Zip code: ";
cin.getline(a.zip, 25);
cout << "\n Province: ";
cin.getline(a.province, 25);
}
void getAddress(const address & a) {
cout << "\n Street: " << a.street << endl;
cout << "\n Number: " << a.number << endl;
cout << "\n Town: " << a.town << endl;
cout << "\n Zip code: " << a.zip << endl;
cout << "\n Province: " << a.province << endl;
}
void setBirthDate(birthdate & bd) {
cout << "\n Day: ";
cin >> bd.day;
cin.get(dummy);
cout << "\n Month: ";
cin >> bd.month;
cin.get(dummy);
cout << "\n Year: ";
cin >> bd.year;
cin.get(dummy);
}
void getBirthDate(const birthdate & bd) {
cout << "\n Day: " << bd.day << endl;
cout << "\n Month: " << bd.month << endl;
cout << "\n Year: " << bd.year << endl;
}
void setGender(char & g) {
cout << "\n Gender: ";
cin >> g;
cin.get(dummy);
}
void getGender(const char & g) {
cout << "\n Gender: " << g << endl;
}
void setPerson(person & p) {
setFirstName(p.firstName);
setLastName(p.lastName);
setAddress(p.location);
setBirthDate(p.bd);
setGender(p.gender);
}
void getPerson(const person & p) {
getFirstName(p.firstName);
getLastName(p.lastName);
getAddress(p.location);
getBirthDate(p.bd);
getGender(p.gender);
}
The program works fine, but, after entering all the information and prompting the program to display the entered data, the first three fields (first name, last name and street) all show the same output, that is, the street's name... somehow the first and last names are not saved into the respective char arrays...
for example, say I enter A and B as first and last name then C as the street name, the output will be
First name: C
Last name: C
Street: C
... then from here, the output is correct...
EDIT: btw, I haven't used the string type on purpose. Being it object-oriented, it is not covered in the programming course, so I'm stuck with the c string type (arrays of chars or pointers to char)
EDIT 2: here's the methods.h file. I've found the error myself. see comments in the code in struct person
// methods.h
#ifndef METHODS_H
#define METHODS_H
struct address {
char street[15];
char number[6];
char town[15];
char zip[6];
char province[3];
};
struct birthdate {
int day;
int month;
int year;
};
struct person {
char firstName[25]; // error due to me omitting the array length.
char lastName[25]; // same here, I had written char firstName[] & char lastName[]
address location;
birthdate bd;
char gender;
};
// base methods
void setFirstName(char s[]);
void getFirstName(const char s[]);
void setLastName(char s[]);
void getLastName(const char s[]);
void setAddress(address & a);
void getAddress(const address & a);
void setBirthDate(birthdate & bd);
void getBirthDate(const birthdate & bd);
void setGender(char & g);
void getGender(const char & g);
void setPerson(person & p);
void getPerson(const person & p);
#endif
thanks for all your advice!
Related
So the purpose of the program is to Create an array of 3 people, allow the user to populate the data in a for loop, ensure that the results are capitalized, and output the results.
These new projects instructions were to
1. Rewrite capitalize() as a method within the structure.
2. Rewrite printPerson() as a method within the structure
The program itself works just fine, it's just not in the format that my professor wanted. He said the point of it is to not use any arguments but again, I don't know what he means. I just started programming a few months ago and even though I'm trying I don't have a strong knowledge of the terminology.
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
struct Person {
string firstName;
string middleName;
string lastName;
int age;
string gender;
void capitalize(Person &arg);
void printPerson(Person arg);
};
Pretty sure these are the methods right here, but I'm not sure if the (person &arg) and (person arg) are the arguments itself or if they are parameters. I thought it was the "arg" part but I can't find a way to get the program to run without them as I'm pretty sure I need the & of operator to modify the information.
int main(void) {
Person myPerson;
Person a[3];
const int size = 5;
for (int i = 0; i <= 2; i++) {
cout << "What is First Name #" << i + 1 << "? ";
getline(cin, a[i].firstName);
cout << "What is Middle Name #" << i + 1 << "? ";
getline(cin, a[i].middleName);
cout << "What is Last Name #" << i + 1 << "? ";
getline(cin, a[i].lastName);
cout << "Age #" << i + 1 << "? ";
cin >> a[i].age;
cin.ignore();
cout << "Male or Female #" << i + 1 << "? ";
getline(cin, a[i].gender);
cout << endl;
}
for (int i = 0; i <= 2; i++) {
myPerson.capitalize(a[i]);
cout << "PERSON #" << i + 1 << endl;
cout << "~~~~~~~~~~~~~~~" << endl;
myPerson.printPerson(a[i]);
}
system("pause");
return 0;
}
Along with that, I don't know how to manipulate the functions to work without the "parameters/arguments" (I'm not sure the difference at this point) or without the "arg"
void Person::capitalize(Person &arg) {
transform(arg.firstName.begin(), arg.firstName.end(), arg.firstName.begin(), ::toupper);
transform(arg.middleName.begin(), arg.middleName.end(), arg.middleName.begin(), ::toupper);
transform(arg.lastName.begin(), arg.lastName.end(), arg.lastName.begin(), ::toupper);
}
void Person::printPerson(Person arg) {
cout << "\nFirst Name: " << arg.firstName << endl;
cout << "\nMiddle Name: " << arg.middleName << endl;
cout << "\nLast Name: " << arg.lastName << endl;
cout << "\nAge: " << arg.age << endl;
cout << "\nGender: " << arg.gender << endl;
cout << "\n\n";
}
The capitalize and the printPerson are now members (usually called methods) of the struct Person. This means that they operate on the member variables of an Person instance. Like this, you can just access all the classes members in these methods. See the following code. I also completed it with a constructor and made it slightly more readable.
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
struct Person {
public:
Person();
void readFromUserInput();
void capitalize();
void print();
public:
string firstName;
string middleName;
string lastName;
int age;
string gender;
};
Person::Person() :
firstName(""),
middleName(""),
lastName(""),
age(0),
gender("")
{
}
void Person::readFromUserInput()
{
cout << "What is the First Name ? ";
getline(cin, firstName);
cout << "What is Middle Name ? ";
getline(cin, middleName);
cout << "What is Last Name ? ";
getline(cin, lastName);
cout << "Age ? ";
cin >> age;
cin.ignore();
cout << "Male or Female ? ";
getline(cin, gender);
}
void Person::capitalize()
{
transform(firstName.begin(), firstName.end(), firstName.begin(), ::toupper);
transform(middleName.begin(), middleName.end(), middleName.begin(), ::toupper);
transform(lastName.begin(), lastName.end(), lastName.begin(), ::toupper);
}
void Person::print()
{
cout << "\nFirst Name: " << firstName << endl;
cout << "\nMiddle Name: " << middleName << endl;
cout << "\nLast Name: " << lastName << endl;
cout << "\nAge: " << age << endl;
cout << "\nGender: " << gender << endl;
cout << "\n\n";
}
int main(void)
{
const int NUM_PERSONS = 3;
Person a[NUM_PERSONS];
for (int i = 0; i < NUM_PERSONS; i++)
{
cout << "### " << (i + 1) << ". User:" << endl;
a[i].readFromUserInput();
cout << endl;
}
for (int i = 0; i < NUM_PERSONS; i++)
{
a[i].capitalize();
cout << "PERSON #" << i + 1 << endl;
cout << "~~~~~~~~~~~~~~~" << endl;
a[i].print();
}
system("pause");
return 0;
}
I've been trying to figure out how to deal with pointers & structs. I wrote the following code.
#include <iostream>
using namespace std;
struct Person
{
char name[20]; //Question 2
int id;
};
const int max_num_of_childs=10;
struct Family
{
Person dad;
Person mom;
int num_of_childs;
Person* child[max_num_of_childs];
};
void add_child (Family& f)
{
char answer;
do
{
if (f.num_of_childs==max_num_of_childs)
{
cout << "no more children" <<endl;
return;
}
cout << "more child? Y/N" <<endl;
cin >> answer;
if (answer == 'Y')
{
f.child[f.num_of_childs] = new Person;
cout << "enter name and id" << endl;
cin >> f.child[f.num_of_childs]->name;
cin >> f.child[f.num_of_childs]->id;
f.num_of_childs++;
}
}
while (answer=='Y');
return;
}
void add (Family& f)
{
cout << "insert dad name & id" << endl;
cin >> f.dad.name >> f.dad.id;
cout << "\ninsert mom name & id" << endl;
cin >> f.mom.name >> f.mom.id;
add_child (f);
}
void print_child (const Family f) //Question 1
{
for (int i=0; i<f.num_of_childs; i++)
cout << "#" << i+1 << "child name: " << f.child[f.num_of_childs]->name << "child id: " << f.child[f.num_of_childs]->id << endl;
}
void print (const Family f)
{
cout << "dad name: " << f.dad.name << "\tdad id: " << f.dad.id << endl;
cout << "mom name: " << f.mom.name << "\tmom id: " << f.mom.id << endl;
print_child (f);
}
int main()
{
Family f;
f.num_of_childs=0;
add(f);
print(f);
return 0;
}
Why is the output of print_child() gibberish?
dad name: AAAA dad id: 11
mom name: BBBB mom id: 22
1child name: ï Uï∞ u u u h░├evhchild id: 6846053
2child name: ï Uï∞ u u u h░├evhchild id: 6846053
How can I define an array of chars with unlimited length? (using string also requires a defined length).
Why is the output of print_child() gibberish?
In the print_child() method, code is reaching out of initialized range of f.child array. There is:
void print_child (const Family f) //Question 1
{
for (int i=0; i<f.num_of_childs; i++)
cout << "#" << i+1 << "child name: " << f.child[f.num_of_childs]->name << "child id: " << f.child[f.num_of_childs]->id << endl;
}
I believe there should be:
void print_child (const Family f) //Question 1
{
for (int i=0; i<f.num_of_childs; i++)
cout << "#" << i+1 << "child name: " << f.child[i]->name << "child id: " << f.child[i]->id << endl;
}
i is always smaller then f.num_of_childs, so code will not reach uninitialized memory.
Apart of this there is one more small thing.
Usually integer class members get initialized to 0, but this is not guarantied. I would recommend initialization, to ensure that initial value of num_of_childs is 0 when object of Family class is created:
struct Family
{
Person dad;
Person mom;
int num_of_childs = 0;
Person* child[max_num_of_childs];
};
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];
}
}
Here is my code. It's homework but i have done it all. But the search function does not work. I know Im doing something wrong in it. The user has to enter a name to search and it should display the whole entry. There is an error at the void search function which says "error C2120: 'void' illegal with all types"
// Awholenew world.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<process.h>
#include<iomanip>
#include<stdio.h>
#include<string.h>
#include<fstream>
#include<sstream>
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
struct main
{
char FName[20];
char Last[20];
char Address[30];
long Phone;
int ID;
long CNIC;
}obj;
class Main
{
public:
virtual void init()
{
cin.getline(obj.FName,20);
cout << "Enter Name: ";
cin.getline(obj.Last,20);
cout << "Enter Address: ";
cin.getline(obj.Address, 30);
cout << "Enter Phone: ";
cin>>obj.Phone;
}
void view()
{
cout << "Name: " << obj.Last<< endl;
cout << "Address: " << obj.Address << endl;
cout << "Phone: " << obj.Phone << endl;
cout << "CNIC: " << obj.CNIC << endl;
cout << "ID: " << obj.ID << endl;
}
virtual void search()
{
char choice4;
char Target[20];
int Found=0;
fstream fin;
if (fin.open("Main.txt", ios::in| ios::out) == NULL)
cout<<"File is empty" << endl;
else
cout<<"Enter Name: " << endl;
cin.getline(Target, 20);
while(!fin.eof() && Found ==0)
{
fin<< endl << obj.Last << endl <<obj.Address <<endl <<obj.Phone << endl << obj.CNIC << endl << obj.ID;
if (strcmp(Target, obj.Last) == 0)
Found =1;
}
if(Found)
{
Main::view();
}
else if (!Found)
printf("**There is no such Entry**\n");
fin.close();
}
void display()
{
char BUFFER[100];
ifstream fin("Main.txt");
while (!fin.eof())
{
fin.getline(BUFFER, 100);
cout << BUFFER << endl;
}
}
};
class Teacher : public Main
{
public:
void tinit()
{
ofstream fin("Main.txt", ios::app);
Main::init();
cout << "Enter CNIC of Teacher" << endl;
cin>>obj.CNIC;
fin<< endl << obj.Last << endl << obj.Address << endl << obj.Phone << endl << "Teacher CNIC: " << obj.CNIC << endl;
}
};
class Student : public Main
{
public:
void sinit()
{
ofstream fin("Main.txt", ios::app);
Main::init();
cout << "Enter ID of Student" << endl;
cin>>obj.Phone;
fin<< endl << obj.Last <<endl << obj.Address << endl << obj.Phone << endl << "Student ID" << obj.ID << endl;
}
};
class Employee : public Main
{
public:
void einit()
{
ofstream fin("Main.txt", ios::app);
Main::init();
cout << "Enter Employee CNIC" << endl;
cin>>obj.CNIC;
fin << endl << obj.Last <<endl << obj.Address << endl << obj.Phone << endl << "Employee CNIC: " << obj.CNIC << endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Main* myarr[100];
Teacher* tearr[100];
Student* starr[100];
Employee* emarr[100];
int var=0;
int choice;
char input;
start:
printf("===============MAIN MENU===============");
printf("\n[1]Insert\n[2]Display All\n[3]Search\n[4]Exit\n");
printf("=======================================");
printf("\n\nEnter Your Choice: ");
cin >> choice;
switch(choice)
{
case 1:
label:
cout<< "Enter Choice s/t/e" << endl;
cin >> input;
if (input == 't')
tearr[var]->tinit();
if (input == 's')
starr[var]->sinit();
if (input == 'e')
emarr[var]->einit();
cout << " Enter another (y/n)? ";
cin >> input;
if (input == 'y')
goto label;
else
goto start;
break;
case 2:
myarr[var]->display();
break;
case 3:
myarr[var]->search();
break;
case 4:
cout << "Are you sure? y/n" << endl;
char in;
cin>>in;
if (in=='y')
getch();
else goto start;
break;
default:
return 0;
break;
return 0;
}
}
One problem I can see is:
fstream fin;
if (fin.open("Main.txt", ios::in| ios::out) == NULL)
The function fstream::open is declared as:
void open ( const char * filename,
ios_base:openmode mode = ios_base::in | ios_base::out );
as you can see its return type is void, so you cannot use it in the if expression.
The most glaring thing I noticed is that it looks like the struct of type 'main' stored in the obj variable is outside of any particular class instance. This means that every single object of type Main, Employee, Student or Teacher is only writing to a single obj outside of its own scope. ALL Employees, Teachers, Students, 'Mains' will be sharing whatever FName, Last, Address values you wrote last.
What you probably want is for each instance of Main to have its own FName, Last, Address, etc...
class Main
{
struct main
{
char FName[20];
char Last[20];
char Address[30];
long Phone;
int ID;
long CNIC;
}obj;
public:
virtual void init()
{
... rest of code here
please reduce your post to the main problem. if you just list everything in a single "code" block. noone can understand what you do.
1: don't use "goto" !(!!!!!)
2: you sould realy use bool instead of int for flags
3: don't use main / Main for structs or class names. it's actually reserved for the mainmathod (on some systems) and it is confusing to have one class and one struct with the same name (ignoring the case)
4: you're accessing the function Main::view(); in your search method. but on which object ? normaly you should load the data to an object and accessing it via this->view();
5: the whole concept appear wired. why are you using a textfile and a polymorphic class hierarchy ? the textfile dumps the hierarchy. maybe you should reconsider your class concept.
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.