How to call a class method/function in switch statement C++

closed account (9wkE8vqX)
#include <fstream>
#include <string>
#include <iostream>
#include <iomanip>
using namespace std;

class Record2{
public:
static void menu();
void enter(int, int);
void view(int);
void update(string, int, int, int);
void del(string, int, int, int, int);
void search(string, int);
private:

};
struct addressbook
{
string fullname, contact, email;
}; addressbook record;
void Record2::menu()
{
int choice;
system("color 17"); system("cls");
cout << "\n_______________________MAIN MENU_______________________\n\n\n";
cout << "1. Add a contact \n";
cout << "2. View all contacts\n";
cout << "3. Update a contact info\n";
cout << "4. Delete a contact info\n";
cout << "5. Search a contact info\n";
cout << "6. Exit";
cout << "\n\nNote: To choose a performance please select the number you want\n\n";
cout << "Enter your Choice: ";
cin >> choice;

switch (choice)
{
case 1: void enter();
break;
case 2: void view();
break;
case 3: void update();
break;
case 4: void del();
break;
case 5: void search();
break;
default:
break;
}
}
void Record2::enter(int choice, int ch2){
Entry:
fflush(stdin);
system("cls"); system("color 17");
cout << "\n_______________________CREATING NEW ENTRY_______________________\n\n\n";
fname:
cout << "Full Name: ";
getline(cin, record.fullname);
if (record.fullname == ""){
cout << "\nPlease input correctly! You cannot leave this info a blank\n" << endl; goto fname;}
contact:
cout << "Contact Number: ";
getline(cin, record.contact);
if (record.contact == ""){ cout << "\nPlease input correctly! You cannot leave this info a blank\n" <<endl; goto contact; }
email:
cout << "E-Mail Address: ";
getline(cin, record.email);
if (record.email == ""){ cout << "\nPlease input correctly! You cannot leave this info a blank\n" <<endl; goto email; }
cout << "\nAre you sure you want to save this?\n\n1. Yes\n2. No\n\nChoice: "; cin >> ch2;
if (ch2 == 1){ goto saving; }
if (ch2 == 2) { goto Entry; }

saving:
cout << "\n\nEntry Successfully Added! ";
ofstream outfile("AddressBook.txt", ios_base::out | ios_base::app);
if (outfile.is_open()){
outfile << record.fullname << endl;
outfile << record.contact << endl;
outfile << record.email << endl;
}
outfile.close();
cout << "\n\n1. Add Another Entry\n2. Return to Main Menu\n3. Exit The Program\n\nChoice: ";
cin >> choice;
if (choice == 1){ goto Entry; }
if (choice == 2){ void menu(); }
if (choice == 3){ exit(0); }
else
cout << "\n\nUnavailable Input, Press Enter to Exit";
system("pause>0");
}
void Record2::view(int choice){
view:
system("cls");
ifstream infile("Addressbook.txt");
cout << "\n_____________________________________ADDRESS BOOK RECORD_______________________________________\n\n";
cout << " Full Name: Contact Number: E-Mail Address: ";
cout << "\n-------------------------------------------------------------------------------------------------\n";
if (infile.is_open())
{
while (getline(infile, record.fullname), getline(infile, record.contact), getline(infile, record.email))
{
cout << setiosflags(ios::right) << setw(24) << record.fullname;
cout << setiosflags(ios::right) << setw(37) << record.contact;
cout << setiosflags(ios::right) << setw(36) << record.email << endl;
}
infile.close();
}
else {cout << "File is not open!" << "\n";}
cout << "\n1. Refresh\n2. Return to Main Menu\n3. Exit the Program\n\nChoice: ";
cin >> choice;
if (choice == 1){goto view;}
if (choice == 2){void menu();}
if (choice == 3){ exit(0);}
else
cout << "\n\nUnavailable Input, Press Enter to Exit";
system("pause>0");
}
void Record2::update(string tname, int choice, int ch2, int x = 0)
{
UpdateRecord:
system("cls");
fflush(stdin);
cout << "\n_________________________EDIT/CHANGE/UPDATE ENTRY________________________\n";
cout << "\nEnter the First Name of the entry you want to Update: ";
getline(cin, tname);
ifstream infile("AddressBook.txt");
ofstream temp("temp.txt");
while (getline(infile, record.fullname), getline(infile, record.contact), getline(infile, record.email))
{
if (tname != record.fullname){
temp << record.fullname << endl;
temp << record.contact << endl;
temp << record.email << endl;
}
if (tname == record.fullname){
x = 1;
fname:
cout << "\nFull Name: ";
getline(cin, record.fullname);
if (record.fullname == ""){
cout << "\nPlease input correctly! You cannot leave this info a blank\n" << endl; goto fname;}
contact:
cout << "Contact Number: ";
getline(cin, record.contact);
if (record.contact == ""){ cout << "\nPlease input correctly! You cannot leave this info a blank\n" << endl; goto contact; }
email:
cout << "E-Mail Address: ";
getline(cin, record.email);
if (record.email == ""){ cout << "\nPlease input correctly! You cannot leave this info a blank\n" << endl; goto email; }
cout << "\n\nAre you sure you want to save this changes? " << endl;
cout << "\n1. Yes\n2. No";
cout << "\n\nChoice: ";
cin >> ch2;
if (ch2 == 1){
temp << record.fullname << endl;
temp << record.contact << endl;
temp << record.email << endl;
}
else if (ch2 == 2){
goto choice;
}
}
}
infile.clear();
infile.seekg(0, ios::beg);
infile.close();
temp.close();
remove("AddressBook.txt");
rename("temp.txt", "AddressBook.txt");
if (x == 0)
{
cout << "\n\nThere is no Data with name you entered." << endl;
goto choice;
}
else
{
cout << "\n\nData has been succesfully Edited." << endl;
goto choice;
}
choice:
cout << "\n\n1. Edit Another Entry\n2. Return to Main Menu\n3. Exit The Program\n\nChoice: ";
cin >> choice;
if (choice == 1){ goto UpdateRecord; }
if (choice == 2){ void menu(); }
if (choice == 3){ exit(0); }
}
void Record2::del(string searchname, int ch, int choice, int fchoice, int x = 0)
{
// x "counter" to check if user entered wrong name
DeleteRecord:
system("cls");
fflush(stdin);
cout << "\n_________________________DELETE________________________\n";
cout << "\n\n1. Delete a specific contact entry";
cout << "\n2. Delete all contacts at once";
cout << "\n\nChoice: ";
cin >> fchoice;

if (fchoice == 1){
fflush(stdin);
cout << "\nEnter the Full Name of the entry you want to Erase: ";
getline(cin, searchname);
ifstream infile("AddressBook.txt");
ofstream temp("temp.txt");
while (getline(infile, record.fullname), getline(infile, record.contact), getline(infile, record.email))
{
if (searchname != record.fullname)
{
temp << record.fullname << endl;
temp << record.contact << endl;
temp << record.email << endl;
}
if (searchname == record.fullname){
x = 1;
}
}
infile.clear();
infile.seekg(0, ios::beg);
infile.close();
temp.close();
remove("AddressBook.txt");
ofstream outfile("AddressBook.txt");
if (x == 0)
{
cout << "\nThere is no Contact Entry with name you entered." << endl;
goto choice;
}
else
{
cout << "\nData has been succesfully deleted." << endl;
goto choice;
}
}
else if (fchoice == 2){
cout << "\nWarning! This will delete all contacts. Do you want to procees?" << endl;
cout << "\n1. Yes\n2. No\n\nChoice: ";
cin >> ch;
if (ch == 1){
remove("AddressBook.txt");
rename("temp.txt", "AddressBook.txt");
cout << "\nSuccess! All informations has been deleted " << endl;
goto choice;
}
else if (ch == 2){
goto choice;
}
}
choice:
cout << "\n\n1. Delete Again\n2. Return to Main Menu\n3. Exit The Program\n\nChoice: ";
cin >> choice;
if (choice == 1){ goto DeleteRecord; }
if (choice == 2){ void menu(); }
if (choice == 3){ exit(0); }
}
int main(){
Record2 rec;
rec.menu();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
switch (choice) {
    case 1: enter();
        break;
    case 2: view();
        break;
    case 3: update();
        break;
    case 4: del();
        break;
    case 5: search();
        break;
    default:
        break;
}
Last edited on
Topic archived. No new replies allowed.