我正在嘗試製作一個記錄系統,其中我要登錄,使用我的員工ID和密碼登錄,或者如果可能,只需要員工ID。問題是,無論何時我將僱主ID放入一個文本文件中,我的代碼僅限於我的代碼中預定義的用戶名。我如何讓每個僱主的身份證明文件都能被接受並能成功登錄?C程序 - 文件處理限制
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
void mainMenu();
void EnterItem();
void ViewItem();
void SearchItem();
void Exit();
int count=1,ulength,plength;
char username[]="Admin",password[]="1234";
char name1[10],pass[10];
char code[20],name[20],search[20];
int price,qty;
main(){
printf("\nEnter USer ID And Password below(You have Only Three Chances!\nPress Enter To Continue");
getch();
while(count<=3)
{
printf("\nENter User ID:");
scanf("%s",name1);
printf("\nENter Password:");
scanf("%s",pass);
ulength=strcmp(name1,username);
plength=strcmp(pass,password);
if(ulength==0&&plength==0)
{
printf("\nWelcome %s",name1);
break;
}
else
{
printf("\nUsername And Password is Invalid!\n You Have %d more Chances/s.",3-count);
}
getch();
count++;
}
if(count==4)
printf("Maximum of three(3)Try only!");
getch();
mainMenu();
EnterItem();
ViewItem();
SearchItem();
Exit();
getch();
}
void mainMenu(void){
char choice;
printf("\t*************************************\n");
printf("\t******========================*******\n");
printf("\t******==!!INVENTORY SYSTEM!!==*******\n");
printf("\t******========================*******\n");
printf("\t*************************************\n");
printf("\n");
printf("\nMenu:\n[a]EnterItem\n[b]ViewItem\n[c]SearchItem\n[d]Exit\n");
printf("What do you want to perform?:");
fflush(stdin);
scanf("%c",choice);
switch(choice){
case 'a':
EnterItem();
break;
case 'b':
ViewItem();
break;
case 'c':
SearchItem();
break;
case 'd':
Exit();
break;
default:
printf("Invalid Choice");
}
}
void EnterItem(void){
FILE *MyFile;
char opt;
do{
printf("Enter Item Code:");
fflush(stdin);
scanf("%s",code);
printf("Enter Item Name:");
fflush(stdin);
scanf("%s",name);
printf("Enter Item Price:");
fflush(stdin);
scanf("%i",&price);
printf("Enter Item Quantity:");
fflush(stdin);
scanf("%i",&qty);
MyFile=fopen("MyFiles.txt","a+");
fprintf(MyFile,"%s\t%s\t%i\t%i\n",code,name,price,qty);
printf("Item Saved!!\n");
printf("Do you want to Add More?:[y/n]");
fflush(stdin);
scanf("%c",&opt);
}while(opt=='Y'||opt=='y');
printf("Do you want to Go Back To main?:[y/n]");
fflush(stdin);
scanf("%c",&opt);
if(opt=='y'||opt=='Y')
mainMenu();
else
printf("Invalid Choice!!");
}
void ViewItem(void){
FILE *MyFile;
int c;
if((MyFile=fopen("MyFiles.txt","r"))==NULL)
{
printf("Error Reading File!!");
}
while((c=fgetc(MyFile))!=EOF)
printf("%c",c);
printf("Go to Main Menu[y/n]:");
fflush(stdin);
scanf("%c",opt);
if(opt=='y'||opt=='Y')
mainMenu();
else();
fclose(MyFile);
getch();
}
void SearchItem(void){
FILE *MyFile;
MyFile=fopen("MyFiles.txt","a+");
printf("Enter the Item Code to Search:");
fflush(stdin);
scanf("%s",search);
while(!feof(MyFile))
{
fscanf(MyFile,"%s %s %i %i",code,name,price,qty);
if(strcmp(search,code)==0)
{
printf("Item Code: %s\n",code);
printf("Item Name: %s\n",name);
printf("Item Price: %i\n",price);
printf("Item Quantity: %i\n",qty);
break;
printf("Go Back To main Menu[y/n]:");
fflush(stdin);
scanf("%s",opt);
if(opt=='y'||opt=='Y')
mainMenu();
else
}
}
fclose(MyFile);
}
void Exit(void){
getch();
}
完成:)剛剛編輯的代碼。 – Xelza
除了在標準輸入框中調用fflush的其他錯誤,這會導致未定義的行爲。 –