我想扫描一个字符串并检查它是否可以在已编译的结构中找到并返回一些此值。如果不是相关信息。我有以下代码,但我收到错误。
错误发生在案例4中。其余案例5和6我仍在修理它们。
#include <stdio.h>
#include <string.h>
#define Total 200 // Total number of products
#define Len 50 // Lenght of product name
//Creation of a product structure
struct product
{
char name[Len]; //Product's Name
double price; // Product's Price
int stock; // Product's Stock
};
//Start of main function
int main()
{
//Create an array named "products" where each value has a product's struct. Addind the info for each one of them.
struct product products[Total]={"TOMATO, PASTE, DRUM 55 GAL",243.26,151,
"ALMONDS, ROASTED 25 ",70.81,5,
"TEST",58.08,5,
"APPLES",16.9,38,
"APPLES (138 COUNT) ",23.6,14,
"APPLES, FRESH GRANNY SMITH (125 COUNT)",28.77,5,
"APPLES, FRESH, SLICED (100/2 OZ.)",17.75,5,
"APPLES, FRESH, SLICED (200/2 OZ.)",33.83,5,
"APPLES, FRESH, SLICED (64/2 OZ.)",10.92,6,
"APPLES, FUJI (125 COUNT)",32.95,5,
"APPLES, FUJI (138 COUNT)",33.73,55,
"APPLES, FUJI SLICED (200/2 OZ.)",52.04,5,
"APPLES, GRANNY SMITH, SLICED (200/2 OZ.)",52.04,5,
"APPLES, GREEN (138-163 COUNT)",28.77,55,
"APPLES, GREEN, DELICIOUS (125-138 COUNT)",25.1,5,
"APPLES, RED (138-163 COUNT)",26.67,55,
"APPLES, RED, DELICIOUS (125-138 COUNT) ",28.77,5,
"APPLES, RED, DELICIOUS (163 COUNT) ",26.67,65,
"APPLES, ROYAL GALA (125-150 COUNT)",28.77,5,
"APPLES, SLICES, CND 6/10 ",24.22,3,
"APPLES, SLICES, FRZ 30 ",13.35,16,
"APPLESAUCE,CND 6/10 ",18.06,18,
"APRICOT, CUP, FRZ 96/4.5 OZ.",31.41,9,
"APRICOT, FRZ 40",32.43,19,
"APRICOTS, CND 6/10 ",31.01,1,
"APRICOTS, DICED, CND 6/10 ",26.7,1,
"APRICOTS, FRZ 20 ",18.85,14,
"AVOCADOS (48 COUNT)",32.43,14,
"AVOCADOS (60 COUNT) ",35.75,6,
"BEANS, BLKEYE, CND 6/10 ",12.21,11,
"BEANS, DRY, GREAT NORTHERN 25 ",12.09,8,
"BEANS, DRY, NAVY PEA 25",10.89,59,
"BEANS, GARBANZO, CND 6/10 ",19.29,15,
"BEANS, GREEN, CND 6/10 ",19.43,17,
"BEANS, GREEN, FRZ 30 ",18.25,34,
"BEANS, PINTO, CND 6/10 ",17.88,11,
"BEANS, PINTO, DRY 25 ",14.5,51,
"BEANS, RED KDNY, CND 6/10",18.8,16,
"BEANS, REFRIED, CND 6/10 ",21.38,19,
"BEANS, VEG, CND 6/10 ",18.37,18,
"BEEF 4/10",94.33,47,
"BEEF CRUMB SPP",91.63,1,
"BEEF PATTIES SPP 40",81.84,11,
"BEEF, 100 IRR 40",76,47,
"BEEF, BNLS SPECIAL TRM, FRZ, CTN-60 LB",176.86,5,
"BEEF, BNLS, FRESH, COMBO ",42.34,1,
"BEEF, BRD PATTY, CKD, PKG-4/10 LB",75,11,
"BEEF, CND 24/24 OZ. ",114.09,57,
"BEEF, COARSE GROUND, FRZ, BULK ",145.45,7,
"BEEF, GROUND, FRZ 40 ",94.33,47,
"BEEF, PATTIES, FRZ, 3 OZ. 40 ",89.76,11,
"BEEF, PATTIES, LEAN 40 ",94.58,11,
"BEEF, PTY SPP, CKD, HOMESTYLE 40",95.6,13,
"BEEF, SLOPPY JOE, CKD 4/10 ",62.54,15,
"BEEF, TACO FILLING, CKD 4/10",60.25,1,
"BLACKBERRIES (12/6 OZ.)",18.31,71.,
"BLACKBERRIES, MARION, FRZ 30",31.43,1,
"BLUEBERRIES (12/4.4 OZ.) ",24.58,7,
"BLUEBERRIES, CULTIVATED, FRZ ",18.64,35,
"BLUEBERRIES, DRY 10",40.29,11,
"BLUEBERRIES, WILD, FRZN 30",27.29,35,
"BROCCOLI (14 COUNT)",18.31,11,
"BROCCOLI FLORETS (4/5)",35.04,37,
"CABBAGE, GREEN (24 COUNT)",14.12,117,
"CABBAGE, RED, SHREDDED",10.88,11,
"CANTALOUPE, DICED",11.77,6,
"CARROTS, BABY, WHOLE (100/2 OZ.)",15.06,5,
"CARROTS, BABY, WHOLE (4/5)",18.41,18,
"CARROTS, BABY, WHOLE, SNACK PACK (150/2 OZ.)",20.76,1,
"CARROTS, CND 6/10 ",16.02,18,
"CARROTS, FRESH BABY CUTS, BAG (100/2 OZ.)",12.48,5,
"CARROTS, SLICED, FRZ 30 ",21.08,31,
"CARROTS, STICKS (4/5) ",32.95,33,
"CARROTS, WHOLE BABY, BULK (4/5)",16.75,5,
"CARROTS, WHOLE BABY, SNACK (150/2 OZ.)",16.66,1,
"CARROTS,BABY, WHOLE, PEELED (50/3 OZ.)",10.98,5,
"CATFISH STRIPS, FRZ, IQF",199.6,15,
"CAULIFLORETS",10.98,31,
"CAULIFLORETS (12 COUNT)",27.2,5,
"CAULIFLORETS (4/5) ",39.23,58,
"CELERY & CARROT (50/3 OZ.) ",15.11,5,
"CELERY, PASCAL (24-36 COUNT)",18.83,63,
"CELERY, STICKS (4/5)",24.2,1,
"CELERY, STICKS (4/5)",24.58,3,
"CHEDDAR, RED, FAT, YEL 4/10",65.48,6,
"CHEESE, 30 LVS 6/5",46.22,4,
"CHEESE, BARREL 500 ",770.35,5,
"CHEESE, BLEND, SLICED 6/5",46.22,4,
"CHEESE, CHED RD FT, SHD, YELLOW 6/5",48.09,1,
"CHEESE, CHEDDAR, SHRED Y 6/5",46.22,4,
"CHEESE, CHEDDAR, WHITE 4/10 ",65.48,6,
"CHEESE, CHEDDAR, WHITE 40 ",65.48,6,
"CHEESE, CHEDDAR, WHITE 40 ",65.48,6,
"CHEESE, CHEDDAR, YELLOW 4/10 ",65.48,6,
"CHEESE, CHEDDAR, YELLOW 40 ",65.48,6,
"CHEESE, CHEDDAR, YELLOW 40 ",65.48,6,
"CHEESE, MOZZ, LMPS SD 30 ",46.22,4,
"CHEESE, MOZZ, LT SHRD, FROZEN",46.22,4,
"CHEESE, MOZZ, UNFRZ ",73.9536,76,
"CHEESE, MOZZARELLA, FRZ 8/6",73.95,76,
"CHEESE, RDU SKIM, SLC Y 6/5",46.22,48,
"CHEESE, SHRED, RDU-FAT, WHITE 6/5",48.09,4,
"CHEESE, SLICED 6/5",46.22,4,
"CHEESE, SLICED, WHITE 6/5",46.22,4,
"CHEESE, SLICED, YELLOW 6/5",46.22,4,
"CHEESE, SLICED, YELLOW 6/5 ",46.22,4,
"CHERRIES, DRIED 2 8/2",59.53,18,
"CHERRIES, DRIED 4/4",53.18,18,
"CHERRIES, FRZ 30",22.6,3,
"CHERRIES, IQF, FRZ 40 ",34.5,1,
"CHERRIES, RED TART, CND 6/10 ",29.63,18,
"CHICKEN, BONED, CND 12/50 OZ. ",84.53,55,
"CHICKEN, CHILL, LRG 40 BULK ",29.848,3,
"CHICKEN, CHILL, SM 40 BULK ",29.792,3,
"CHICKEN, CUT UP, FRZ 8-PIECE 40 ",32.9,8,
"CHICKEN, DICED, CKD, FRZ 4/10 ",86.62,6,
"CHICKEN, FAJITA ",64.6,17,
"CHICKEN, LEGS, CHILL, CTN",26.8,59,
"CHICKEN, ROASTED",71.15,5,
"CORN, COB, FRZ 96-EAR ",18.86,9,
"CORN, FRZ 30 ",21.04,3,
"CORN, WHOLE KERNEL, CND 6/10 ",18.48,17,
"CORNMEAL, DEG 8/5",10.32,11,
"CORNMEAL, DEGERMED, YLW 4/10 ",7.34,11,
"CRANBERRY, DRIED 5/5 ",43.03,37,
"CRANBERRY, SAUCE, CND 6/10 ",18.29,18,
"CRANBERRY, WHOLE, FZN 40",28.54,55,
"EGGS, WHOLE LIQUID, BULK ",20.736,7,
"EGGS, WHOLE, FRZ 30 ",21.53,1,
"EGGS, WHOLE, FRZ 6/5 ",26.75,1,
"FIGS 24/1",26.08,14,
"FLOUR, ALL PURPOSE 50 ",14.08,18,
"FLOUR, ALL-PURPOSE ",14.08,17,
"FLOUR, BAKERS HARD, WHITE, BULK ",14.12,17,
"FLOUR, BHW 50 ",14.67,16,
"FLOUR, BHW 50 ",14.67,17,
"FLOUR, MASA, YELLOW 50 ",14.795,17,
"FLOUR, WHOLE WHEAT 50 ",13.13,18,
"FLOUR, WW, 4/10 LB BAGS",10.41,1,
"FRT-NUT MIX 24",52.76,156.,
"FRT-NUT MIX 5/5",55.64,167.,
"FRUIT MIX, CND 6/10 ",21.27,18,
"GRAPES, GREEN, SEEDLESS",26.67,11,
"GRAPES, LUNCH BUNCH (123/3 OZ.) ",22.99,11,
"GRAPES, RED (150/2 OZ.)",43.62,1,
"GRAPES, RED, SEEDLESS",22.49,11,
"HAM, CKD, FRZ, CUBE",76.37,15,
"HAM, CKD, FRZ, SLC",80.88,51,
"HAM, FRZ, WATER ADDED 4/10",62.11,51,
"HONEYDEW, DICED",11.51,4,
"KIWIFRUIT",20.92,18,
"KIWIFRUIT, FRESH (108 COUNT)",22.99,1,
"LETTUCE, NAKED (24 COUNT)",25.63,115,
"LETTUCE, ROMAINE (24 COUNT)",25.63,115,
"LETTUCE, SHREDDED (4/5)",24.58,48,
"MACARONI, ELBOW 20 ",11.08,3,
"MACARONI, ROTINI, SPIRAL 20 ",11.23,33,
"MACARONI, ROTINI, WHOLE GRAIN 20",12.26,33,
"MACARONI, WHOLE GRAIN 20",10.38,3,
"MILK, NOFAT, DRY REG. BULK 50 ",60.14,5,
"MILK, NONFAT, DRY ",60.14,5,
"OATS, ROLLED 12/3 ",17.45,168,
"ONION, GREEN",19.72,1,
"ORANGE J, TANK",124.15,11,
"ORANGE JUICE, CAN-12/46 OZ",14.32,1,
"ORANGE JUICE, FRZ, CONC.12/32 OZ.",26.85,76,
"ORANGE JUICE, SINGLE 70/4 OZ. ",8.5,5,
"ORANGE, DICED",11.21,3,
"ORANGES (113 COUNT)",17.26,1,
"ORANGES (138 COUNT)",17.26,15,
"ORANGES, CTN (34-39 LB)",13.33,5,
"ORANGES, DICED",27.47,5,
"ORANGES, FRESH (113 COUNT) ",12.5,11,
"PEACHES, CLING DICED, CND, 6/10 ",23.84,19,
"PEACHES, CLING SLICED, CND 6/10 ",21.43,5,
"PEACHES, DICED, FRZ 96/4.4 OZ.",28.14,5,
"PEACHES, FREESTNE/SLC/FRZ 20 ",16.22,14,
"PEANUT BUTTER, DRUM ",772.15,5,
"PEANUT BUTTER, SMOOTH 6/5LB",48.27,43,
"PEANUT BUTTER, SMOOTH 6/5 ",48.27,41,
"PEANUTS, ROASTED, RUNNER 6/10 ",46.92,38,
"PEARS, BARTLETT, FRESH, CTN (45 LB)",18.81,5,
"PEARS, BOSC, FRESH, CTN (45 LB)",22.64,5,
"PEARS, D-ANJOU, FRESH ",27.5,1,
"PEARS, D'ANJOU, FRESH, CTN (45 LB)",18.82,5,
"PEARS, DICE, CND 6/10",23.6,18,
"PEARS, HALVES, CND 6/10 ",25.27,31,
"PEARS, SLICED, CND 6/10 ",24.51,5,
"PEARS, VARIETY (100-150 COUNT)",23.54,5,
"PEAS, GREEN, CND 6/10 ",23.56,16,
"PEAS, GREEN, FRZ 30 ",20.58,18,
"PORK, BONELESS, PICNIC",83.784,9,
"TEST",93.51,1,
"PORK, CRUMB W/SPP",89.2,3,
"PORK, HAM ROAST, FRZ 32-42 ",64.6,44,
"PORK, HAM ROAST, FRZ 36-42 ",64.6,3,
"PORK, HAM, WATERAD, CHILLED, PKG-4/10 LB",53.66,6,
"PORK, PTY LNK, 1 OZ. CKD 8/5",69.72,6,
"PORK, SEASND, PATTY, 1.2 OZ CKD, PKG-4/10 LB",57.08,53
};
//Create an array named "temp" where to add if any items are found with more than 10 stock items
int item=0; // for stock more than 10 times
double temp1=0; //for bubbleshorting
int temp2=0; //for bubbleshorting
char temp3[Len]; //for bubbleshorting
char checkname[Len]; // for checking a product's name
struct product tempPr[Total];
char choice; // For the switch module
int i,j; // for the loops
int max=products[0].stock; // for most stock
int min=10; // for stock less than 10 items
// Print the menu
printf(" ==========================================================\n");
printf("|Choose the number of the action you wish and click Enter: |\n");
printf(" ==========================================================\n");
printf("|1|: Print the products with the most stock items.\n|2|: Print the products with less than 10 stock items.\n|3|: Print the products with more than 10 stock items in price descending order.\n|4|: Enter product name for its information.\n|5|: -.\n|6|: -.\n|7|: Exit.\n");
printf("==========================================================\n");
scanf ("%c",&choice); // for choosing which action to do
switch (choice)
{
case '1':
// To find the product with the most stock items
for (i = 0; i < Total; i++ )
{
if (products[i].stock>=max)
{
max=products[i].stock;
}
}
// Print the product or products with the most stock items
for (i = 0; i < Total-1; i++)
{
if (products[i].stock==max)
{
printf("\nThe product/products with the most stock is/are:\nProduct No: %d\nProion: %s \nPrice: %.2f euro \nStock: %d pieces\n",i,products[i].name,products[i].price,products[i].stock);
}
}
break;
case '2':
// To find the product with less than 10 stock items and print it
for (i = 0; i < Total-1; i++ )
{
if (products[i].stock<min)
{
min=products[i].stock;
printf("\nA product with less than 10 stock is:\nProduct No: %d\nProion: %s \nPrice: %.2f euro \nStock: %d pieces\n",i,products[i].name,products[i].price,products[i].stock);
}
}
break;
case '3':
// To find the products with more than 10 stock items in descending order
for (i = 0; i <= Total-1; i++)
{
if (products[i].stock>=10)
{
strcpy(tempPr[item].name,products[i].name);
tempPr[item].price=products[i].price;
tempPr[item].stock=products[i].stock;
item=item+1;
}
}
for (i = 0; i < item-1; i++)
{
for (j=0; j < item-1; j++)
{
if (tempPr[j].price<tempPr[j+1].price)
{
temp1=tempPr[j+1].price;
temp2=tempPr[j+1].stock;
strcpy(temp3,tempPr[j+1].name);
tempPr[j+1].price=tempPr[j].price;
tempPr[j+1].stock=tempPr[j].stock;
strcpy(tempPr[j+1].name,tempPr[j].name);
tempPr[j].price=temp1;
tempPr[j].stock=temp2;
strcpy(tempPr[j].name,temp3);
}
}
}
for (i = 0; i < item; i++)
{
printf("No: %d\n Name: %s\n Price: %.2f\n Stock:%d\n",i,tempPr[i].name,tempPr[i].price,tempPr[i].stock);
}
break;
case '4':
printf("Enter product's name:\n");
scanf("%s",&checkname );
for (i = 0; i < Total-1; i++)
{
if ((strcmp(*checkname,products[i].name)=0)) {
printf("Found!\n");
}
else {
printf("Not found!\n");
break;
}
}
break;
case '5':
break;
case '6':
break;
case '7':
printf("\nYou can now close this window.\n");
break;
}
return 0;
}
答案 0 :(得分:4)
更改
if ((strcmp(*checkname,products[i].name)=0))
到
if ((strcmp(checkname,products[i].name)==0)).
根据strcmp()
int strcmp(const char *s1, const char *s2);
它期望const char *
作为第一个参数。
另外,为了进行比较,我们使用==
,而不是=
。
最后,您可以使用scanf("%s",checkname );
。无需在那里使用&
。
答案 1 :(得分:1)
更改
scanf("%s",&checkname );
为:
scanf("%s",checkname );
和
if ((strcmp(*checkname,products[i].name)=0))
到
if ((strcmp(checkname,products[i].name)==0))
答案 2 :(得分:0)
checkname
你声明为一个char数组,这意味着你需要读取一个字符串:
scanf("%s", checkname );
你不应该通过放置&
类型为char **
的{{1}}来传递数组的引用,而是char *
类型的数组的基地址是好的。
其次,strcmp
接受两个字符串指针进行比较,checkname
的相同基址是指向数组的指针,所以只需将其基址传递给:
strcmp(checkname, products[i].name)