0

I've tried everything i know to get it work right but i don't know how to make this program to get terminated when escape key get pressed any help i know i can use getch or getchar to get the key stroke of esc key but i also want it to be working with my other inputs too...

I'm a beginner. Any ideas? How it can be done with this program?

#include <bits/stdc++.h>
#include <conio.h>

using namespace std;

class Node {
public:
        int roll;
        string Name;
        string address;
        int contact;
        string email;
        Node* next;
};

Node* head = new Node();

bool check(int x)
{
        if (head == NULL)
                return false;

        Node* t = new Node;
        t = head;

        while (t != NULL) {
                if (t->roll == x)
                        return true;
                t = t->next;
        }

        return false;
}

void Insert_Record(int roll, string Name,
                   string address, int contact, string email)
{
        if (check(roll)) {
                cout << "Student with this "
                     << "record Already Exists\n";
                return;
        }
        Node* t = new Node();
        t->roll = roll;
        t->Name = Name;
        t->address = address;
        t->contact = contact;
        t->email = email;
        t->next = NULL;

        if (head == NULL|| (head->roll >= t->roll)) {
                t->next = head;
                head = t;
        }

        else {
                Node* c = head;
                while (c->next != NULL
                        && c->next->roll < t->roll) {
                        c = c->next;
                }
                t->next = c->next;
                c->next = t;
        }

        cout << "Record Inserted "
                << "Successfully\n";
}

void Search_Record(int roll)
{
        if (!head) {
                cout << "No such Record "
                        << "Avialable\n";
                return;
        }

        else {
                Node* p = head;
                while (p) {
                        if (p->roll == roll) {
                                cout << "Roll Nmuber\t"
                                        << p->roll << endl;
                                cout << "Name\t\t"
                                        << p->Name << endl;
                                cout << "Address\t"
                                        << p->address << endl;
                                cout << "Contact\t\t"
                                        << p->contact << endl;
                                cout << "Email\t\t"
                                        << p->email << endl;
                                return;
                        }
                        p = p->next;
                }

                if (p == NULL)
                        cout << "No such Record "
                             << "Avialable\n";
        }
}

int Delete_Record(int roll)
{
        Node* t = head;
        Node* p = NULL;

        // Deletion at Begin
        if (t != NULL && t->roll == roll) {
                head = t->next;
                delete t;

                cout << "Record Deleted "
                        << "Successfully\n";
                return 0;
        }

        // Deletion Other than Begin
        while (t != NULL && t->roll != roll) {
                p = t;
                t = t->next;
        }

        if (t == NULL) {
                cout << "Record does not Exist\n";
                return -1;
                p->next = t->next;

                delete t;
                cout << "Record Deleted "
                     << "Successfully\n";

                return 0;
        }
}

void Show_Record()
{
        Node* p = head;
        if (p == NULL) {
                cout << "No Record "
                        << "Available\n";
        }
        else {
                cout << "Roll-No\tName\tAddress\tContact\tEmail \n";

                while (p != NULL) {
                        cout<<""<< p->roll <<"\t\t"<< p->Name << "\t\t"<< p->address << "\t\t"<< p->contact << "\t\t"<< p->email << endl;
                        p = p->next;
                }
        }
}

int main()
{
        head = NULL;
        string Name, address, email;
        int Roll, contact;

        while (true) {
                cout << "\n\t\tWelcome "
                                "\n\n\tPress\n\t1 to Store New Data\n\t2 To Display the Record\n\t4 To delete a Data\n\t5 Press Escape Key to Exit\n";
                cout << "\nEnter your Choice\n";

                int Choice;
                
                cin>>Choice;
                if (Choice == 1) {
                        cout << "Enter Roll Number of Student\n";
                        cin >> Roll;
                        cout << "Enter Name of Student\n";
                        cin >> Name;
                        cout << "Enter The Address of Student \n";
                        cin >> address;
                        cout << "Enter  Contact of Student\n";
                        cin >> contact;
                        cout<< "Enter Email Address of the Student\n";
                        cin>> email;
                        Insert_Record(Roll, Name, address, contact, email);
                }
                else if (Choice == 2) {
                        cout << "Enter Roll Number of Student whose "
                                        "record you want to Search\n";
                        cin >> Roll;
                        Search_Record(Roll);
                }
                else if (Choice == 3) {
                        cout << "Enter Roll Number of Student whose "
                                        "record is to be deleted\n";
                        cin >> Roll;
                        Delete_Record(Roll);
                }
                else if (Choice == 0) {
                        exit (0);
                }
                else {
                        cout << "Invalid Choice "
                                << "Try Again\n";
                }
        }
        return 0;
}

Yun
  • 3,056
  • 6
  • 9
  • 28
  • `std::cin`, used like this, will read one character from the input buffer. So, if the user types "" you should be able to catch this by checking for character value 27. If you want to detect _key presses_ directly, then use something like `kbhit`. See [here](https://stackoverflow.com/questions/11987717/c-cin-keypress-event). – Yun Aug 19 '21 at 12:55

1 Answers1

0

As you are already using conio.h, this answer probably has all you need.
how to detect the ESC key in C?

Be aware that conio/_getch may well "steal" characters from the cin stream, so you might be simpler to remove cin, and only use _getch/_kbhit().

EDIT: in response to comment

There are few design decisions to be made here. By trying to bypass the limitations of the C++ model, you are entering the slightly obscure world of OS-Specific character codes. You will need to decide how many of these codes you want to support. This really comes down to usability.
Do you want to support backspace/delete?
How about cursor keys?
How about tabs?
The table here gives some sort of idea how complex this can get. However, maybe you don't need it that complex? It's up to you.

If it were me, I'd aim to store a line at a time, in a std::string. On a newline character, make a std::istringstream to do complex input parsing.

Like all user-interface design, it's fun to play with, but is often a lot of work to make perfect.

Tiger4Hire
  • 1,065
  • 5
  • 11