0
我正在嘗試執行滾動菜單。我的問題是在GetAsyncKeyState(VK_RETURN)遇到
GetAsyncKeyState(VK_RETURN)
在我輸入我的信息後,它不回到我的菜單。它突然問我輸入另一個信息(這是無止境的)。
我不確定它是否使用GetAsyncKeyState(VK_RETURN)語句,因爲每次我嘗試放入最後一個信息,即「輸入年份」時,它都會返回菜單,但會自動按下Enter在「輸入學生」選項上。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#include<windows.h>
#include<iostream>
using namespace std;
typedef struct{
char fname[24],lname[16],mi,course[16];
int year;
unsigned long ID;
}studtype;
void getStudent(void);
void getHeader();
void readToFile(void);
// FILE * openFile(FILE *);
// void closeFile(FILE *);
// studtype *writeFile(studtype *,FILE *);
int main(void)
{
char* Menu[2];
Menu[0] = "Input Student";
Menu[1] = "Display Student";
int choice;
int pointer = 0;
while(true)
{
system("cls");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
printf("Main Menu\n\n");
for (int i = 0; i < 2; ++i)
{
if (i == pointer)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 11);
cout << Menu[i] << endl;
}
else
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
cout << Menu[i] << endl;
}
}
while(true)
{
if (GetAsyncKeyState(VK_UP) != 0)
{
pointer -= 1;
if (pointer == -1)
{
pointer = 1;
}
break;
}
else if (GetAsyncKeyState(VK_DOWN) != 0)
{
pointer += 1;
if (pointer == 2)
{
pointer = 0;
}
break;
}
else if (GetAsyncKeyState(VK_RETURN) != 0)
{
switch (pointer)
{
case 0:
{
printf("\n\n");
getStudent();
} break;
case 1:
{
printf("\n\n");
getHeader();
readToFile();
Sleep(5000);
} break;
}
break;
}
} Sleep(150);
}
}
void getHeader()
{
printf("%-10s","ID");
printf("%-10s","FirstName");
printf("%-10s","LastName");
printf("%-20s","MiddleInitial");
printf("%-15s","Course");
printf("%-15s","Year");
printf("%-10s","-------");
printf("%-10s","---------");
printf("%-10s","--------");
printf("%-10s","----------");
printf("%-10s","-------");
printf("%-10s","---------");
printf("%10s","-------");
printf("%s"," ");
}
void getStudent(void)
{
studtype stud;
FILE * fp;
if((fp = fopen("sample.txt","a"))!=NULL)
{
system("cls");
printf("Enter Student ID: ");
scanf("%d",&stud.ID);
printf("Enter FirstName: ");
fflush(stdin);
gets(stud.fname);
printf("Enter LastName: ");
fflush(stdin);
gets(stud.lname);
printf("Enter MI: ");
scanf("%c",&stud.mi);
printf("Enter Course: ");
fflush(stdin);
gets(stud.course);
printf("Enter Year: ");
scanf("%d",&stud.year);
fwrite(&stud,sizeof(studtype),1,fp);
fclose(fp);
}
}
void readToFile(void)
{
studtype stud;
FILE * fp;
if((fp = fopen("sample.txt","r"))!=NULL)
{
while(fread(&stud,sizeof(studtype),1,fp))
{
printf("%-10d",stud.ID);
printf("%15s",stud.fname);
printf("%10s",stud.lname);
printf("%10c",stud.mi);
printf("%15s",stud.course);
printf("%10d",stud.year);
printf("%10s","");
}
fclose(fp);
}
}
什麼似乎是問題?
您正在使用GetAsyncKeyState()錯誤,它也可以返回一個非零值,當鍵是*不* *。它的返回值編碼*兩條*信息。它只是不適合使用winapi函數。你已經#including conio.h,你可以使用它的_getch()函數。 ReadConsoleInput()是基礎的winapi函數。 –
@HansPassant我想知道,我嘗試輸入最後一個信息後,如何返回到我的菜單?它有點像雙擊RETURN。 –