gpt4 book ai didi

c++ - GetAsyncKeyState(VK_RETURN)遭遇

转载 作者:行者123 更新时间:2023-11-30 17:31:31 27 4
gpt4 key购买 nike

我正在尝试做一个滚动菜单。我的问题在于

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);
}


}

问题出在哪里?

最佳答案

一般来说,混合使用 C++ 风格的流输入和输出、C 风格的 I/O、conio 风格的键盘 I/O 和系统调用屏幕 clr 并不是一个好主意。四种不同的东西不能保证一起发挥良好的作用。

关于c++ - GetAsyncKeyState(VK_RETURN)遭遇,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24583802/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com