我正在尝试滚动菜单。我的问题在于
GetAsyncKeyState(VK_RETURN)
输入信息后,它不会返回我的菜单。它突然要求我输入另一个信息(这是无穷无尽的)。
我不确定它是否带有GetAsyncKeyState(VK_RETURN)语句,因为每次我尝试输入最后一个信息,即“输入年份”时,它会返回菜单,但会自动按下回车键“输入学生”选项。
#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);
}
}
什么似乎是问题?
答案 0 :(得分:0)
通常,混合C ++样式流输入和输出,c样式I / O,conio样式键盘I / O和系统调用屏幕clr并不是一个好主意。四种不同的东西不能保证在一起很好地发挥作用。