Stack Overflow的优秀人才。需要一些帮助我的代码。我基本上试图将存储在变量itemCode,itemName,pricepu和stock中的数据放入数组中,我没有IDEA怎么做..
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#define totalitems 20
#define maxitems 300
void mainmenu(void);
void alt_menu(void);
void purchase(void);
void edit_item(void);
void update_item(void);
void del_item(void);
void shw_item(void);
void invent_menu(void);
void alt_invent_menu(void);
void daily_trans(void);
void exit_escape(void);
void back_inventmenu(void);
void back_menu(void);
void gst_array(void);
char invent_back, back; //global declarations
char input, newinput;
char *barcode;
char taxitems[20][20];
char ntaxitems[20][20];
char line[2048];
char *itemCode[20][20];
char itemName[20][20];
unsigned char pricepu[10][20];
char stock[10][20];
int i, ret, quantpurchase;
double total;
void purchase() {
char exitcode[] = "-1";
barcode = (char*) malloc(5);
printf(" Please key in the barcode number of the product: ");
scanf(" %s", barcode);
while(strcmp(exitcode, barcode) != '\0') {
double priceint;
FILE *gst = fopen("gst.txt", "r");
FILE *ngst = fopen("ngst.txt", "r");
printf("\n");
printf("\n Item Code Entered: %s\n", barcode);
while(fgets(line, sizeof(line), gst) != NULL) { //while fgets does not fail to scan a line
if(sscanf(line, "%[^;];%[^;];%[^;];%[^;]", itemCode, itemName, pricepu, stock) != 4) {
//If sscanf failed to scan everything from the scanned line
//%[^;] scans everything until a ';'
printf("Bad line detected\n");
exit(-1); //Exit the program
}
for (i=0; i < totalitems; i++) {
if (strcmp(itemCode[i], barcode) == '\0') {
printf("\n");
printf(" Item Found.\n");
printf("\n");
printf("===========================================================================\n");
printf(" Item code\t Item name \t\t\tPrice\t Stock Available \n");
printf("===========================================================================\n");
printf(" %-10s\t %-16s\t\t%s\t %s\n", itemCode, itemName, pricepu, stock);
printf("\n");
printf("\n");
priceint = strtod(pricepu, (char **)NULL);
printf(" How many would you like: ");
scanf("%d", &quantpurchase);
total = priceint * quantpurchase;
printf(" %.2lf\n", total);
for (i=0; i<maxitems; i++) {
taxitems[0][i] = itemCode[i];
printf(" %s", taxitems[0][0]); //HERE'S WHERE I NEED HELP
}
purchase();
}
}
}
}
}
它没有给我我想要的结果。基本上说“AG001”目前存储在变量“itemCode”下,我想把它移到一个数组中。感谢我能得到的所有帮助。感谢。
答案 0 :(得分:0)
taxitems[0][i] = itemCode[i];
您无法使用简单赋值复制char数组。您需要使用strcpy()函数。
strcpy( taxitems[0][i], itemcode[i], strlen(itemcode[i]);
您需要添加
#include <string.h>
代码。您可以找到对strcpy()函数here
的引用