这是文本文件数据:
值78,2,98是productID。
78 MINERALWATER 123 345.00
2 RAYBAN 2 450.00
98 ADIDASWATCH 19 200.00
如果用户输入的输入与上面的productID相同。我怎么打印,“产品已经存在!”
以下是我在int addProduct()
中已经改变的代码部分。
printf("Enter product ID : ");
scanf(" %d", &a.id);
productID = a.id;
fp = fopen("inventory.txt","r");
while(fscanf(fp, "%d %s %d %f", &a.id, a.name, &a.quantity, &a.price)==4)
{
///The Output Came From Text File Must Be Same As User Instructed
if(a.id == productID)
{
printf("The item already exist!");
}
else{
gotoxy(22,10);
printf("Enter product name : ");
scanf(" %s", a.name);
gotoxy(22,12);
printf("Enter product quantity : ");
scanf(" %d", &a.quantity);
gotoxy(22,14);
printf("Enter product price : ");
scanf(" %f", &a.price);
gotoxy(22,17);
///Store @ Write Product Data Inside FP Text File
fprintf(fp, "%d %s %d %f\n\n", a.id, a.name, a.quantity, a.price);
printf("Record saved!\n\n");
}
}
用户输入值2并存储在a.id
。然后,编译器扫描整个文件,如果a.id = productID
,则打印项已经存在。否则,继续添加新产品。
但是,它失败了。编译器继续添加数据,尽管用户输入值2和文本文件变为:
78 MINERALWATER 123 345.00
2 RAYBAN 2 450.00
98 ADIDASWATCH 19 200.00
2 NEWITEM 4 219.00
如果存在相同的productID,那么这是我的完整代码而不会出现循环争议:兴趣点在int addProduct()
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <ctype.h>
#include <conio.h>
///Function Prototype
void gotoxy( int column, int line );
int main();
int addProduct();
int displayProduct();
int searchProduct();
int reorderProduct();
int updateProduct();
///Global Variables
struct product
{
int quantity, reorder, i, id;
char name[20];
float price;
};
COORD coord = {0, 0};
///GotoXY Function
void gotoxy (int x, int y)
{
coord.X = x; coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
///Main Function
int main()
{
int choice;
///GotoXY Output
gotoxy(17,5);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2 SYZ INVENTORY PROGRAM \xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
gotoxy(17,20);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
///Menu Display
gotoxy(22,8);
printf("1. Add Product\n\n");
gotoxy(22,10);
printf("2. Display Product\n\n");
gotoxy(22,12);
printf("3. Search Product\n\n");
gotoxy(22,14);
printf("4. Reorder Level of Product\n\n");
gotoxy(22,16);
printf("5. Update Product\n\n");
gotoxy(22,18);
printf("6. Exit\n\n");
///Choice Input
gotoxy(22,22);
printf("Please Enter Your Choice : ");
scanf(" %d", &choice);
///Choice Using Switch Case
switch(choice)
{
case 1 : addProduct();
break;
case 2 : displayProduct();
break;
case 3 : searchProduct();
break;
case 4 : reorderProduct();
break;
case 5 : updateProduct();
break;
case 6 : break;
default : system("cls");
main();
}
return (0);
}
///Add Product Function
int addProduct()
{
///File Pointer
FILE * fp;
int number;
char checker;
struct product a;
///Clear Screen On Command Prompt
system("cls");
///FP Text File is Open. A = Append
fp = fopen("inventory.txt","a+t");
///Do-While Loop For Product Input
do
{
system("cls");
///GotoXY Output
gotoxy(17,5);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2 SYZ INVENTORY PROGRAM \xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
gotoxy(17,20);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
///4 Input Of Add Product | Product ID | Product Name | Product Quantity | Product Price |
gotoxy(22,8);
printf("Enter product ID : ");
scanf(" %d", &a.id);
gotoxy(22,10);
printf("Enter product name : ");
scanf(" %s", a.name);
gotoxy(22,12);
printf("Enter product quantity : ");
scanf(" %d", &a.quantity);
gotoxy(22,14);
printf("Enter product price : ");
scanf(" %f", &a.price);
gotoxy(22,17);
///Store @ Write Product Data Inside FP Text File
fprintf(fp, "%d %s %d %f\n\n", a.id, a.name, a.quantity, a.price);
printf("Record saved!\n\n");
///Close FP Text File
fclose(fp);
///Choice Input For User Enter New Product Or Back To Main
gotoxy(22,22);
printf("Do you want to enter new product? Y / N : ");
scanf(" %c", &checker);
///Change To Uppercase, If User Enter Lowercase Character
checker = toupper(checker);
system("cls");
}
///If Checker Input is Y @ Yes : Continue Loop Adding Product
while(checker=='Y');
///If Checker Input Is N @ No : Back To Main
if(checker == 'N')
{
main();
}
else
{
do
{
system("cls");
///GotoXY Output
gotoxy(17,5);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2 SYZ INVENTORY PROGRAM \xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
gotoxy(17,20);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
gotoxy(18,8);
printf(">>> Wrong Input! Please Enter Y Or N Only! <<<");
gotoxy(19,12);
printf("Do You Want To Enter New Product? Y / N : ");
scanf(" %c", &checker);
///Change To Uppercase, If User Enter Lowercase Character
checker = toupper(checker);
}
///Keep On Looping If User Enter Other Than Y Or N.
while(checker != 'Y' && checker != 'N');
///If Input Y : Add Product
if(checker == 'Y') { addProduct(); }
///If Input N : Back To Main
if(checker == 'N') { system("cls");
main();
}
}
return(0);
}
///Display Product Function
int displayProduct()
{
///File Pointer
FILE * fp;
struct product a;
system("cls");
///FP Text File is Open. R = Read Only
fp = fopen("inventory.txt","r");
///GotoXY Output
gotoxy(17,5);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2 SYZ INVENTORY PROGRAM \xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
gotoxy(5,6);
printf("======================================================================");
///Table Title
gotoxy(5,7);
printf("Product ID\t\t Product Name\t\t Quantity\t Unit Price\n");
gotoxy(5,8);
printf("======================================================================");
///Content Of Text File Is Read And Store Into Variables
gotoxy(0,10);
while(fscanf(fp, "%d %s %d %f", &a.id, a.name, &a.quantity, &a.price)==4)
{
///List Of Product From Text File Is Display On Command Prompt
printf("\t%-10d\t %-12s\t\t %8d\t %8.2f\n\n", a.id, a.name, a.quantity, a.price);
}
///Close FP Text File
fclose(fp);
///GotoXY Output
printf("\t\t \xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
printf("\n\n\t\t >> Press any key to return to Main Menu <<");
getch();
system("cls");
///Back To Main Function
main();
return (0);
}
int searchProduct()
{
///File Pointer
FILE * fp;
system("cls");
struct product a;
int productID;
///FP Text File is Open. R = Read Only
fp = fopen("inventory.txt","r");
///GotoXY Output
gotoxy(17,5);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2 SYZ INVENTORY PROGRAM \xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
gotoxy(17,20);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
///Input Product ID Want To Search
gotoxy(22,8);
printf("Please enter product ID : ");
scanf(" %d", &productID);
gotoxy(5,10);
printf("======================================================================");
///Table Title
gotoxy(5,12);
printf("Product ID\t\t Product Name\t\t Quantity\t Unit Price\n");
gotoxy(5,14);
printf("======================================================================");
gotoxy(0,16);
///Content Of Text File Is Read And Store Into Variables
while(fscanf(fp, "%d %s %d %f", &a.id, a.name, &a.quantity, &a.price)==4)
{
///The Output Came From Text File Must Be Same As User Instructed
if(a.id == productID)
{
printf("\t%-10d\t %-12s\t\t %8d\t %8.2f\n\n", a.id, a.name, a.quantity, a.price);
}
}
///Close FP Text File
fclose(fp);
gotoxy(17,20);
printf("\n\n\t\t >> Press any key to return to Main Menu <<");
getch();
system("cls");
main();
return(0);
}
int reorderProduct()
{
///File Pointer
FILE * fp;
system("cls");
struct product a;
int productQuantity;
///FP Text File is Open. R = Read Only
fp = fopen("inventory.txt","r");
///GotoXY Output
gotoxy(17,5);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2 SYZ INVENTORY PROGRAM \xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
///Input Minimum Product Quantity
gotoxy(22,8);
printf("Please enter minimum reorder level : ");
scanf(" %d", &productQuantity);
gotoxy(5,10);
printf("======================================================================");
///Table Title
gotoxy(5,12);
printf("Product ID\t\t Product Name\t\t Quantity\t Unit Price\n");
gotoxy(5,14);
printf("======================================================================");
gotoxy(0,16);
///Content Of Text File Is Read And Store Into Variables
while(fscanf(fp, "%d %s %d %f", &a.id, a.name, &a.quantity, &a.price)==4)
{
if(productQuantity >= a.quantity)
{
///The Output Came From Text File Must Be Greater Or Equal As User Instructed (Item Need To Be Reorder)
printf("\t%-10d\t %-12s\t\t %8d\t %8.2f\n\n", a.id, a.name, a.quantity, a.price);
}
}
///Close FP Text File
fclose(fp);
///GotoXY Output
printf("\t\t \xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
printf("\n\n\t\t >> Press any key to return to Main Menu <<");
getch();
system("cls");
main();
return (0);
}
int updateProduct()
{
///File Pointer
FILE *fp, *fp2;
system("cls");
struct product a;
int productID;
///FP Text File is Open. R = Read Only
fp = fopen("inventory.txt","r");
rewind(fp);
///FP Text File is Open. R = Read Only
fp2 = fopen("temporary.txt" ,"a");
///GotoXY Output
gotoxy(17,5);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2 SYZ INVENTORY PROGRAM \xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
gotoxy(17,20);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
///Input Product ID To Be Update
gotoxy(22,8);
printf("Enter product ID : ");
scanf(" %d", &productID);
///Content Of Text File FP Is Read
while (!feof(fp))
{
///Content Is Store Into Variables
fscanf(fp, "%d %s %d %f", &a.id, a.name, &a.quantity, &a.price);
if (feof(fp)) { continue; }
if (a.id == productID) continue;
fprintf(fp2, "%d %s %d %f\n\n", a.id, a.name, a.quantity, a.price);
}
///Close FP Text File
fclose(fp);
fclose(fp2);
///Inventory Text File Is Remove
remove("inventory.txt");
///Temporary Text File Is Rename Into Inventory Text File
rename("temporary.txt", "inventory.txt");
///FP Text File is Open. R = Append
fp = fopen("inventory.txt","a+t");
///3 Input Of Add Product | Product Name | Product Quantity | Product Price |
gotoxy(22,10);
printf("Enter product name : ");
scanf(" %s", a.name);
gotoxy(22,12);
printf("Enter product quantity : ");
scanf(" %d", &a.quantity);
gotoxy(22,14);
printf("Enter product price : ");
scanf(" %f", &a.price);
gotoxy(22,17);
///Store @ Write Product Data Inside FP Text File
fprintf(fp, "%d %s %d %f\n\n", productID, a.name, a.quantity, a.price);
printf("Record saved!\n\n");
///Close FP Text File
fclose(fp);
printf("\n\n\t\t >> Press any key to return to Main Menu <<");
getch();
system("cls");
main();
return(0);
}