如何与If-Else语句中的其他键一起读取Esc键



我已经尝试了我所知道的一切让它正常工作,但我不知道如何使这个程序当转义键被按下时终止任何帮助我知道我可以使用getch或getchar来获得esc键的键击,但我也希望它能与我的其他输入一起工作…

我是初学者。什么好主意吗?这个程序怎么做呢?

#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 Existsn";
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 "
<< "Successfullyn";
}
void Search_Record(int roll)
{
if (!head) {
cout << "No such Record "
<< "Avialablen";
return;
}
else {
Node* p = head;
while (p) {
if (p->roll == roll) {
cout << "Roll Nmubert"
<< p->roll << endl;
cout << "Namett"
<< p->Name << endl;
cout << "Addresst"
<< p->address << endl;
cout << "Contacttt"
<< p->contact << endl;
cout << "Emailtt"
<< p->email << endl;
return;
}
p = p->next;
}
if (p == NULL)
cout << "No such Record "
<< "Avialablen";
}
}
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 "
<< "Successfullyn";
return 0;
}
// Deletion Other than Begin
while (t != NULL && t->roll != roll) {
p = t;
t = t->next;
}
if (t == NULL) {
cout << "Record does not Existn";
return -1;
p->next = t->next;
delete t;
cout << "Record Deleted "
<< "Successfullyn";
return 0;
}
}
void Show_Record()
{
Node* p = head;
if (p == NULL) {
cout << "No Record "
<< "Availablen";
}
else {
cout << "Roll-NotNametAddresstContacttEmail n";
while (p != NULL) {
cout<<""<< p->roll <<"tt"<< p->Name << "tt"<< p->address << "tt"<< p->contact << "tt"<< p->email << endl;
p = p->next;
}
}
}
int main()
{
head = NULL;
string Name, address, email;
int Roll, contact;
while (true) {
cout << "nttWelcome "
"nntPressnt1 to Store New Datant2 To Display the Recordnt4 To delete a Datant5 Press Escape Key to Exitn";
cout << "nEnter your Choicen";
int Choice;

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

由于您已经在使用conio.h,因此这个答案可能已经满足了您的所有需求。
如何检测C中的ESC键?

注意conio/_getch很可能"偷窃"。字符从cin流,所以您可能更容易删除cin,只使用_getch/_kbhit()。

编辑:回复评论

这里需要做的设计决策很少。通过尝试绕过c++模型的限制,您进入了os特定字符代码的稍微模糊的世界。您需要决定要支持多少这样的代码。这真的归结为可用性。
是否支持退格/删除?
光标键呢?
制表符呢?
下面的表格给出了这个过程有多复杂的概念。然而,也许你不需要那么复杂?由你决定。

如果是我,我的目标是在std::string中一次存储一行。在换行符上,创建std::istringstream来执行复杂的输入解析。

就像所有的用户界面设计一样,玩起来很有趣,但通常需要做很多工作才能做到完美。

最新更新